开发者

Android - Listview delete item and Refresh

开发者 https://www.devze.com 2023-02-03 09:25 出处:网络
开发者_开发百科I am trying to implement ListView with Delete functionality to delete item from the listview. I am successful to delete but failed to refresh the listview after deletetion of an item fr

开发者_开发百科I am trying to implement ListView with Delete functionality to delete item from the listview. I am successful to delete but failed to refresh the listview after deletetion of an item from the database.

Actually, Click on listitem, i am displaying AlertBox for "Delete" and "Cancel" action, on clicking "Delete", item should be removed from the database and as well as from the listview and listview should be refreshed. I have also used notifyDataSetChanged() method.

lview = (ListView) findViewById(R.id.lview);
adapter = new ListView_CustomAdapter(this, listitemDisplay);
lview.setAdapter(adapter);

lview.setOnItemClickListener(new OnItemClickListener() 
{
     @Override
     public void onItemClick(AdapterView<?> a, View v, int position, long id)
     {
        Show_Alert_box(v.getContext(),"Please select action.",position);
     }
});

and the code for Show_Alert_box:

 public void Show_Alert_box(Context context, String message,int position)
     {
         final int pos = position;

         final AlertDialog alertDialog = new  AlertDialog.Builder(context).create();
            alertDialog.setTitle(getString(R.string.app_name_for_alert_Dialog));
            alertDialog.setButton("Delete", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    try
                    {
                        db.open();
                                 String[] whereArgs={String.valueOf(pkID)};
        return db.delete(DATABASE_TABLE_4,"pk_pkID == ?",whereArgs);    
                        adapter.notifyDataSetChanged();
                        db.close();
                    }
                    catch(Exception e)
                    {

                    }
            } }); 
            alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
            } }); 

            alertDialog.setMessage(message);
            alertDialog.show();
     }


Does it remove it from your list adapter? If not that would be the reason the notifyDataSetChanged() won't do you much good.

Actually looking at your code again i can only find that you're removing it from your database and not the adapter itself.

edit (to answer comment): Well that's hard to do without your ListView_CustomAdapter class. The problem is, in this adapter there's a data set (the one you put in the constructor (listitemDisplay)) which needs to be updated as well. Only then the notifyDataSetChanged() will work.


Call that Activity once again Using Intent


I'm guessing that using

getActivity().recreate();

instead of restarting the activity via a new Intent is better because using a new Intent will only stop the current activity and not destroy it.

Anyway, it works.


if you have the cursor, call requery() before calling notifyDataChanged()


I did something like this in my adapter:

((Activity)cxt).finish();
Intent intent = new Intent(cxt, YourActivity.class);
cxt.startActivity(intent);

This ends the activity and then starts the same one again.


I think instead of calling the activity again, you should set the adapter to the listview on the alertBox delete option after getting the updated data from the database and putting into listitemDisplay list like this.

alertDialog.setButton("Delete", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                try
                {
                    db.open();
                             String[] whereArgs={String.valueOf(pkID)};
                    return db.delete(DATABASE_TABLE_4,"pk_pkID == ?",whereArgs); 
                    listitemDisplay = db.getItemFromDB();
                    adapter = new ListView_CustomAdapter(this, listitemDisplay);
                    lview.setAdapter(adapter);
                    db.close();
                }
                catch(Exception e)
                {

                }
        } }); 

This will refresh the listView


I have the solution:

If you want to delete a row from a list view clicking on the DELETE button of each of that row do this. Here you have an example of a custom adapter class with a name and a delete button. Every time you press the button the row is deleted

public class UserCustomAdapter extends ArrayAdapter<User>{ 

Context context;
int layoutResourceId;
ArrayList<User> data = new ArrayList<User>();

public UserCustomAdapter(Context context, int layoutResourceId,ArrayList<User> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    UserHolder holder = null;


    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new UserHolder();
        holder.textName = (TextView) row.findViewById(R.id.textView1);
        holder.btnDelete = (Button) row.findViewById(R.id.button2);
        row.setTag(holder);
    } else {
        holder = (UserHolder) row.getTag();

    }

    User user = data.get(position);


    holder.btnDelete.setTag(position);
    holder.textName.setText(user.getName());



    holder.btnDelete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            String pos = v.getTag().toString();
            int _posicion = Integer.parseInt(pos);
            data.remove(_posicion);
            notifyDataSetChanged();

        }
    });

    return row;


}

static class UserHolder {
    TextView textName;
    Button btnDelete;
}
}


Try calling refreshDrawableState to tell the list to redraw.


Make a new function outside your onCreate block {something like... getdata()} and inside that insert and get all your data and set to the adapter.
Then call the function again in your onResume() block. So whenever you will delete the data from the list it will reflect immediately.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号