0

Newbee question: Both snippets below work, either from string array or cursor from SQLite table. Now, how do I get these values from the table into a string array so I can manipulate them, then display them as a lstAdapter?
(I can provide the complete source but 14 pages 10 pt. minimal heading footer before lots of comments added).

I have tried lots of samples from here and other places. Most generate errors I haven't figured out, rest don't work, so I am missing something.

This structure will eventually be part of an app for my WIMM One, so it needs to be Android version 7.

//From my ...ListFragment:
//This works; I get "First", "Second", etc. listed on my screen:
//-----------------------
private SimpleCursorAdapter mAdapter;
private ListAdapter lstAdapter;
. . . 
    Log.d("RemindLF","S onCrt: NEW");
// an array of string items to be displayed        
    String[] strLstitems = new String[] 
             {"first", "Second", "Third", "Fourth"};
//
    lstAdapter = new ArrayAdapter<String>(getActivity(),
            R.layout.reminder_row, R.id.text1, strLstitems);
//
// Call to SetListAdapter()informs ListFragment how to fill ListView
    setListAdapter(lstAdapter);
    getLoaderManager().initLoader(0, null, this);
    Log.d("RemindLF","X onCrt:" + strLstitems[0]);



//This also works; I get the values of one column from each record listed on my screen
//--------------------------
private ArrayAdapter aryAdapter;
. . . 
    Log.d("RemindLF","X onCrt: BASE" ); 
    String[]  strItems =  new String[]  
      { 
       ReminderProvider.COLUMN_BODY
      };
    Log.d("RemindLF","X onCrt:strI:" + strItems[0]);          
    int[] iCnt = new int[]
      {  
       R.id.text1
  };

    mAdapter =  new SimpleCursorAdapter(getActivity(),R.layout.reminder_row,
                null, strItems, iCnt, 0);

    setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);



//Other pieces
//------------
//Loader set-up:
@Override
public Loader<Cursor> onCreateLoader(int ignored, final Bundle args)
   {
    Log.d("RemindLF","S LoadCSR");
    return new CursorLoader(getActivity(), ReminderProvider.CONTENT_URI, null, null, null, null);
   }
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
   {
    Log.d("RemindLF","S LoadFin");
    mAdapter.swapCursor(cursor);
   }


REMINDER_LIST.XM:L
<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.dummies.android.taskreminder.ReminderListFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />


REMINDER_ROW.XML:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:padding="10dip" />

1 Answer 1

4

I'm not sure if it's the best method, but you can always try something like this:

private String[] fromCursorToStringArray(Cursor c){
    String[] result = new String[c.getCount()];
    c.moveToFirst();
    for(int i = 0; i < c.getCount(); i++){
        String row = c.getString(c.getColumnIndex(ReminderProvider.COLUMN_BODY));
        //You can here manipulate a single string as you please
        result[i] = row;
        c.moveToNext();
    }
    return result;
}

Also as an ArrayList:

private ArrayList<String> fromCursorToArrayListString(Cursor c){
    ArrayList<String> result = new ArrayList<String>();
    c.moveToFirst();
    for(int i = 0; i < c.getCount(); i++){
        String row = c.getString(c.getColumnIndex(ReminderProvider.COLUMN_BODY));
        //You can here manipulate a single string as you please
        result.add(row);     
        c.moveToNext();
    }
    return result;
}

Of course, after this methods you will have the array(list) of strings to manipulate as you want. It should work on API 7, it doesn't use any advanced call.

There is another option, which would be to create your own adapter, extending the SimpleCursorAdapter and override the getView() method to modify the strings "on the fly", but it doesn't appeal to me for this particular situation.

Sign up to request clarification or add additional context in comments.

1 Comment

Appreciate the reply. I ended up doing basically that. My problem was that the sample from the book used simplecursoradapter which was done in the onCreate method. So I tried sample after sample after sample from this and other sites to access the cursor there. Anything I did generated various errors because my cursor was undefined. I finally figured I needed to do it in the onLoadFinished, and it all worked. Many thanks, Clark

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.