0

我在列表视图中显示名称,带有复选框,如果我单击该按钮时我需要删除选定的项目并从列表视图中,仅保留我需要在列表视图中显示的项目,这里我有按钮,我可以使用下面的代码找到我选择的项目,但我不知道如何删除这些项目,你能建议我吗

MainActivity.class: 
   public class MainActivity extends Activity {

 private ListView listview;  
 ArrayList<String> items = new ArrayList<String>();  
 private int count;  
 private boolean[] thumbnailsselection;  
 @Override  
 protected void onCreate(Bundle savedInstanceState) {  
      super.onCreate(savedInstanceState);  
      setContentView(R.layout.activity_main);  
      fillarray();  
      count = items.size();  
      thumbnailsselection = new boolean[count];  
      listview = (ListView) findViewById(R.id.listView1);  
      listview.setAdapter(new ImageAdapter(MainActivity.this));  
 }  
 private void fillarray() {  
      // TODO Auto-generated method stub  
      items.clear();  
      items.add("Android alpha");  
      items.add("Android beta");  
      items.add("1.5 Cupcake (API level 3)");  
      items.add("1.6 Donut (API level 4)");  
      items.add("2.0 Eclair (API level 5)");  
      items.add("2.0.1 Eclair (API level 6)");  

 }  
 @Override  
 public boolean onCreateOptionsMenu(Menu menu) {  
      // Inflate the menu; this adds items to the action bar if it is present.  
      getMenuInflater().inflate(R.menu.activity_main, menu);  
      return true;  
 }  
 public class ImageAdapter extends BaseAdapter {  
      private LayoutInflater mInflater;  
      private Context mContext;  
      public ImageAdapter(Context context) {  
           mContext = context;  
      }  
      public int getCount() {  
           return count;  
      }  
      public Object getItem(int position) {  
           return position;  
      }  
      public long getItemId(int position) {  
           return position;  
      }  
      public View getView(int position, View convertView, ViewGroup parent) {  
           ViewHolder holder;  
           if (convertView == null) {  
                holder = new ViewHolder();  
                convertView = LayoutInflater.from(mContext).inflate(  
                          R.layout.row_photo, null);  
                holder.textview = (TextView) convertView  
                          .findViewById(R.id.thumbImage);  
                holder.checkbox = (CheckBox) convertView  
                          .findViewById(R.id.itemCheckBox);  
                convertView.setTag(holder);  
           } else {  
                holder = (ViewHolder) convertView.getTag();  
           }  
           holder.checkbox.setId(position);  
           holder.textview.setId(position);  
           holder.checkbox.setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {  
                     // TODO Auto-generated method stub  
                     CheckBox cb = (CheckBox) v;  
                     int id = cb.getId();  
                     if (thumbnailsselection[id]) {  
                          cb.setChecked(false);  
                          thumbnailsselection[id] = false;  
                     } else {  
                          cb.setChecked(true);  
                          thumbnailsselection[id] = true;  
                     }  
                }  
           });  
           holder.textview.setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {  
                     // TODO Auto-generated method stub  
                     int id = v.getId();  
                }  
           });  
           holder.textview.setText(items.get(position));  
           holder.checkbox.setChecked(thumbnailsselection[position]);  
           holder.id = position;  
           return convertView;  
      }  

    public void removeItems()
      {
          notifyDataSetChanged();
      }
 }  
 class ViewHolder {  
      TextView textview;  
      CheckBox checkbox;  
      int id;  
 }  
 public void click(View v) {  
      if (v.getId() == R.id.button1) {  

           boolean noSelect = false;  

           for (int i = 0; i < thumbnailsselection.length; i++) {  
              if (thumbnailsselection[i] == true) {  
                   noSelect = true;  

                       items.remove(i);

                }  


         }  
          thumbnailsselection = new boolean[items.size()];
         adapter.removeItems(); 
      }  
 }  
}
4

4 回答 4

1

尝试以下解决方案

在 oncreate 之前声明这个

ImageAdapter adapter;

在你的 oncreate 改变

ImageAdapter adapter=new ImageAdapter(MainActivity.this);
listview.setAdapter(adapter);

在 ImageAdapter 中添加以下方法

public void removeItems()
{
    notifyDataSetChanged();
}

在您的 onclick 方法中

for (int i = 0; i < thumbnailsselection.length; i++) {  
            if (thumbnailsselection[i] == true) {  
                 noSelect = true;  
                 Log.e("sel pos thu-->", "" + i);  
            //     posSel.add(i);  //
                     items.remove(i);
                 // break;  
            }  
       }  
thumbnailsselection = new boolean[items.size()];
adapter.removeItems();
于 2013-06-03T10:04:22.883 回答
1
  1. 记录所有 CheckBox 的状态。

    • 这里的数据类型SparseBooleanArrayboolean[]好。
    • ImageAdapter 构造函数中初始化thumbnailsselection ,并在 checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener () {})中修改其值。

    // Initialize

    for (int i = 0; i < itemCount; i++)
        thumbnailsselection.put(i, false);
    

    // Modify

    holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int position = buttonView.getId();
            thumbnailsselection.put(position, isChecked);
        }
    });
    
  2. 一个一个地从后到前删除项目,因为删除只影响被删除项目后面的项目的索引。

    for (int i = itemCount - 1; i >= 0; i--) {
        position = thumbnailsselection.keyAt(i);
        isChecked = thumbnailsselection.get(position, false);
    
        if (!isChecked)
            continue;
    
        if (position < items.size())
            items.remove(position);
    }
    
  3. 显示新数据。

    imageAdapter.notifyDataSetChanged();
    
于 2013-07-26T07:10:39.463 回答
0

我有点困惑,因为我认为您的适配器甚至在删除之前都不会显示您想要的内容,因为:

public int getCount() {  
           return count;  
}  
public Object getItem(int position) {  
      return position;  
}  

一旦您的适配器正确设置了要显示的数据数组(在您的情况下items,我认为),您只需从该数组()中删除要删除的对象items.remove(obj),然后调用notifyDataSetChanged()适配器。

编辑:

我认为您的代码中存在一些逻辑问题。我假设您想要显示来自 的数据,Array因此您可能应该扩展ArrayAdapter代替BaseAdapter. 接下来我认为项目和项目位置之间存在混淆。getItem()对于数组,一个经典的实现是:

public Object getItem(int position) {  
      return array.get(position);  
}

也许我完全错了,但我认为你应该首先清理你的代码以完全满足你的需求,然后很容易实现你想要的。

于 2013-06-03T09:34:27.287 回答
0

您可以尝试下面的代码并将其写入按钮单击事件

        SparseBooleanArray checked = keywordsList.getCheckedItemPositions();

        ArrayList<String> selectedKeywords = new ArrayList<String>();
        for (int i = 0; i < checked.size(); i++) {
            int position = checked.keyAt(i);
            if (checked.valueAt(i))
                selectedKeywords.add(keywordsAdapter.getItem(position));
        }

        for (int i = 0; i < selectedKeywords.size(); i++) {
            keywordsAdapter.remove(selectedKeywords.get(i));
        }

        keywordsAdapter.notifyDataSetChanged();

这里我的适配器代码是这样的

        keywordsAdapter = new ArrayAdapter<String>(
                            IncidentEditActivity.this,
                            android.R.layout.simple_list_item_multiple_choice,
                            arrKeywords);
        keywordsList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        keywordsList.setAdapter(keywordsAdapter);
于 2013-06-03T09:59:14.747 回答