7

I have 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 ListView needs to be updated to reflect this, but it doesn't show the updates unless the application is restarted.

I've tried calling, notifyDataSetChanged() and startActivityForResult() in my onResume() method but that didn't work. What method should I use to accomplish updating the ListView in my current code?

I understand a SimpleCursorAdapter may be used and I have tried to implement that code with no success. I'm a novice and need actual code to understand what needs to be done.

public class LoginList extends Activity implements OnClickListener, OnItemClickListener {
    private ListView loginList;
    private Button webLogin;

    private ListAdapter loginListAdapter;

    private ArrayList<LoginDetails> loginArrayList;

    @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();

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

    }


@Override
    public void onItemClick(AdapterView<?> arg0 , View arg1, int arg2, long arg3) {
        Toast.makeText(getApplicationContext(), "Selected ID :" + arg2, Toast.LENGTH_SHORT).show();

        Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class);
        LoginDetails clickedObject = loginArrayList.get(arg2);

        Bundle loginBundle = new Bundle();
        loginBundle.putString("clickedWebSite",clickedObject.getsName());
        loginBundle.putString("clickedWebAddress",clickedObject.getwUrl());
        loginBundle.putString("clickedUserName",clickedObject.getuName());
        loginBundle.putString("clickedPassWord",clickedObject.getpWord());
        loginBundle.putString("clickedNotes",clickedObject.getlNotes());

        updateDeleteLoginInfo.putExtras(loginBundle);

        startActivityForResult(updateDeleteLoginInfo, 0);   
    }
}
Steven Byle
  • 13,149
  • 4
  • 45
  • 57
user1165694
  • 1,255
  • 2
  • 18
  • 29
  • 1
    Did you try `SimpleCursorAdapter` instead of `ArrayAdapter`? – Glenn Feb 12 '13 at 01:34
  • Well, the fact that the code in your `onResume` can not be wrong, means that you are not editing the database correctly in your `UpdateDeleteLoginList`. And of course, you should probably use a cursor adapter for correctness and performance; though, still a matter of choice I think. – Sherif elKhatib Feb 15 '13 at 14:22

7 Answers7

5

This is exactly what a Loader is great for. I suggest you create a SimpleCursorAdapter to bind the DB to the UI (ListView in this case), a ContentProvider to interface with the DB, and a CursorLoader to monitor the DB for changes, and update the UI when necessary. The Loader will handle all DB changes and update your ListView by simply updating your adapter. It seems like a lot of work up front, but is incredibly powerful once configured, and will work through the entire Android lifecycle.

These tutorials should be helpful:

Steven Byle
  • 13,149
  • 4
  • 45
  • 57
5

Edit

private ArrayList<LoginDetails> loginArrayList = new ArrayList<LoginDetails>();;

@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);


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

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

public void populateList () {
    loginListAdapter.clear();
    loginArrayList.clear();
    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);
    }
    loginListAdapter.notifyDatasetChanged();
    sqliteDatabase.close();
}

You are losing reference to your listview that why your list isn't updating...

Do this modification in your code. 1. Initialize your ArrayList when it is declared(globally).

ArrayList loginArrayList = new ArrayList<LoginDetails>();
  1. Directly set assign list to Adapter (onCreate)

    loginListAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, loginArrayList);

  2. call populateList() in onCreate().

  3. In your populateList() instead of Adding data to the new list add to existing list associated with your Adapter i.e loginArrayList

if your list is completely new call adapter.clear() and loginArrayList.clear() in the populateList() before adding data to the loginArrayList. After Adding the data to the loginArrayList call adapter.notifyDataSetChanged()

This should Work...

Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • can you provide example for step 2 and 3 – user1165694 Feb 20 '13 at 18:56
  • I have made some changes in your code see that...I have tried the same code with my Database and is working perfectly – Pragnani Feb 20 '13 at 19:10
  • 1
    A small syntax note on `adapter.notifyDataSetChanged()`: if you are having trouble finding your adapter, consider `((BaseAdapter) getListView().getAdapter()).notifyDataSetChanged()` – abriggs May 28 '13 at 19:12
0

My recomendation.

  1. Do not do a loop to load your list. If it is a simple list with all strings. Try to use a SimpleCursorAdapter and will make your app faster and shorter code.
  2. Once you update the database, then what you do is query the DB to get the Cursor, and to the Adapter use .swapCursor(newCursor). That will update your list while maintaining the scroll position.
Glenn
  • 12,741
  • 6
  • 47
  • 48
soynerdito
  • 501
  • 3
  • 10
0

If you're manually creating the backing data of the adapter (in this case you're using an ArrayAdapter - which is completely acceptable in a lot of cases) then when the database changes you need to requery the database, recreate your dataset, change the backing dataset of your adapter, and tell the list that the dataset has changed.

A way to accomplish this may be to broadcast an intent that lets your activity know to perform the steps mentioned above (by catching that intent with a BroadcastReceiver).

