2

I a developing an app whereby i retrieve data from an sqlite database and the data is displayed on a listview I am retrieving data from two columns of a specific table and it is added to a SimpleCursorAdapter which is then set to the listview i would like to display each record in the different columns to be displayed on its own row currently data from both columns is displayed in one row any help will be appreciated especially a link to a working example

i have deicded to add my code maybe this will help you help me even better

mDicCursor=managedQuery(DictionaryProvider.CONTENT_URI, null, null,
              new String[] {query}, null);

      if (mDicCursor == null ) {
          mTextView.setText(getString(R.string.no_results, new Object[] {query}));
    /*    final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom_dialog);
            dialog.setTitle("WORD MISSING");*/
            showDialog(DIALOG_WORDMISSING);


       //new AlertDialog.Builder(this).setTitle("Argh").setMessage("Watch out!").setNeutralButton("Close", null).show();  
      } else {
          int count = mDicCursor.getCount();
          String countString = getResources().getQuantityString(R.plurals.search_results,
                                  count, new Object[] {count, query});
          mTextView.setText(countString);


/*  int  iWord = mDicCursor.getColumnIndexOrThrow(DictDatabase.REC_DESCRIPTION);
    mTextView.setText(mDicCursor.getString(iWord));*/
     // Specify the columns we want to display in the result

    String[] from = new String[]{ DictDatabase.REC_WORD,
            DictDatabase.REC_DESCRIPTION   };




       // and an array of the fields we want to bind those fields to (in this case just text1)
     int[] to = new int[]{ R.id.text2,
                           R.id.text3};
      SimpleCursorAdapter words = 
         new SimpleCursorAdapter(this, R.layout.dictwords, mDicCursor, from, to);

      mListView.setAdapter(words);
4

2 回答 2

0

在光标适配器中,您可以详细说明数据是如何设置并在屏幕上显示的。您需要确保每第二行都来自第二列。这可以使用当前行索引的模数来完成

boolean isEvenRow = (currentRow % 2==0); // will give 0 for even rows, 1 for odd rows

然后您可以使用此布尔结果来选择数据是来自 A 列还是 B 列。

String data;
if (isEvenRow){
 data = mCursor.getString(COLUMN_A);
}else{
 data = mCursor.getString(COLUMN_B);
}

然后您还需要确保总行数将是游标大小的两倍,因此当您返回计数时,请确保返回正确的值:

return mCursor.getCount()*2;

这将允许您滚动两倍的光标长度。

于 2012-07-18T10:35:37.267 回答
0

创建另一个包含 2 个文本视图的布局并将它们彼此相邻放置。在您的活动“R.layout.new_layout”中,并在您的 simplecursoradapter 的构造函数中使用它和您的新 2 textviews id.m 示例:

SimpleCursorAdapter simple=new SimpleCursorAdapter(this, R.layout.new_layout,cursor, new String[]{column1,column2} , new int[] {textview1,textview2});
于 2012-07-18T10:39:23.610 回答