Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

refresh ListView after delete from database

I have a ListView with a SimpleCursorAdapter,it load informations from a database,I want onlongclicklistener to delete an item from database,it works fine,but the list does not refreshes,I tried with listView.refreshDrawableState(); and some other methods that I found as answer but no one works,this is my onCreate function:

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_act);
    imzero = (ImageView)findViewById (R.id.iv_zero_pers);
    tvzero = (TextView)findViewById (R.id.tv_zero_pers);
    dbConnector = new DatabaseConnectorrso(MyListActivity.this);
    dbConnector.checkCursor();
    if (DatabaseConnector.de)//check if database is emplty
    {
    try{                
        dbConnector.open();             
        dbConnector.load();//load information from database
        dbConnector.close();
        imzero.setVisibility(View.GONE);
        tvzero.setVisibility(View.GONE);
    }
    catch(Exception ex) {
        ex.printStackTrace();
        dbConnector.close();
    }
    conAdapter =(new ImageAndTextAdapter(MyListActivity.this,
            R.layout.meniu_lista_element, null, null, null));
    setListAdapter(conAdapter);
    }
    else{
        dbConnector.close();
        imzero.setVisibility(View.VISIBLE);
        tvzero.setVisibility(View.VISIBLE);
    }

and I tried to refresh the list like this

   listView.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            deleteDialog(Integer.parseInt (DatabaseConnector.id.get(position))); //delete from database 
            dbConnector.checkCursor();
            if (DatabaseConnector.de)
            {
            try{                
                dbConnector.open();             
                dbConnector.loadPersoane();
                listView.refreshDrawableState();
                dbConnector.close();
                imzero.setVisibility(View.GONE);
                tvzero.setVisibility(View.GONE);
            }
            catch(Exception ex) {
                ex.printStackTrace();
                dbConnector.close();
            }
            conAdapter =(new ImageAndTextAdapter(AltePersoane.this,
                    R.layout.meniu_lista_element, null, null, null));
            setListAdapter(conAdapter);
            }
            else{
                dbConnector.close();
                imzero.setVisibility(View.VISIBLE);
                tvzero.setVisibility(View.VISIBLE);
            }

            return true;
        }
    }); 
like image 497
Stern Edi Avatar asked Nov 24 '22 09:11

Stern Edi


1 Answers

Try

yourAdapter.notifyDataSetChanged();

or

yourAdapter.getCursor().requery();

Update:

I think you're doing something wrong, try this:

listView.setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        deleteDialog(Integer.parseInt (DatabaseConnector.id.get(position))); //delete from database 
        dbConnector.checkCursor();
        if (DatabaseConnector.de)
        {
        try{                
            dbConnector.open();             
            dbConnector.loadPersoane();
                                //Removed
            dbConnector.close();
            imzero.setVisibility(View.GONE);
            tvzero.setVisibility(View.GONE);
        }
        catch(Exception ex) {
            ex.printStackTrace();
            dbConnector.close();
        }
        conAdapter.notifyDataSetChanged();
        }
        else{
            dbConnector.close();
            imzero.setVisibility(View.VISIBLE);
            tvzero.setVisibility(View.VISIBLE);
        }

        return true;
    }
}); 
like image 100
ThePCWizard Avatar answered Jun 21 '23 02:06

ThePCWizard