从sqlite获取光标数据到字符串数组 [英] get cursor data from sqlite into string array

查看:115
本文介绍了从sqlite获取光标数据到字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Newbee问题: 下面的两个代码片段都可以从字符串数组或SQLite表中的游标开始工作. 现在,如何将这些值从表中获取到字符串数组中,以便可以对其进行操作,然后将其显示为lstAdapter?
(我可以提供完整的资源,但在添加大量评论之前,请提供14页10磅的最小标题页脚.)

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.

此结构最终将成为我的WIMM One应用程序的一部分,因此它必须是Android版本7.

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" />

推荐答案

我不确定这是否是最好的方法,但是您可以随时尝试以下方法:

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

也作为ArrayList:

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

当然,使用此方法后,您将可以根据需要操作字符串数组(列表).它应该可以在API 7上运行,并且不使用任何高级调用.

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.

还有另一种选择,那就是创建您自己的适配器,扩展SimpleCursorAdapter并重写getView()方法来即时"修改字符串,但是对于这种特殊情况,它并不吸引我.

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.

这篇关于从sqlite获取光标数据到字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