Android ListView with SQLite

Posted by soclose on Stack Overflow See other posts from Stack Overflow or by soclose
Published on 2010-04-24T07:46:51Z Indexed on 2010/04/24 7:53 UTC
Read the original article Hit count: 1949

Filed under:
|
|

Hi

I'd like to refresh the Listview items. These items are populated from SQLite database. My code is below

public class Weeve extends Activity {
      private String[] lv_arr;
    protected ListView CView;
    private DBAdapter mDbHelper;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

   mDbHelper = new DBAdapter(this);
    mDbHelper.open();

    Cursor c = mDbHelper.getAll();
    if (c.getCount() > 0)
    {if (c.moveToFirst())
    {
        ArrayList strings = new ArrayList();
       do {                    
          String mC = c.getString(0);
          strings.add(mC);  

       } while (c.moveToNext());
       lv_arr = (String[]) strings.toArray(new String[strings.size()]);
    }
    }  
    else Toast.makeText(this, 
           "No more Records", 
           Toast.LENGTH_SHORT).show();
    c.close();

    ListView CView = new ListView(this);
    CView.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, lv_arr));      
    setContentView(CView);}}

I'd like to make refreshing this list view after adding, updating or deleting SQLite table. These operations are called by content or option menu. I tried to create these code into a separated function and call it after every operation. But can't. I think setContentView(CView) statement.

I also tried to use SimpleCursorAdapter like notepad sample from Android.com. I got Thread error. Help me.

© Stack Overflow or respective owner

Related posts about android

Related posts about listview