【问题标题】:getView() of a custom adapter giving error自定义适配器的 getView() 给出错误
【发布时间】:2014-05-31 14:28:32
【问题描述】:

我希望listview 的每一项都有不同的背景颜色。以下是我的自定义适配器getView() 方法的代码。

public View getView(int position, View convertView, ViewGroup parent) {

    convertView = mInflater.inflate(mViewResourceId, null);
    View v = super.getView(position, convertView, parent);
    RatingBar ratingBar = ((RatingBar)convertView.findViewById(R.id.my_choices_row_rating));
    ratingBar.setRating(mRatings[position]);


    TextView tv = (TextView)convertView.findViewById(R.id.my_choices_row_text);
    tv.setText(mStrings[position]);

    Log.v(TAG,"getView nalist=" + mNAList[position]);
    Log.v(TAG,"getView gotlist=" + mGotList[position]);

    if(mNAList[position].equalsIgnoreCase("N/A")){
        TextView tv1 = (TextView)convertView.findViewById(R.id.my_choices_row_na_text);
        tv1.setText(mNAList[position]);
    }

    if(mGotList[position].equalsIgnoreCase("Purchased")){
        TextView tv2 = (TextView)convertView.findViewById(R.id.my_choices_row_got_text);
        tv2.setText(mGotList[position]);
    }
    convertView.setBackgroundColor(mColors[position]);

    return convertView;
}
}

这是我的列表行的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id = "@+id/RHE"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="0"
         android:padding="5dp"
         android:layout_span = "3">

     <TableRow android:layout_height="wrap_content">
        <RatingBar
            android:id="@+id/my_choices_row_rating"
            android:progressDrawable="@drawable/hearts_rating_bar"
            android:layout_width="fill_parent"
            android:paddingTop="5dp"
            android:layout_height="25sp"
            android:numStars="5"
            android:stepSize="1" />

        <TextView
            android:id="@+id/my_choices_row_text"
            android:paddingLeft="2px"
            android:paddingRight="2px"
            android:paddingTop="2px"
            android:textSize="18sp"
            android:gravity="left|center"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <CheckBox
            android:id="@+id/my_choices_row_checkBox"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:gravity="right"
            android:focusable="false"
            android:focusableInTouchMode="false"
             />
    </TableRow>

     <TableRow android:layout_height="wrap_content">

        <TextView
            android:id="@+id/my_choices_row_na_text"
            android:textSize="12sp"
            android:gravity="left|center"
            android:textColor="#8B0000"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/my_choices_row_got_text"
            android:textSize="12sp"
            android:textColor="#8B0000"
            android:gravity="left"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

    </TableRow>
</TableLayout>  
</LinearLayout>

我不断收到以下错误

06-01 00:20:52.057: E/AndroidRuntime(711):FATAL EXCEPTION:main
06-01 00:20:52.057: E/AndroidRuntime(711):java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
06-01 00:20:52.057: E/AndroidRuntime(711):at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)

【问题讨论】:

  • 检查您的 mViewResourceId。你确定它是 TextView 吗?
  • 问题与构造函数有关。超级期望布局包含一个文本视图,而您正在为它提供其他内容。
  • 我已经编辑了我的问题并添加了我的布局代码。
  • 尝试在行布局中添加一个文本视图。你不需要为这个文本视图做任何事情。
  • tana@ 我的行布局中已经有 3 个文本视图。

标签: android listview android-arrayadapter custom-adapter


【解决方案1】:

您的教程
有什么不懂的问我

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //your colors list
        ArrayList<Integer> colors  = new ArrayList<Integer>();
        for(int i = 0; i < 10; i++){
            Random ran = new Random();
            int color = ran.nextInt();
            colors.add(color);
        }

        //get listview and set adapter for it
        ListView lv = (ListView) findViewById(R.id.myListView);
        lv.setAdapter(new MyColorAdapter(this, colors));
    }

    public class MyColorAdapter extends ArrayAdapter<Integer>{
        ArrayList<Integer> colors;
        Context context;

        public MyColorAdapter(Context context, ArrayList<Integer> colors) {
            super(context, R.layout.row, colors);
            this.colors = colors;
            this.context = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.row, parent, false);
            rowView.setBackgroundColor(colors.get(position));
            return rowView;
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 2015-10-10
    • 1970-01-01
    相关资源
    最近更新 更多