删除CustomArrayAdapter中listview上的项目

时间:2014-03-03 10:05:10

标签: java android android-listview

我的listview的每个项目都有一个图片按钮,如果您点击我要删除当前图片按钮关联或当前关注的项目。

listview = (QuickReturnListView) v.findViewById(R.id.carddemo_list_expand);

        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
            "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
            "Android", "iPhone", "WindowsMobile" };


        list = new ArrayList<String>();
        for (int i = 0; i < values.length; ++i) {
          list.add(values[i]);
        }

        adapter = new MySimpleArrayAdapter(getActivity(), list);
        listview.setAdapter(adapter);

 public class MySimpleArrayAdapter extends ArrayAdapter<String> {
      private final Context context;
      private final ArrayList<String> values;

      public MySimpleArrayAdapter(Context context, ArrayList<String> list) {
        super(context, R.layout.inner_base_header_cutom, list);
        this.context = context;
        this.values = list;
      }

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

v.remove = (ImageView)rowView.findViewById(R.id.cancel);
v.remove.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


             //Global Variables 
            list.remove(position);
            adapter.notifyDataSetChanged();


            //

        }
    });

}

3 个答案:

答案 0 :(得分:1)

ArrayAdapterremove(T object)方法,你应该调用它来清理你提供给超类的数据集:在onClick中你应该调用

remove(list.get(position));

答案 1 :(得分:0)

要首先从列表中删除项目,获取位置和从列表中删除并调用notifyDataSetChanged(),以便重新加载列表。

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

v.remove = (ImageView)rowView.findViewById(R.id.cancel);
v.remove.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        parent.remove(position);
        notifyDataSetChanged();
        //

    }
});

}

答案 2 :(得分:0)

您将在主要活动中编写列表视图OnItemclick侦听器。

你会更喜欢 ArrayList String&gt; values = new ArrayList String&gt ;;

listStudies.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position,
                long i) {
            //You just remove your date from the String Array and give adapter notifychaged....
            values.remove(position);
            adapter.notifyDataSetChanged();

        }
    });