关于arrayAdapter

时间:2011-06-23 20:44:47

标签: android listview adapter

我的应用中有ListView adapter,我用它来显示包含chechboxtextView

的列表
public class MyListAdapter extends ArrayAdapter<Model> {
    private LayoutInflater inflater;
    private int position;

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public MyListAdapter (Context context, List<Model> listMeasurement){
        super(context, R.layout.simplerow, R.id.empty, listMeasurement);
        inflater= LayoutInflater.from(context);
    }

    public View getView(int position, View convertView, ViewGroup parent){
        Model model= (Model)this.getItem(position);
        CheckBox checkBox;
        TextView textView;
    }
}

我的问题是:

我想在另一个list中展示另一个activity,这将有一个image,两个textViews和一个button。图像取决于textView的值。

执行此操作的最佳方法是执行其他ArrayAdapter?或使用其他东西?

提前致谢。

1 个答案:

答案 0 :(得分:1)

您需要扩展BaseAdapter并覆盖getView()方法。这是一个样本。

    private class CustomAdapter extends BaseAdapter {

        private LayoutInflater inflater;
        private ArrayList<Model> list;

        public CustomAdapter(Context context, ArrayList<Model> list) {
            this.inflater = LayoutInflater.from(context);
                        this.list = list;
        }

        @Override
        public int getCount() {
                    return list.size();
        }

        @Override
        public Object getItem(int position) {
           return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {

            // If the view is null inflate it from xml
            if (view == null)
                view = inflater.inflate(R.layout.list_row, null);

            // Bind xml to java
           ImageView icon = (ImageView) view
                .findViewById(R.id.image);
           TextView text = (TextView) view.findViewById(R.id.text);
                       text.setText(list.get(position).getText());
                       icon.setImageDrawable(list.get(position).getDrawable());

           return view;
       }

   }