Calculating the db data will then enumerate it using either SimpleCursorAdapter or ArrayAdapter
I just started programming in android a few weeks ago, so I'm not really sure how to deal with the listing values. Please help me! I have some questions regarding displaying datasets from db in a list. Currently I have a pointer returned by my db items to a list of strings and I want to display 2 column values in one row of the list. The xml line looks like this:
<TextView android:id="@+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/text2"
android:textSize="14sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
so I thought about using a simplecursoradapter, which supposedly makes my life easier by displaying data in a list. However, this is only true if I want to display raw data. For my program, I need to do some calculations on raw datasets and then display them. I'm not sure how to do this with SimpleCursorAdapter. Here's how I display the raw data:
String[] from = new String[]{BtDbAdapter.KEY_EX_TYPE,BtDbAdapter.KEY_EX_TIMESTAMP};
int[] to = new int[]{R.id.text1, R.id.text2};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter records =
new SimpleCursorAdapter(this, R.layout.exset_row, mExsetCursor, from, to);
setListAdapter(records);
Is there a way to do the computation of the data in these rows before I link it to the SimpleCursorAdapter? I tried to use an alternative way to do it using arraylist and arrayadapter, but this way I don't know how to achieve displaying two elements on the same line.
This is my code for using arrayadapter which only displays 1 text per line instead of 2 text views per line:
//fill in the array
timestamp_arr = new ArrayList<String>();
type_arr = new ArrayList<String>();
fillRecord();
Log.d(TAG,"setting now in recordlist");
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,timestamp_arr));
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2,type_arr));
It is very obvious that it only displays one text image per line, because I am setting the second array, overwriting the first! I tried to use R.id.text1 and R.id.text2 for them, but it gave me some errors saying 04-23 01: 40: 58.658: ERROR / AndroidRuntime (3309): android.content.res.Resources $ NotFoundException : resource id # 0x7f070008 type # 0x12 invalid
I believe the second way can do it, but I'm not sure how to handle layout issues, so if you have any suggestions please let me know. Thanks!!
a source to share
SimpleCursorAdapter
is what it is, errr simple. More complex requirements require different adapters. In your case, switch to ResourceCursorAdapter
and overwrite bindView()
. Call view.findViewById(R.id.text1)
, retrieve data from the cursor, make changes, and set the text to the view.
I would not switch to ArrayAdapter
in your case. For instance. you can easily update your cursor adapter, it will consume less resources and scale better.
a source to share