Brandon
  • 1,164
  • 14
  • 22
0

Here's my sample code for editing the each row as well as the database of the listview I hope it will helps you

First create I create an Adapter name "ListAdapterItem.java"

     import java.util.ArrayList;
     import java.util.HashMap;

     import android.content.Context;
     import android.view.LayoutInflater;
     import android.view.View;
     import android.view.ViewGroup;
     import android.widget.BaseAdapter;
     import android.widget.RelativeLayout;
     import android.widget.TextView;

     public class ListAdapterItem extends BaseAdapter {

private ArrayList<HashMap<String, Object>> list;
private Context context;
private RelativeLayout ll;
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
private TextView label,label_id;
String name,id;

public ListAdapterItem (Context context,ArrayList<HashMap<String, Object>> list) {
    this.list = list;
    this.context = context;
}
public void setSelectedPosition(int pos) {
    selectedPos = pos;
    // inform the view of this change
    notifyDataSetChanged();
}
public int getSelectedPosition() {
    return selectedPos;
}
public View getView(int position, View convertView, ViewGroup parent) {
    ll = (RelativeLayout) LayoutInflater.from(this.context).inflate(R.layout.data_list_item, null);
    // get text view
    label = (TextView) ll.findViewById(R.id.txtview_country_name);
    label_id=(TextView) ll.findViewById(R.id.txtview_id);

    id=list.get(position).get("Id").toString();
    name = list.get(position).get("Name").toString();

    label.setText(name);
    label_id.setText(id);
    return ll;
}
public int getCount() {
    return this.list.size();
}
public Object getItem(int position) {
    return position;
}
public long getItemId(int position) {
    return position;
}
}

And here's my data_list_item

                            <?xml version="1.0" encoding="utf-8"?>
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:padding="5dp">
                 <TextView
                    android:id="@+id/txtview_id"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_margin="8dp"
                    android:layout_marginRight="2dp"
                    android:textColor="@android:color/black"
                    android:textSize="22dp"
                    android:visibility="invisible"/>
                <TextView
                    android:id="@+id/txtview_country_name"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_margin="8dp"
                    android:layout_marginRight="2dp"
                    android:textColor="@android:color/black"
                    android:textSize="22dp" />


            </RelativeLayout>

