【问题标题】:How can I send cursor's values to an string array[]如何将光标的值发送到字符串数组 []
【发布时间】:2013-01-05 16:14:17
【问题描述】:

正如我在标题中所说,我想将光标的值保存到一个字符串数组中。我将在 ArrayAdapter 中使用此数组并调用 setLineAdapter(ArrayAdapter).. 我有这些代码,但 LogCat 说 arr[i] = crr.getString(i) 行有问题...可以来人帮帮我?

DBAdapter db;
String arr[];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    db = new DBAdapter(this);
    db.open();

    ArrayAdapter<String> AA = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arr);

    try {
          Cursor crr = db.getRecord(4);

          crr.moveToFirst();

          for (int i = 0; i <= cr.getCount(); i++){ 
            arr[i] = cr.getString(i);
            crr.moveToNext();
    }}
    catch (IOException e) {e.printStackTrace();}

    setListAdapter(AA);
    db.close();

【问题讨论】:

  • 可能像 String arr[]=new String[cr.getCount()];

标签: android listview cursor android-arrayadapter


【解决方案1】:

将代码更改为使用 do-while 并使用 ArrayList 而不是 Array 从光标填充动态值:

 ArrayList<String> arrcurval=new ArrayList<String>();
if (crr.moveToFirst()) {
   do {
       arrcurval.add(crr.getString(0)); //<< pass column index here instead of i

     } while (crr.moveToNext());
}

【讨论】:

  • 谢谢...现在一切正常 =)
  • 现在,当我单击项目时,如何执行打开 TableView 的 onListItemListener?我不能用 if position is x, start intent 我想......因为我不知道会有多少项目......监听器应该打开 TableView 并且还应该发送一个整数(索引) 到 TableView 来说明选择了哪个项目。 TableView 应该从DataBase 中获取数据,并显示出来;使用监听器发送的索引?但是,我不知道我该怎么做......(或者我错了吗?)
  • @ArdaOğulÜçpınar :要完成当前任务,您需要为 ListView 设置 setOnItemClickListener 并完成您的工作。如果您有任何问题,请尝试,然后为新问题提出新问题。谢谢
【解决方案2】:

你必须分配数组,因此:

      Cursor crr = db.getRecord(4);
      int n = crr.count();
      arr = new int[n];

【讨论】:

  • 您可能还想替换 cr.getString(i) 以从正确的列中获取字符串,cr.getString(cr.getColumnIndex(THE_INDEX)) :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多