choco
choco

Reputation: 255

populate listView from a SimpleCursorAdapter

i am trying to display SQLite database in a listview, but it dosn't work, i tried to display one row from the database in a textview and it worked so the database is correctly connected , here is the code:

public class Diction extends ListActivity  {


  Cursor cur;
  ListView list;
  private   DataBaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dictionnaire);

        dbHelper = new DataBaseHelper(this);


        list = (ListView)findViewById(R.id.dict);

         dbHelper = new DataBaseHelper(this);

         try {
                dbHelper.CopyDataBaseFromAsset();
         } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
         }
         dbHelper.openDataBase();



          cur = dbHelper.fetchAllDICT();
        startManagingCursor(cur);
         cur.moveToFirst();

         SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(
     this,
     R.layout.row_list,
     cur,
     new String[] {"arabic", "french", "english","spanish" },
     new int[] { R.id.arabic,R.id.french,R.id.english, R.id.spanish}
     ); 

        setListAdapter(dataAdapter);


        dbHelper.close();


      } }

this is the method to get data in a cursor

public Cursor fetchAllDICT() {
    db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_DICTIONNAIRE, new String[] {KEY_IID,
            KEY_ARABIC,  KEY_FRENCH, KEY_ENGLISH ,KEY_SPANISH   },
   null, null, null, null, null);



      return cursor;
     }

The activity_dictionnaire XML file

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

<ListView
    android:id="@+id/dict"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

The row_list XML activity

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

<TextView
    android:id="@+id/arabic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    />

<TextView
    android:id="@+id/french"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/arabic"
    android:layout_below="@+id/arabic"
    />

<TextView
    android:id="@+id/spanish"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/french"
    android:layout_below="@+id/french"
     />

<TextView
    android:id="@+id/english"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spanish"
    android:layout_below="@+id/spanish"
     />

</RelativeLayout>

my logCat

03-27 00:39:04.156: W/dalvikvm(373): threadid=1: thread exiting with uncaught exception (group=0x40014760)
03-27 00:39:04.349: E/AndroidRuntime(373): FATAL EXCEPTION: main
03-27 00:39:04.349: E/AndroidRuntime(373): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.guide_oran/com.example.guide_oran.Diction}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.os.Looper.loop(Looper.java:126)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ActivityThread.main(ActivityThread.java:3997)
03-27 00:39:04.349: E/AndroidRuntime(373):  at java.lang.reflect.Method.invokeNative(Native Method)
03-27 00:39:04.349: E/AndroidRuntime(373):  at java.lang.reflect.Method.invoke(Method.java:491)
03-27 00:39:04.349: E/AndroidRuntime(373):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-27 00:39:04.349: E/AndroidRuntime(373):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-27 00:39:04.349: E/AndroidRuntime(373):  at dalvik.system.NativeStart.main(Native Method)
03-27 00:39:04.349: E/AndroidRuntime(373): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
03-27 00:39:04.349: E/AndroidRuntime(373):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:227)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.Activity.setContentView(Activity.java:1777)
03-27 00:39:04.349: E/AndroidRuntime(373):  at com.example.guide_oran.Diction.onCreate(Diction.java:26)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-27 00:39:04.349: E/AndroidRuntime(373):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
03-27 00:39:04.349: E/AndroidRuntime(373):  ... 11 more

i did some changes, but it craches and the LogCat didn't change, it displayes the same messages, it is supposed to change, isn't it?

public class Diction extends Activity  {


  Cursor cur;
  ListView list;
  private   DataBaseHelper dbHelper;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dictionnaire);

        dbHelper = new DataBaseHelper(this);


        list = (ListView)findViewById(R.id.list);





         dbHelper = new DataBaseHelper(this);

         try {
                dbHelper.CopyDataBaseFromAsset();
         } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
         }
         dbHelper.openDataBase();



            cur = dbHelper.fetchAllDICT();
            startManagingCursor(cur);
            cur.moveToFirst();


 // create the adapter using the cursor pointing to the desired data
    //as well as the layout information
         SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(
     this,
     R.layout.row_list,
     cur,
     new String[] {"arabic", "french", "english","spanish" },
     new int[] { R.id.arabic,R.id.french,R.id.english, R.id.spanish},0
     ); 

  list.setAdapter(dataAdapter);

  dbHelper.close();



      }

Upvotes: 0

Views: 205

Answers (1)

codeMagic
codeMagic

Reputation: 44571

Typically when your app crashes you need to post the logcat to allow us to help you. However, here I'm pretty sure you are getting an error that says something like, "you need a ListView with the id android.R.id.list". You need to do just what it says when your Activity extends ListActivity. So the id of your ListView in your layout should be

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

You can do this or just extends Activity instead of extends ListActivity. Using ListActivity basically gives you some convenience methods which I don't see you using so you can try just changing it to extends Activity. If this doesn't solve your problem then you *need to post your logcat from the crash.

Upvotes: 1

Related Questions