0

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

我已经尝试了很多来自这里和其他地方的样品。大多数会产生我没有弄清楚的错误,休息不起作用,所以我错过了一些东西。

这种结构最终将成为我的 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" />
4

1 回答 1

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() 方法以“即时”修改字符串,但对于这种特殊情况,它对我没有吸引力。

于 2013-03-22T13:17:59.467 回答