1

I tried to populate a listview from a cursor so I use this code:

Cursor curVal = db.rawQuery("SELECT * FROM category ORDER BY order_cat ASC", null);

String[] inVal = new String[] { "nume_cat", "order_cat" };
int[] outVal = new int[] { R.id.title_cat, R.id.duration  };

SimpleCursorAdapter cadapter = new SimpleCursorAdapter(this, R.layout.list_row, curVal, inVal, outVal);

ListView listView = (ListView) findViewById(R.id.mainListView);
listView.setAdapter(cadapter); 
listView.setOnItemClickListener(this); 

here are my XML`s:

List_row.xml

......................

 <TextView
    android:id="@+id/title_cat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="FILL CAT NAME HERE"
    android:textColor="#040404"
    android:typeface="sans"
    android:textSize="15dip"
    android:textStyle="bold"/>

  <TextView
    android:id="@+id/duration"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/title_cat"
    android:textColor="#343434"
    android:textSize="10dip"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="FILL DURATION HERE" />

 .....................

main.xml

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

  <TextView android:id="@+id/title_main"
        android:text="TITLE HERE"
            android:padding="15dip"
            android:textSize="18dip"
            android:textStyle="bold"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>


    <ListView android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
       android:padding="15dip"
      android:id="@+android:id/mainListView">
    </ListView>

</LinearLayout>

The problem : When I run the app nothing happens and I can't see my listview. Eclipse don't return me any error or warning in logcat...

I tryed the easy method, using ArrayAdapter and all works ok, but I need to use cursoradapter because I need to have more than 1 textview on each list item.

Here is the working ArrayAdapter:

    ArrayList<String> catList = new ArrayList<String>();
    ListView listView = (ListView) findViewById( R.id.mainListView );
    ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.list_row, R.id.title_cat, catList);
    Cursor curVal = db.rawQuery("SELECT * FROM category ORDER BY order_cat ASC", null);
        if (curVal.moveToFirst()) {
        do {
            String data = curVal.getString(curVal.getColumnIndex("nume_cat"));
            listAdapter.add(data);
        } while (curVal.moveToNext());
    } 
    listView.setAdapter(listAdapter); 
    listView.setOnItemClickListener(this);

Now lets exclude any questions:

Yes, my cursor contains an _id field.

Yes, I doublechecked my cursor and it has 20 rows.

System.out.println("Cursor adapter has " + cadapter.getCount() + " lines.");  

prints "Cursor adapter has 18 lines."

Is possible to be from:

    int[] outVal = new int[] { R.id.title_cat, R.id.duration  };

    and

    SimpleCursorAdapter cadapter = new SimpleCursorAdapter(this, R.layout.list_row, curVal, inVal, outVal);

    NEED TO REPLACE WITH ?

    int[] outVal = new int[] { android.R.id.title_cat, android.R.id.duration  };

    and

    SimpleCursorAdapter cadapter = new SimpleCursorAdapter(this, android.R.layout.list_row, curVal, inVal, outVal);

But how I define the ID as android.R.id.title_cat instead of R.id.title_cat? If I put in my xml

    <TextView
        android:id="@+android:id/title_cat"

In my class file eclipse retuns me an error "title_cat cannot be resolved or is not a field"

Deepzz
  • 4,573
  • 1
  • 28
  • 52
catalin87
  • 625
  • 6
  • 19

2 Answers2

3

I think you have problem at XMl. Check id of your listview. android:id="@+android:id/mainListView">

SathishKumar
  • 1,644
  • 1
  • 14
  • 28
2

Give id of listview as

android:id="@+android:id/list"

Then give

SimpleCursorAdapter cadapter = new SimpleCursorAdapter(this, R.layout.list_row, curVal, inVal, outVal);
setListAdapter(cadapter);

instead of

ListView listView = (ListView) findViewById(R.id.mainListView);
listView.setAdapter(cadapter); 

Also your class should extend ListActivity

Deepzz
  • 4,573
  • 1
  • 28
  • 52