How to delete a selected item from the listview?
SORRY FOR THIS CONFUSION: UPDATED QUESTION: I'm trying to remove a list item from the listview. when the item is clicked, the alertdialog is shown. If i click OK, then the selected item must be removed from the listview. My Code goes below:
case R.id.lvinc:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delet开发者_高级运维e Event ");
builder.setMessage("Delete this Event ?");
builder.setPositiveButton("Ok, Delete",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try
{
???? //What code to delete the selected list item?
}catch(Exception e)
{
e.printStackTrace();
}
}
});
AlertDialog alert = builder.create();
alert.show();
displaylist();
break;
Any help is really appreciated and thanks in advance...
I tried to solve it and got one solution pls go through the code below:
listview.java
public class listview extends Activity implements OnItemClickListener{
ListView list;
ListAdapter adapter;
ArrayList<String> nameArray;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//xml should have an ListView element
nameArray = new ArrayList<String>();
nameArray.add("Item1");
nameArray.add("Item2");
nameArray.add("Item3");
nameArray.add("Item4");
nameArray.add("Item5");
list = (ListView) findViewById(R.id.listView);
list.setOnItemClickListener(listview.this);
adapter=new ListAdapter(listview.this, nameArray);
list.setAdapter(adapter);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
showDialog(arg2);
}
@Override
protected Dialog onCreateDialog(final int id) {
Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Delete Event")
.setCancelable(true)
.setPositiveButton("Ok, Delete",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
nameArray.remove(id);
adapter=new ListAdapter(listview.this, nameArray);
list.setAdapter(adapter);
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
return super.onCreateDialog(id);
}
}
//Adapter Class
ListAdapter.java
public class ListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<String> name;
private static LayoutInflater inflater=null;
public ListAdapter(Activity a, ArrayList<String> nameArray) {
activity = a;
name = nameArray;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return name.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView text;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.list_item, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.title);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.text.setText(name.get(position));
return vi;
}
}
I don't know what kind of data you are using.
I imagine Cursor, database or List, feel free to tell us, so it will be easier to help.
This example is for a list:
protected void onListItemClick(View v, int pos, long id) {
Log.i(TAG, "onListItemClick id=" + id);
//Display your Dialog
(...)
builder.setPositiveButton("Ok, Delete",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
myList.remove(pos);
myAdapter.notifyDataChanged();
}
});
}
You propably have some kind of Adapter, that you have feed with some kind of data.
Remove the item from that list, and notifyDataChanged to the ListView.
And finally: dialog.dismiss();
itemLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
indexClick = position;
}
}
if ((indexClick) == (position)) {
itemLayout.setBackgroundResource(R.drawable.tab_select);
}
otherwise put tab_unselected image.
精彩评论