问题
I have 50 list item in the list. Now i have checked 10 item, so how to this 10 checked item delete(remove) from the listview when I click delete button.
Here is My Code
Please see my code and response where is the error:
public class BookmarksJokes extends Activity implements OnClickListener,
OnItemClickListener {
ListView lv;
Button btn_delete;
public String TAG = "horror";
private SQLiteDatabase db;
public static final String PREFS_NAME = "MyPreferences";
static String[] tempTitle = new String[100];
static String[] tempBody = new String[100];
private static boolean bRequiresResponse;
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return tempTitle.length;
}
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) {
convertView = mInflater.inflate(R.layout.bookmarks_list_item,
null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.title);
holder.text2 = (TextView) convertView
.findViewById(R.id.body);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(tempTitle[position]);
holder.text2.setText(tempBody[position]);
// bRequiresResponse = checkBox.isChecked();
return convertView;
}
static class ViewHolder {
TextView text1;
TextView text2;
CheckBox checkBox;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bokmarksjoks);
try {
db = (new DatabaseHelper(this)).getWritableDatabase();
} catch (IOException e) {
e.printStackTrace();
}
setUpViews();
title = (TextView) findViewById(R.id.body);
SharedPreferences pref = getSharedPreferences(PREFS_NAME, 0);
//String ids = pref.getString("jid", "");
String one = pref.getString("title", "");
String two = pref.getString("body", "");
tempTitle = one.split(",");
tempBody = two.split(",");
lv.setAdapter(new EfficientAdapter(this));
}
private void setUpViews() {
lv = (ListView) findViewById(R.id.list);
btn_delete = (Button) findViewById(R.id.delete);
btn_delete.setOnClickListener(this);
// checkbox = (CheckBox) findViewById(R.id.checkbox);
}
private void removeData(){
//int pos= getIntent().getIntExtra("POSITION", 0);
//lv.removeViewAt(pos);
// notifyAll();*/
// int pos= getIntent().getIntExtra("POSITION", 0);
// if(checkBox.isChecked()){
// Log.d(TAG, " checked d]"+pos);
// Toast.makeText(this, "checked "+pos, Toast.LENGTH_SHORT).show();
// }
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.delete:
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Are you Sure want to delete all checked jok ?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
//removeJok();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
alert.setTitle("Delete Jokes");
alert.show();
case R.id.checkbox:
default:
break;
}
}
public void onItemClick(AdapterView<?> arg0, View view, int position,
long ids) {
try {
// checkBox = (CheckBox)view.findViewById(R.id.checkbox);
// checkBox.setChecked(true);
Intent intent = new Intent(BookmarksJokes.this,
BookmarkJokesDetails.class);
intent.putExtra("POSITION", position);
intent.putExtra("ID", ids);
Cursor cursor = (Cursor) adapter.getItem(position);
intent.putExtra("_ID",
cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(BookmarksJokes.this,
"Item in position " + position + " clicked", Toast.LENGTH_LONG)
.show();
}
}
Here is my full code:
http://pastebin.com/LB2WKHMP
回答1:
I performed the same task in one of my app.
What you should do is use an ArrayList having the same size as the list item and each item contains 0 by default . Now in your Efficient Adapter for each checkbox that is checked , assign 1 in that particular position in arrayList . Finally on the button click of delete , traverse through the ArrayList and delete all those item from list which contain 1 in that postion in that ArrayList and atLast call notifyDatasetChanged() method of list so that list get refreshed and shows the new list.
Here is some sample code to give you a better idea over this :
ArrayList<Integer> checks=new ArrayList<Integer>();
Now in onCreate method
for(int b=0;b<tempTitle.length;b++){
checks.add(b,0); //assign 0 by default in each position of ArrayList
}
Now in your efficientAdapter's getView method
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.bookmarks_list_item,
null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.title);
holder.text2 = (TextView) convertView
.findViewById(R.id.body);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(((CheckBox)v).isChecked()){
checks.set(position, 1);
}
else{
checks.set(position, 0);
}
}
});
return convertView;
}
Finally on delete Button click :
delete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i=0;i<checks.size();i++){
if(checks.get(i)==1){
//remove items from the list here for example from ArryList
checks.remove(i);
//similarly remove other items from the list from that particular postion
i--;
}
}
((EfficientAdapter)lv.getAdapter()).notifyDataSetChanged();
}
}
Hope this will solve your issue.
回答2:
I am assuming i got ids which are selected. now simply arrayList.remove(position); and call notifyDataSetChanged();
回答3:
man, your code lacks the relation between the check box and the object and also you didn't implement onCheckedchangeListener inside your adapter ....
here is what you need http://www.vogella.de/articles/AndroidListView/article.html#listadvanced_interactive
Or I'll wrap it for you:
you'll need to create a class model.java which represents the cell of the list as follows:
public class Model { private String title; private String body; private boolean isSelected; public Model(String title, String body) { this.title = title; this.body = body; this.isSelected = false; } // here you MUST create your set of setters and getters. }modify your adapter to extend
ArrayAdapter<Model>modify the constructor of the adapter to be
private Model[] model; public EfficientAdapter(Context context, Model model) { mInflater = LayoutInflater.from(context); this.model = model; }then you'll need to add the
onCheckedChangeListenerinside your adapter inside thegetViewmethod , so yourgetViewmethod will look like this:@Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; if (convertView == null) { LayoutInflater inflator = context.getLayoutInflater(); final ViewHolder viewHolder = new ViewHolder(); viewHolder.text1 = (TextView) convertView.findViewById(R.id.title); viewHolder.text2 = (TextView) convertView.findViewById(R.id.body); viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox); viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { Model element = (Model) viewHolder.checkbox.getTag(); element.setSelected(buttonView.isChecked()); } }); view.setTag(viewHolder); viewHolder.checkbox.setTag(list[position]); } else { view = convertView; ((ViewHolder) view.getTag()).checkbox.setTag(list[position]); } ViewHolder holder = (ViewHolder) view.getTag(); holder.text1.setText(tempTitle[position]); holder.text2.setText(tempBody[position]); return view; }then create the model array inside your activity and pass it to the adapter.
the last thing to be done is to delete the selected items from the list:
final ArrayList<Model> newModel = new ArrayList<Model>(); for (int i = 0; i < model.length/*this is the original model*/; i++) { if(model[i].isChecked()){ // fill the array list ... newModel.add(model[i]); } }that's it, pass the newModel to the adapter and rest the adapter to the list.
step number 7 can be performed also by filling the model(the original one which is passed originally to the array) and then call notifyDataSetChanged() using the adapter.
that's all and that's what always worked for me... hope this helps you too.
回答4:
If you use a listview with mode CHOICE_MODE_MULTIPLE, you should be able to use something like: listView.getCheckedItemPositions();
then if you have the list items stored in some kind of list or vector, you should be able to easily remove items with the same positions, and the updating the listview.
Altough all my checkbox + list knowledge is based on this: Android list and checkbox
回答5:
I think you problem does not need any big amount of code.You need just a concept.Answer Given above is best way of doing that.
I am assuming i got ids which are selected. now simply arrayList.remove(position);
and call notifyDataSetChanged();
As List view sub child(CheckBox) can use setOnCheckChangesListener in base Adapter class directly.Now you just have to keep track record of check checkbox and their corresponding position in ArrayList.Now when you click on delete btn then remove element from arraylist and call notifyDataSetChanged()
回答6:
Refresh the listview again when you have selected the checkbox and delete button pressed. Means you have to set the adapter again to the list view after items has been deleted. Thanks
来源:https://stackoverflow.com/questions/9016739/how-to-delete-check-box-items-from-list