我在列表视图中显示名称,带有复选框,这里我有按钮如果我点击该按钮那时我需要删除所选项目并从列表视图中保留项目我只需要在列表视图中显示,使用下面我代码我可以找到我选择的项目但我不知道如何删除这些,你能不能建议我
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();
}
}
}
答案 0 :(得分: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();
答案 1 :(得分:1)
记录所有CheckBox的状态。
// 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);
}
});
逐项删除项目从后到前,因为删除只会影响已删除项目背后的项目索引。
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);
}
显示新数据。
imageAdapter.notifyDataSetChanged();
答案 2 :(得分: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);
}
也许我完全错了,但我认为你应该首先清理你的代码以满足你的需求,然后很容易实现你想要的。
答案 3 :(得分: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);