Computation on db data then list them using either SimpleCursorAdapter or ArrayAdapter

by kc2uno_CMU » Fri, 23 Apr 2010 13:53:48 GMT


Sponsored Links
 Hi all,

I juststarted programming in android a few weeks ago, so I am not
entirely sure how to deal with listing values. Please help me out!
I have some questions regarding displaying data sets from db in a
list. Currently I have a cursor returned by my db points to a list of
rows and I want display 2 columns values in a single row of the list.
The row xml 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 was thinking using simplecursoradapter which supposedly makes my
life easier by displaying the data in a list. However that is only
true if I want to display the raw data.
For the purpose of my program I need to do some computations on the
raw data sets, then display them. I am not sure how to do that using
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 computation on the data in those rows before I
bind it with the SimpleCursorAdapter? I was trying to use an
alternative way of doing this by using arraylist and arrayadapter, but
that way I dont know to how achieve displaying 2 items in a single
row.

This is my code for using arrayadapter which only display 1 text in a
row instead of 2 textviews in a row:

      //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's very obvious that it only displays one textview in a row because
I set the second arrayadapter overwrites the first one! I was trying
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 is not valid

I believe the second method can achieve this, but I'm not sure how do
deal with the layout problems, so if you any suggestions, please post
them out. Thank you!!

--



Computation on db data then list them using either SimpleCursorAdapter or ArrayAdapter

by skink » Fri, 23 Apr 2010 15:49:37 GMT


 


use 
 http://developer.android.com/intl/fr/reference/android/widget/ResourceCursorAdapter.html 
and override bindView method

pskink

--


Sponsored Links


Other Threads

1. Timer not available

Hello,

I came to know about Timer in android. I read
http://android-developers.blogspot.com/2007/11/stitch-in-time.html
that Timer is not available at Runtime.
--~--~---------~--~----~------------~-------~--~----~

2. ScrollView not resizing when a a child ListView is populated

Hi,

I have a ListView which is embedded in a ScrollView. I populate the
rest of the Views in the ScrollView, then populate the ListView.
Unfortunately yhe ScrollView doesn't size itself to accomodate the
items in the ListView. Am I missing something? Is there a way to get
the View to resize itself to accomodate the contents of the ListView?

Thanks
S

Here is the View in question :

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
        xmlns:admob="http://schemas.android.com/apk/res/
com.imkon.android.jiradroid"
        android:layout_width="wrap_content"
android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.admob.android.ads.AdView android:id="@+id/ad"
                android:layout_width="fill_parent"
android:layout_height="wrap_content"
                admob:backgroundColor="#000000" admob:textColor="#FFFFFF"
                admob:keywords="Android application" 
admob:refreshInterval="60"/>

        <ScrollView xmlns:android="http://schemas.android.com/apk/res/
android"
                android:layout_width="fill_parent"
android:layout_height="wrap_content" android:paddingLeft="1mm">

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
                        android:layout_width="wrap_content"
android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/
android"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView android:id="@+id/iProject"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:text="@string/
project_label" />

                                <TextView android:id="@+id/iProjectT"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:textSize="20.0sp" />

                        </LinearLayout>



                        <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/
android"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView android:id="@+id/iKey"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:text="@string/
key_label" />

                                <TextView android:id="@+id/iKeyT"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:textSize="20.0sp" />

                        </LinearLayout>

                        <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/
android"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView android:id="@+id/iPriority"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:text="@string/
priority_label" />

                                <TextView android:id="@+id/iPriorityT"
                                        android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                        android:textSize="20.0sp" />

                        </LinearLayout>

                        <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/
android"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView android:id="@+id/iStatus"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:text="@string/
status_label" />

                                <TextView android:id="@+id/iStatusT"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:textSize="20.0sp" />

                        </LinearLayout>

                        <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/
android"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView android:id="@+id/iResolution"
                                        android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                        android:text="@string/resolution_label" 
/>

                                <TextView android:id="@+id/iResolutionT"
                                        android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                        android:textSize="20.0sp" />

                        </LinearLayout>

                        <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/
android"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView android:id="@+id/iCreated"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:text="@string/
created_label" />

                                <TextView android:id="@+id/iCreatedT"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:textSize="20.0sp" />

                        </LinearLayout>

                        <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/
android"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView android:id="@+id/iUpdated"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:text="@string/
updated_label" />

                                <TextView android:id="@+id/iUpdatedT"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:textSize="20.0sp" />

                        </LinearLayout>

                        <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/
android"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView android:id="@+id/iDuedate"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:text="@string/
duedate_label" />

                                <TextView android:id="@+id/iDuedateT"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:textSize="20.0sp" />

                        </LinearLayout>

                        <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/
android"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView android:id="@+id/iVotes"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:text="@string/
votes_label" />

                                <TextView android:id="@+id/iVotesT"
android:layout_width="wrap_content"
                                        android:layout_height="wrap_content" 
android:textSize="20.0sp" />

                        </LinearLayout>

                        <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/
android"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView android:id="@+id/iEnvironment"
                                        android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                        
android:text="@string/environment_label" />

                                <TextView android:id="@+id/iEnvironmentT"
                                        android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                        android:textSize="20.0sp" />

                        </LinearLayout>

                        <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/
android"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView android:id="@+id/iComments"
                                        android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                        android:text="@string/comments_label" />

                                <ListView
                                        android:id="@android:id/list"
                                        android:layout_width="fill_parent"
                                        android:layout_height="fill_parent"
                                        android:scrollbars="none"
                                />

                        </LinearLayout>

                </LinearLayout>

        </ScrollView>

</LinearLayout>

---

Here is the element view used to render the items in the list :

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


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

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

        <TextView
        android:id="@+id/cBody"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20.0sp"
        />

</LinearLayout>

--~--~---------~--~----~------------~-------~--~----~

3. Android plugin download externally

4. Question on Seekbar - clipping of thumb

5. Can we develop application for CECT G1(Clone g1)?

6. 鍥炲: Orienta tion for Dialog

7. PORTING android to my smdk2440 board