誰かがリストビューをセクション化する方法の例を教えてもらえますか? リストビューにデータを表示するために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();
}
});