views:

602

answers:

3

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.

A: 

You can do that using Adapter.notifyDataSetChanged().

However, I find your code slightly off. You're fetching rows from the database using a cursor, just to create a new ArrayAdapter for the row values. Why not use a CursorAdapter to back your list? That's what it's meant for.

Matthias
may i get a sample SimpleCursorAdapter code for my Activity?
A: 

Actually, I need to make this listview refresh after finishing from another activity. I think I should use either onResume or onStart event to do refresh. Right?

My UI layout looks like Android built-in contact. I use tab activity. my code is below

public class M_G extends TabActivity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, Weeve.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("w_m_g").setIndicator("Weeve G",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);

....
    tabHost.setCurrentTab(0);}}

main.xml

<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp" />          
</LinearLayout>

Using SampleCursorAdapter, I modified my Weeve class like below - based on Notepad Tutorial idea.

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();
fillData();}

private void fillData(){
    Cursor c = mDbHelper.getAllName();
startManagingCursor(c);

String[] from = new String[] {DBAdapter.WName};
int[] to = new int[] {R.id.txtTabRow};

SimpleCursorAdapter wc = new SimpleCursorAdapter(this,R.layout.tab_row,c,from,to);
    setListAdapter(wc);}}

tab_list.xml

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

<ListView android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<TextView android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/no_wlc"/>

tab_row.xml

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txtTabRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

I can't see my listview. I got this error below

ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2401 Source not found.

why? how to solve!

A: 

make sure all your activities are in the manifest

Ryan
If we use SimpleCursorAdapter, we must add primary key column '_id'. I got the error is caused of this column name '_id'.