剖面列表视图

时间:2013-01-19 05:22:38

标签: java android

有人可以给我一个关于如何划分列表视图的例子吗? 我使用SimpleCursorAdapter在列表视图中显示数据..

我的代码是这样的。

private WordDbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;

这是onCreate()方法的代码。

    dbHelper = new WordDbAdapter(this);
    dbHelper.open();

    //Clean all data
    dbHelper.deleteAllWords();
    //Add some data
    dbHelper.insertSomeWords();

    //Generate ListView from SQLite Database
    displayListView();

这是onCreate方法之外的代码。

@SuppressWarnings("deprecation")
private void displayListView() {


  Cursor cursor = dbHelper.fetchAllWords();

  // The desired columns to be bound
  String[] columns = new String[] {
          WordDbAdapter.KEY_WORD,

  };

  // the XML defined views which the data will be bound to
  int[] to = new int[] {

    R.id.Word,

  };

  // create the adapter using the cursor pointing to the desired data
  //as well as the layout information
  dataAdapter = new SimpleCursorAdapter(
    this, R.layout.word_info,
    cursor,
    columns,
    to
    );

  ListView listView = (ListView) findViewById(R.id.Diclist);
  // Assign adapter to ListView
  listView.setAdapter(dataAdapter);


  listView.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> listView, View view,
     int position, long id) {
   // Get the cursor, positioned to the corresponding row in the result set
   Cursor cursor = (Cursor) listView.getItemAtPosition(position);


   // Get the word name from this row in the database.
   String wordSelected =
               cursor.getString(cursor.getColumnIndexOrThrow("word"));

       String wordSyllabication =
               cursor.getString(cursor.getColumnIndexOrThrow("syllabication"));
       String wordPartofSpeech =
               cursor.getString(cursor.getColumnIndexOrThrow("partofspeech"));
       String wordMeaning =
               cursor.getString(cursor.getColumnIndexOrThrow("meaning"));

       EditText TextDic = (EditText) findViewById(R.id.TextDic);
       TextDic.setText(wordSelected);


       Toast.makeText(getApplicationContext(),
               wordSyllabication + "\n" + wordPartofSpeech + "\n" + wordMeaning , Toast.LENGTH_SHORT).show();

       }
      });

3 个答案:

答案 0 :(得分:3)

你可以试试这个。

查看ListViews中的Android 部分示例,它很好地描述了如何在ListViews中实现Section。

android-amazing-listview

Jeff Sharkey's SeparatedListAdapter

MergeAdapter by CommonsWare

http://w2davids.wordpress.com/android-sectioned-headers-in-listviews/

感谢。

答案 1 :(得分:0)

我在这个blog上找到了一个很好的分段列表视图示例。它是通过使用ArrayAdapter完成的,但你可以看到这个概念,并尝试相应地操作代码,使其适用于SimpleCursorAdapter。 我希望它会有所帮助!!

答案 2 :(得分:0)

使用光标适配器这很复杂,因为您必须重新映射您的位置。我创建了一个名为SectionCursorAdapter的库来帮助解决这个问题。这很容易实现。要使其与您的数据一起使用,当您扩展SectionCursorAdapter时,只需执行以下操作。

@Override
protected Object getSectionFromCursor(Cursor cursor) {
    int columnIndex = cursor.getColumnIndex("word");
    String word = cursor.getString(columnIndex);
    return word.toUpperCase().substring(0, 1);
}

现在您不必担心回收视图,但您必须自己绑定数据。如果这个库足够流行,我将添加一个SimpleSectionCursorAdapter。