Android_Daily 2015-01-14 09:39 采纳率: 0%
浏览 1525

GridView 无法显示自定义view

package com.example.image;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
/**

  • 音效视图的适配器,将音效视图填充到GridView中
  • @author lewa
    *
    */
    public class MyAdapter extends BaseAdapter {
    private List list;
    private Context context;

    public MyAdapter(List list,Context context){
    this.list = list;
    this.context = context;
    }

    @Override
    public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
    }

    @Override
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ItemView itemview;
    if(convertView == null){
    itemview = new ItemView(context);

    }else{
        itemview = (ItemView) convertView;
    }
    itemview.imageView.setImageResource(list.get(position).getImageId());
    Log.i("ymh","Position:"+position+"----List.size() = "+list.size());
    itemview.textView.setText(list.get(position).getLable());
    Log.i("ymh",itemview.textView.getText().toString());
    itemview.setLayoutParams(new GridView.LayoutParams(85,85));
    itemview.setPadding(8, 8, 8, 8);
    return itemview;
    

    }

}

 package com.example.image;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
 * 音效图片和音效名称的组合view
 * @author lewa
 *
 */
public class ItemView extends RelativeLayout {
    ImageView imageView;
    TextView textView;
    private int state = 0;
    public ItemView(Context context) {
        super(context);
        init(context);
    }

    public ItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }
    private void init(Context context){
        LayoutInflater inflater = LayoutInflater.from(context); 
        View view = inflater.inflate(R.layout.item, null);
        imageView = (ImageView) view.findViewById(R.id.item_image);
        textView = (TextView) view.findViewById(R.id.item_text);
    }
    public void OnClickListener(View.OnClickListener listener){
        if(state==0){
            imageView.setBackgroundColor(android.graphics.Color.GREEN);
            state = 1;
        }else{
            imageView.setBackgroundColor(android.graphics.Color.GRAY);
        }
    }

}

<?xml version="1.0" encoding="utf-8"?>
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
    android:id="@+id/item_image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_launcher" 
    android:padding="5dp"/>

<TextView
    android:id="@+id/item_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/item_image"
    android:layout_alignRight="@id/item_image"
    android:layout_below="@+id/item_image"
    android:gravity="center"
    android:textColor="#00ffaa"
    android:text="textview" />

  • 写回答

2条回答 默认 最新

  • danielinbiti 2015-01-14 10:06
    关注
     private void init(Context context){
            LayoutInflater inflater = LayoutInflater.from(context); 
            View view = inflater.inflate(R.layout.item, null);  //这里不能这么处理了,这么解析后,就不是你自定义new出来的布局了。
            imageView = (ImageView) view.findViewById(R.id.item_image);//把VIEW去掉,用this
            textView = (TextView) view.findViewById(R.id.item_text);//把VIEW去掉,用this
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振