Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 剖切列表视图_Java_Android - Fatal编程技术网

Java 剖切列表视图

Java 剖切列表视图,java,android,Java,Android,有人能给我一个关于如何划分listview的例子吗? im使用SimpleCursorAdapter在listview中显示数据 我的代码是这样的 private WordDbAdapter dbHelper; private SimpleCursorAdapter dataAdapter; 这是onCreate()方法的代码 这是onCreate方法之外的代码 dbHelper = new WordDbAdapter(this); dbHelper.open();

有人能给我一个关于如何划分listview的例子吗? im使用SimpleCursorAdapter在listview中显示数据

我的代码是这样的

private WordDbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
这是onCreate()方法的代码

这是onCreate方法之外的代码

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

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

    //Generate ListView from SQLite Database
    displayListView();
@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();

       }
      });
@SuppressWarnings(“弃用”)
私有void displayListView(){
Cursor Cursor=dbHelper.fetchAllWords();
//要绑定的所需列
字符串[]列=新字符串[]{
WordDbAdapter.KEY\u单词,
};
//数据将绑定到的XML定义的视图
int[]至=新int[]{
R.id.Word,
};
//使用指向所需数据的光标创建适配器
//以及布局信息
dataAdapter=新的SimpleCorsorAdapter(
这个,R.layout.word\u信息,
光标,
柱,
到
);
ListView ListView=(ListView)findViewById(R.id.Diclist);
//将适配器分配给ListView
setAdapter(dataAdapter);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView列表视图、视图、,
内部位置,长id){
//获取光标,定位到结果集中的对应行
游标游标=(游标)listView.getItemAtPosition(位置);
//从数据库中的此行获取单词名称。
选定的字符串字=
cursor.getString(cursor.getColumnIndexOrThrow(“word”);
字串音节化=
cursor.getString(cursor.getColumnIndexOrThrow(“音节化”);
字符串字部分语音=
getString(cursor.getColumnIndexOrThrow(“partofspeech”);
字符串词义=
cursor.getString(cursor.getColumnIndexOrThrow(“含义”);
EditText TextDic=(EditText)findViewById(R.id.TextDic);
TextDic.setText(已选择字);
Toast.makeText(getApplicationContext(),
WordTypelication+“\n”+wordPartofSpeech+“\n”+词义,Toast.LENGTH\u SHORT.show();
}
});

我在这方面找到了一个很好的分段列表视图示例。它是通过使用ArrayAdapter完成的,但您可以看到这个概念,并尝试相应地操作代码,使其与SimpleCorsorAdapter一起工作。 我希望这会有帮助!!

你可以试试这个

看看ListView中的Android部分,它很好地描述了如何在ListView中实现该部分


谢谢。

这对于光标适配器来说很棘手,因为你必须重新映射你的位置。我制作了一个名为的库来帮助你实现这一点。它非常容易实现。要让它与你的数据一起工作,当你扩展CursorAdapter部分时,只需执行以下操作

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

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

将列表视图分段是什么意思。?我喜欢Jeff Sharkey的SeparatedListAdapter:)@Misspressure:我很高兴帮助您。如果您觉得它有帮助,请投我一票或者接受作为答案。谢谢!