简体   繁体   中英

Android ListView sqlite update Refresh

My current application has a Listview which pulls and displays data from a sqlite DB. Data in the first column of the DB is displayed in the listview and when clicked, an activity starts showing the rest of the column associated with the first column. When the data is edited, the DB updates but the listview does not show the updates unless the application is restarted. I am trying to implement startActivityForResult() in my onResume method but am unsuccessful. I am trying to refresh my listview after the DB has been updated. How can I accomplish this.

ListView Activity:

@Override 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_listview);


    loginList = (ListView)
   findViewById(R.id.loginlist);
    loginList.setOnItemClickListener(this);

    webLogin = (Button)
   findViewById(R.id.button3);
    webLogin.setOnClickListener(this);

    loginArrayList = new ArrayList<LoginDetails>();

    loginListAdapter = new ArrayAdapter<String>(this,   android.R.layout.simple_list_item_1, populateList());
    loginList.setAdapter(loginListAdapter);
}

@Override
public void onClick (View v) {
    Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
    startActivity(webLoginIntent);
}

public List<String> populateList (){

    List<String> webNameList = new ArrayList<String>();

    dataStore openHelperClass = new dataStore (this);

    SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();

    Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);

    startManagingCursor(cursor);

    while (cursor.moveToNext()){

    String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE));
    String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS));
    String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME));
    String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD));
    String lNotes = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_NOTES));

    LoginDetails lpDetails = new LoginDetails();

        lpDetails.setsName(sName);
        lpDetails.setwUrl(wUrl);
        lpDetails.setuName(uName);
        lpDetails.setpWord(pWord);
        lpDetails.setlNotes(lNotes);

        loginArrayList.add(lpDetails);
        webNameList.add(sName);
}

sqliteDatabase.close();
return webNameList;
}



@Override
protected void onResume() {
    super.onResume();

    Intent i = new Intent(LoginList.this, UpdateDeleteLoginList.class);
    LoginList.this.startActivityForResult(i, 1);    
    loginList.setAdapter(loginListAdapter);

loginListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());
loginList.setAdapter(loginListAdapter);

}

update Activity:

     @Override
public void onClick(View v){

    loginSitetext = sName.getText().toString();
    loginAddresstext = wUrl.getText().toString();
    loginUsertext = uName.getText().toString();
    loginpassWordtext = pWord.getText().toString();
    loginNotestext = lNotes.getText().toString();

    LoginDetails loginDetails = new LoginDetails();

    loginDetails.setsName(bundledWebSite);
    loginDetails.setwUrl(bundledWebAddress);
    loginDetails.setuName(bundledUserName);
    loginDetails.setpWord(bundledPassWord);
    loginDetails.setlNotes(bundledNotes);

    if(v.getId()==R.id.rucBttn){
        finish();

    }else if(v.getId()==R.id.ruuBttn){
        updateLoginDetails(loginDetails);

        Intent returnIntent = new Intent();
        setResult(RESULT_CANCELED, returnIntent);
        finish();

    }else if(v.getId()==R.id.rudBttn){
        deleteLoginDetails(loginDetails);
    }

}


private void updateLoginDetails(LoginDetails loginDetails){

    dataStore androidOpenDbHelper = new dataStore(this);

    SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase();

    ContentValues contentValues = new ContentValues();

    contentValues.put(dataStore.COLUMN_NAME_SITE, loginSitetext);
    contentValues.put(dataStore.COLUMN_NAME_ADDRESS, loginAddresstext);
    contentValues.put(dataStore.COLUMN_NAME_USERNAME, loginUsertext);
    contentValues.put(dataStore.COLUMN_NAME_PASSWORD, loginpassWordtext);
    contentValues.put(dataStore.COLUMN_NAME_NOTES, loginNotestext);

    String[] whereClauseArgument = new String[1];
    whereClauseArgument[0] = loginDetails.getsName();

    System.out.println("whereClauseArgument[0] is :" + whereClauseArgument[0]);

    sqliteDatabase.update(dataStore.TABLE_NAME_INFOTABLE, contentValues, dataStore.COLUMN_NAME_SITE+"=?", whereClauseArgument);

    sqliteDatabase.close();
    finish();

You can refresh the listview by calling notifyDataSetUpdated on the adapter. That forces the entire listview to refresh, and getView will be called on all views refreshing it.

The problem you're asking about has a standard solution, that is (unfortunately) more complex than a few lines of code. It is accomplished by a combination of Content Provider , Data Loader and Cursor Adapter .

I've put a little demo on GitHub that has a skeleton containing all the components and can be modified to accomplish what you need. It also handles one-pane / two-pane layouts on tablets (and newer phones as well).

Content provider is the module that serves as an interface to your database table and notifies your ListView about the changes, thus taking care of immediate refreshing of the list. Just grep for ' getContentResolver().notifyChange() ' calls, these take care of refreshing your list. As long as you use Content Provider for all access to your data (CRUD - query,insert,delete,update,...), all your actions will be immediately reflected in the ListView.

There is a lot of info about this by Wolfram Rittmeyer , Lars Vogel , Alex Lockwood , ... if you want to get educated about the problem.

Good Luck

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