get cursor data from sqlite into string array

时光怂恿深爱的人放手 提交于 2019-12-10 12:19:19

问题


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:


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.



来源:https://stackoverflow.com/questions/15514724/get-cursor-data-from-sqlite-into-string-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!