简体   繁体   中英

ListView not getting updated with adapter in AsyncTask

I am a beginner in Android and SQLite and in my app, I'm getting data from the SQLite database and adding it to ListView using AsyncTask. But the ListView is not getting updated in the onPostExecute method.

The main activity code -

private class Updateentries extends AsyncTask<String, Void, Void>{

    @Override
    protected Void doInBackground(String... strings) {
        cursor = db.query(SQLiteHelper.Table_name, new String[]{"name"}, "name like ?", new String[]{"%"+strings[0]+"%"}, null, null, null );
        cursorarray = new ArrayList<>();
        if(!cursor.moveToFirst()){
            Toast.makeText(OtherActivity.this, "NO DATA FOUND", Toast.LENGTH_SHORT).show();
        }else{
            do {
                String entry = cursor.getString(cursor.getColumnIndex(SQLiteHelper.name_column));
                cursorarray.add(entry);
            }while(cursor.moveToNext());
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        displaylistadapt = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,cursorarray);
        displaylist.setAdapter(displaylistadapt);
        super.onPostExecute(aVoid);
    }
}

public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.searchactionbar, menu);
    searchtoggle = (SearchView) menu.findItem(R.id.search_toggle).getActionView();

    searchtoggle.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            new Updateentries().execute(query);
            return false;
        }
        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });

    return super.onCreateOptionsMenu(menu);
}

My app uses a search view to fetch data from the database and then show it in listview.

it's better to create views and adapter in the first then when you fetch your data update adapter and call adapter. notifyDataSetChanged()

also instead of getApplicationContext() use activityContext

ArrayList<String> cursorarray = new ArrayList();

class MainActivity{

void oncreate(){

...
     displaylistadapt = new ArrayAdapter(this, android.R.layout.simple_list_item_1,cursorarray);
     displaylist.setAdapter(displaylistadapt);


....


private class Updateentries extends AsyncTask<String, Void, ArrayList<String>>{

@Override
protected ArrayList<String> doInBackground(String... strings) {
    cursor = db.query(SQLiteHelper.Table_name, new String[]{"name"}, "name like ?", new String[]{"%"+strings[0]+"%"}, null, null, null );
    ArrayList <String> list = new ArrayList<>();
    if(!cursor.moveToFirst()){
        Toast.makeText(OtherActivity.this, "NO DATA FOUND", Toast.LENGTH_SHORT).show();
    }else{
        do {
            String entry = cursor.getString(cursor.getColumnIndex(SQLiteHelper.name_column));
            list.add(entry);
        }while(cursor.moveToNext());
    }
    return list;
}



   @Override
   protected void onPostExecute(ArrayList<String> list) {
        cursorarray.clear();
        cursorarray.addAll(list);
        displaylistadapt.notifyDataSetChanged();
   }
}

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