简体   繁体   中英

Android How to refresh items after deleting from sqlite database in ExpandableListView

SQLController dbcon;
@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    final String children = (String) getChild(groupPosition, childPosition);
    TextView text;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.listrow_details, null);
    }
    text = (TextView) convertView.findViewById(R.id.textView1);
    text.setText(children);
    convertView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(activity, children,Toast.LENGTH_SHORT).show();
        }
    });

    convertView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {

            AlertDialog.Builder Delete=new AlertDialog.Builder(activity);
            Delete.setTitle("Show Linups Or Delete");
            Delete.setMessage("Press Delete For Remove "+children+" or Press Show Lineups to get Lineups.");
            Delete.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                   dbcon=new SQLController(activity);
                    dbcon.open();
                    Cursor c=dbcon.returnData();
                    if(c.moveToFirst())
                    {
                        do{
                            if(children.equals(c.getString(0))){
                                dbcon.deleteData(children);
                                notifyDataSetChanged();
                                Toast.makeText(activity,"Successfully Deleted",Toast.LENGTH_LONG).show();
                                break;
                            }
                        }while(c.moveToNext());
                    }
                    dbcon.close();
                }
            }).show();
            return false;
        }
    });
    return convertView;
}

This is my Group class with List in it,

public class Group {

public String string;
public final List<String> children = new ArrayList<String>();

public Group(String string) {
    this.string = string;
}

}

This is my Expandable list adapter's getChildView Method, I am using Sqlite database and I store data to database and need to delete some items also so i use onLongClickListener for Deleting items, when I long click on the item I want to delete a popup appears having delete button, when I click on that button the item from database deleted but the item still appears on that activity until I reopen the application, what I want is when I click on delete button it should be disappear from that list also immediately, Thanks in Advance,

You would have to remove the item from the list in this part of the code:

Cursor c=dbcon.returnData();
if(c.moveToFirst()){
   do{
       if(children.equals(c.getString(0))){
       dbcon.deleteData(children);
       removeItemFromList(c.getString(0);
       notifyDataSetChanged();
       Toast.makeText(activity,"Successfully Deleted",Toast.LENGTH_LONG).show();
       break;
       }
    }while(c.moveToNext());
}

You just need to search for the string in your list and remove it.

private removeItemFromList(String object){
     int index = children.indexOf(object);
     if(index > -1){
        children.remove(index);
     }
}

Then when you call notifyDataSetChanged the list should update to reflect the change in data backing the ListView . Otherwise if you wanted to run this entirely off the SQLite you could use a cursor adapter instead but that would require converting the whole setup.

Edit

If you cannot use indexof just manually search for the string

int size = children.size();
int targetLocation = 0;
for(int i = 0; i < size; i++){
    String indexString = children.get(i);
    if(indexString.equals(object)){
          targetLocation = i;
          break;
    }
}
children.remove(targetLocation);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM