0
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 need to remove the item from the collection backing your adapter and then call adapter.notifyDataSetChanged() to update the display – Rarw Apr 17 '14 at 15:10
  • Thanks For replying, have you post some sample code for how i remove the items? –  Apr 17 '14 at 15:13
  • Have you help me by giving sample code? –  Apr 17 '14 at 15:29
  • Where is your list? I don't see it in the code. Or is this a cursor adapter? – Rarw Apr 17 '14 at 16:21
  • This is my Group class with list in it, public class Group { public String string; public final List children = new ArrayList(); public Group(String string) { this.string = string; } } –  Apr 18 '14 at 06:45
  • This may be helpful for you... http://stackoverflow.com/questions/14598197/how-to-update-the-listview-when-i-click-the-button – Bhaskar Apr 18 '14 at 06:50
  • The post you suggest is not helpful it just update the textview by assigning a value to it, i am facing a problem that i could not delete the item from expandable list view –  Apr 18 '14 at 07:04

1 Answers1

0

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);
Rarw
  • 7,645
  • 3
  • 28
  • 46
  • your method removeItemFromList is not working, what does index.remove(index) means, is it not recursive? remove gives cannot resolve variable remove, and Group.this also has a error. –  Apr 18 '14 at 15:09
  • that's a typo - its children.remove(index). - As for Group.this, looking at your code above you're removing from within an onclick listener so you may not have been able to reference the list directly. In a callback you would have to reference that differently. Also I'm writing this in the answer, I'm not testing this as I do it. You may need to tweek things and not just copy paste. – Rarw Apr 18 '14 at 16:16
  • Thankyou so very much Rarw, You helped me a lot my problem solve now with your perfect answer, Thanks alot Sir. :) –  Apr 21 '14 at 12:07
  • sir its delete the top most item only rather than what i want to delete –  Apr 21 '14 at 13:05
  • Sir its ow-some, You are great, what a logic Sir. Thanks a lot, Thank-you very much its great logic, –  Apr 21 '14 at 15:29