关于arrayAdapter

问题描述 投票:0回答:1

我在我的应用程序中有ListViewadapter,我使用它来显示一个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?还是用其他东西?

提前致谢。

android listview adapter
1个回答
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;
       }

   }
© www.soinside.com 2019 - 2024. All rights reserved.