On 4/1/2012 3:19 PM, Ricardo Santos wrote:
Hi Ted,
Maybe I have understood the theory, now, but my code still do not
work. So, my code looks like this:
//This is my list view
ListView charList = (ListView)findViewById(R.id.list);
R.id.list needs to be the id of a ListView element...
//Setting up the from String[]
String[] from = new String[]{"name"};
//Setting up the to int[]
int[] to = new int[]{R.id.lstCharacters};
//Now, I set the SimpleCursorAdapter
SimpleCursorAdapter simplCursor = new SimpleCursorAdapter(this,
R.id.list, loadCharacterCursor, from, to);
the R.id.list should not be a ListView, it should be resource
identifier of a layout file that defines the views for each row in the
list.
//Setting the adapter do the list - here it crashes.
charList.setAdapter(simplCursor);
I cant see what I'm doing wrong. Can anyone shed (some) more light to
the subject?
I've never used a list view outside of a class extending ListActivity.
Ricardo
On Sun, Apr 1, 2012 at 12:33 AM, Ted Scott <[email protected]
<mailto:[email protected]>> wrote:
Hi Ricardo,
First, I'm sure others will chime in if this is bad advice, but my
understanding is that it is better to use the compatibility
package and cursor loaders and a load manager. See
http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
and the base CursorAdapter.
That said, from is simply a list of columns in the PROJECTION
exposed by the cursor that are of interest to the ListView ids in
the to array. This is in order of the two arrays such that the
data from from[x] is passed to the view with the id in to[x].
Given the list view row definition:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="34dp"
android:gravity="bottom"
android:orientation="horizontal">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/foo"
android:layout_width="100dp"
android:layout_height="30dp"
android:textSize="16dp"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:singleLine="true"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bar"
android:layout_width="50dp"
android:layout_height="30dp"
android:textSize="20dp"
android:gravity="right|center_vertical"
android:paddingLeft="5dip"
android:singleLine="true"
/>
</LinearLayout>
And assuming that you have column1 and column2 in your cursor's
projection, and want to map the contents of column1 to the
textview foo and column2 to textview bar, from and to would be
defined something like:
String[] from = new String[] { "column1" , "coulmn2"};
int[] to = new int[] { R.id.foo , R.id.bar };
Clear as mud?
-Ted
On 3/31/2012 9:36 PM, Ricardo Santos wrote:
Hello everyone!
I have in my application, I hace a list view that needs to be
populated with data from a database, I have tested my database
and my app is handling the data correctly, but I cant populate
the list view, I have read about the SimpleCursorAdapter but I
cant understand how to use the class, specially the from and
to fields, does anyone know how to use it?
My class is not extending ListActivity, my list view is
inside a dialog.
Ricardo
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to
[email protected]
<mailto:[email protected]>
To unsubscribe from this group, send email to
[email protected]
<mailto:android-developers%[email protected]>
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to
[email protected]
<mailto:[email protected]>
To unsubscribe from this group, send email to
[email protected]
<mailto:android-developers%[email protected]>
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en