从sqlite获取游标数据到字符串数组

时间:2013-03-20 03:24:29

标签: arrays string simplecursoradapter listadapter

Newbee问题: 下面的两个片段都起作用,来自字符串数组或来自SQLite表的游标。 现在,如何将表中的这些值转换为字符串数组,以便我可以操作它们,然后将它们显示为lstAdapter?
(在添加大量评论之前,我可以提供完整的资源但是14页10磅最小标题页脚。)

我在这里和其他地方尝试了很多样品。大多数产生的错误我都没想到,休息不起作用,所以我错过了一些东西。

此结构最终将成为我的WIMM One的应用程序的一部分,因此它需要是Android版本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 个答案:

答案 0 :(得分:4)

我不确定这是否是最好的方法,但你可以尝试这样的事情:

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;
}

同样作为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;
}

当然,在这个方法之后,您将拥有要根据需要操作的字符串数组(列表)。它应该适用于API 7,它不使用任何高级调用。

还有另一种选择,即创建自己的适配器,扩展SimpleCursorAdapter并覆盖getView()方法以“动态”修改字符串,但在这种情况下它并不适合我。