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个回答

 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
    }
 LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.item, null);
        textView = (TextView) view.findViewById(R.id.item_text);
        imageView = (ImageView) view.findViewById(R.id.item_image);

        this.addView(view);

试了下,这样可以

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!