java - 切片 ListView

标签 java android

有人能给我一个关于如何分割 ListView 的例子吗? 我正在使用 SimpleCursorAdapter 在 ListView 中显示数据..

我的代码是这样的。

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();

       }
      });

最佳答案

我在这个 blog 上找到了分段 ListView 的一个很好的例子.它是通过使用 ArrayAdapter 完成的,但您可以看到这个概念并尝试相应地操作代码以使其与 SimpleCursorAdapter 一起工作。 希望对您有所帮助!!

关于java - 切片 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14411379/

相关文章:

java - 新 GCM 的 Android HttpsURLConnection 和 JSON

java - 找不到符号 - ArrayList;找不到符号 - 整数

java - JBoss AS/Wildfly 中的多用户数据库连接池

java - 生成java类并动态调用其方法

java - 如何重置每月条目的日期

android - 如何将Makefile改写成android.mk?

java - Hibernate 实体上的 SpEL

android - 导航到Android中的另一个 fragment 后如何清除导航堆栈

javascript - 如何动态添加 ImageButtons 和 OnClick Listeners 到 300 多个图像?

java - 如何检查哪些复选框已选中,哪些未选中?