And the main Class "Main.java"

                    public class Main extends Activity implements OnClickListener {

                    String name;
                    SQLiteDatabase db;
                    Cursor cursor;

                    private ProgressDialog progressDialog;

                    public ListAdapterItem list_adapter;
                    private ArrayList<HashMap<String, Object>> lst_data;
                    private HashMap<String, Object> hm;
                    private ListView listview;
                    private String list_id;
                    private int counter_id,selectedPosition;
                    private View selectedView;

                    public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.main_layout);
                        lst_data = new ArrayList<HashMap<String, Object>>();
                        listview = (ListView) findViewById(R.id.list);
                        new FetchDB().execute();
                        }
                    private class FetchDB extends AsyncTask<String, Void,ArrayList<HashMap<String, Object>>> {
                        protected void onPreExecute() {
                            progressDialog = ProgressDialog.show(Main.this,"Fetching Data", "Loading,Please wait...", true);
                        }

                        protected ArrayList<HashMap<String, Object>> doInBackground(String... lstStrings)throws IllegalArgumentException {
                            try {
                                db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
                                cursor = db.rawQuery("SELECT * FROM person WHERE Id <> ''",null);

                                if (cursor != null && cursor.getCount() > 0) {

                                    if (cursor.moveToFirst()) {
                                        do {
                                            hm = new HashMap<String, Object>();
                                            hm.put("Id",cursor.getInt(cursor.getColumnIndex("Id")));
                                            hm.put("Name", cursor.getString(cursor.getColumnIndex("Name")));
                                            lst_data.add(hm);

                                        } while (cursor.moveToNext());
                                    }// end of cursor.moveToFirst()
                                    cursor.close();
                                }

                            } catch (Exception e) {
                                e.getMessage();
                            }
                            db.close();// database close
                            return lst_data;
                        }// end of doInbackgournd

                        @Override
                        protected void onPostExecute(ArrayList<HashMap<String, Object>> result) {
                            progressDialog.dismiss();
                            list_adapter = new ListAdapterItem(Main.this,result);
                            listview.setAdapter(list_adapter);
                            listview.setOnItemClickListener(new OnItemClickListener() {

                                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                                    list_adapter.setSelectedPosition(arg2);
                                    selectedPosition = arg2;
                                    selectedView = arg1;
                                    list_id=((TextView) selectedView.findViewById(R.id.txtview_id)).getText().toString();
                                }

                            });
                        }
                    }// end of FetchDBTask
                        private class SaveTask extends AsyncTask<String, Void,ArrayList<HashMap<String, Object>>> {
                        @Override
                        protected void onPreExecute() {
                            progressDialog = ProgressDialog.show(Main.this,"Saving Data", "Loading,Please wait...", true);
                        }
                        protected ArrayList<HashMap<String, Object>> doInBackground(String... arg0)throws IllegalArgumentException {
                            counter_id++;
                            name = editext_name.getText().toString();

                            hm = new HashMap<String, Object>();
                            hm.put("Id",counter_id);
                            hm.put("Name",name);
                            lst_data.add(hm);
                            saveDB();
                            return lst_data;
                        }// end of doInbackgournd

                        protected void onPostExecute(ArrayList<HashMap<String, Object>> result) {
                            progressDialog.dismiss();
                            list_adapter = new ListAdapterItem(Main.this,result);
                            listview.setAdapter(list_adapter);
                            listview.setOnItemClickListener(new OnItemClickListener() {
                                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                                    list_adapter.setSelectedPosition(arg2);
                                    selectedPosition = arg2;
                                    selectedView = arg1;
                                    list_id=((TextView) selectedView.findViewById(R.id.txtview_id)).getText().toString();

                                }
                            });

                        }

                    }// end of saveTask
                    public void saveDB(){
                        String sql;
                        db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
                        // create table if not e exist
                        db.execSQL("CREATE TABLE IF NOT EXISTS person(Name VARCHAR ,Id INT(3));");
                        sql = "SELECT * FROM person WHERE Id="+list_id; //selected or click row in list item
                        Cursor cursor = db.rawQuery(sql, null);

                        if (cursor.moveToFirst()) {
                            String sqlUpdate = "UPDATE person SET Name=? WHERE Id="+db_id;
                            db.execSQL(sqlUpdate, new Object[] {db_name});
                            cursor.close();
                            db.close();// database close
                        } else {// empty insert
                            String sqlInsert = "INSERT INTO person VALUES (?,"+null+")";
                            db.execSQL(sqlInsert, new Object[] {db_name,db_address,db_phone,db_email,db_license,db_comments,db_company,db_policy,db_phone_insurance,vehc_year,vehc_make,vehc_model,vehc_license,vehc_vinnum,vehc_color,db_position });
                            cursor.close();
                            db.close();// database close

                        }
                    }
                    //Edit by row
                    private class EditData extends AsyncTask<String, Void,ArrayList<HashMap<String, Object>>> {
                        protected void onPreExecute() {
                            progressDialog = ProgressDialog.show(Main.this,"Saving Data", "Loading Please wait...", true);
                        }
                        protected ArrayList<HashMap<String, Object>> doInBackground(String... id)throws IllegalArgumentException {
                            name = editext_name.getText().toString();


                            hm = new HashMap<String, Object>();
                            hm.put("Id",counter_id);
                            hm.put("Name",name);

                            lst_data.set(selectedPosition, hm); //specific row update
                            list_id=Integer.parseInt(edit_counter);
                            saveDB();

                            return lst_data;
                        }// end of doInbackgournd
                        protected void onPostExecute(ArrayList<HashMap<String, Object>> result) {
                            progressDialog.dismiss();
                            list_adapter = new ListAdapterItem(Main.this,result);
                            listview.setAdapter(list_adapter);

                        }
                    }
                }

And the main_layout.xml

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"                    
                android:orientation="vertical" >

                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:background="@android:color/transparent"
                    android:cacheColorHint="#00000000"
                    android:dividerHeight="2dp"
                    android:paddingTop="8dp"
                    android:transcriptMode="normal" />

            </LinearLayout>

Specify your id every listrow in the listview hope that this answer your question

Cjames
  • 1,852
  • 1
  • 20
  • 23
  • I am currently able to edit my data. my issue is having my listview refresh after my edits are complete without having to close and restart the application. – user1165694 Feb 14 '13 at 14:32
  • You mean all rows? or each row that you've clicked and edit?.. for me I'm using hashmap to automatically replace each entries in the UI view – Cjames Feb 15 '13 at 00:55
0

I think that in your populate() method before the while block you should call loginListAdapter.notifyDataSetChanged() and loginListAdapter.clear(); That would clear the adapter and notice to it of the new list of data.

The block will look like this:

 loginListAdapter.notifyDataSetChanged();
 loginListAdapter.clear();
 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);
    }

I Just edited because the order you call the notify and clear wasn't correct. The clear must occur after the notify, is just what i got from my experience, unless the adapter wouldn't redraw the list.

MonkeyDroid
  • 637
  • 7
  • 14
0

Try this code

in public area add variable : List<String> arrayList = new ArrayList<String>();

and in onCreate() add this :

    arrayList = populateList();

//then add variable in adapter like this

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

in onResume() this code:

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

    loginArrayList.clear();

    arrayList.clear();

    arrayList = populateList();

    loginListAdapter.notifyDataSetChanged()

}

Let us know if you solved it.

AwadKab
  • 3,054
  • 2
  • 17
  • 17