ListView和带有插入功能的多个TextView

时间:2013-04-30 04:12:04

标签: android listview android-arrayadapter

我想在listview行中使用多个(2)textviews。我想我必须创建自定义适配器,但是使用这个适配器我想在每次单击按钮时使用插入多个数据来定位0:mAdapter.insert(mytext1, mytext2, 0);

但我无法使用示例适配器实现此目的:

public class Durations {
            private String name;
            private String address;

            public Durations(String name, String address) {
                this.name = name;
                this.address = address;
            }
            public void setName(String name) {
                this.name= name;
            }
            public String getName() {
                return name;
            }
            public void setAddress(String address) {
                this.address= address;
            }
            public String getAddress() {
                return address;
            }
        }



      public class CustomAdapter extends ArrayAdapter {
            private final Activity activity;
            private final List list;

            public CustomAdapter(Activity activity, ArrayList<Durations> list) {
                this.activity = activity;
                this.list = list;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View rowView = convertView;
                ViewHolder view;

                if(rowView == null)
                {
                    // Get a new instance of the row layout view
                    LayoutInflater inflater = activity.getLayoutInflater();
                    rowView = inflater.inflate(R.layout.row, null);

                    // Hold the view objects in an object, that way the don't need to be "re-  finded"
                    view = new ViewHolder();
                    view.retaurant_name= (TextView) rowView.findViewById(R.id.text1);
                    view.restaurant_address= (TextView) rowView.findViewById(R.id.text2);

                    rowView.setTag(view);
                } else {
                    view = (ViewHolder) rowView.getTag();
                }


                return rowView;
            }

            protected  class ViewHolder{
                protected TextView retaurant_name;
                protected TextView restaurant_address;
            }



        }

1 个答案:

答案 0 :(得分:0)

你需要覆盖ArrayAdapter函数。因为你使用的是ViewHolder,它也会重用布局。

getItem(int position)
getItemId(int position)

您也可以轻松找到基于定义自定义适配器的各种教程。