自定义适配器的getView()给出错误

时间:2014-05-31 14:28:33

标签: android listview android-arrayadapter custom-adapter

我希望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)

1 个答案:

答案 0 :(得分:0)

你的教程
问我是否有任何你不理解的事情

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;
        }
    }

}