我有一个 Activity ,我希望它使用 ListView 显示来自数据库的结果。该表包含三列:词、描述和类别。在 Activity 页面上,我有一个从数组中读取的类别列表。我想将其设置为如果您单击列表中的某个项目(例如 Cat1),则从光标返回的结果将是数据库中类别为 Cat1 的所有单词/描述。目前,我只有一个 Toast,在单击时会显示该类别的名称。
就目前而言, Activity 无法正常运行。我已在网上阅读,但不确定如何进行。这是我到目前为止的代码。如果有人能帮助我,我将不胜感激。
公共(public)类类别扩展 ListActivity { DataBaseHelper db = new DataBaseHelper(this); 私有(private) SQLiteDatabase 数据; 内部位置;
private ListView list;
private String[] categories = {
"C1", "C2", "C3", "C4",
"C5", "C6", "C7", "C8",
"C9", "C10", "C11", "C12"
};
@Override
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.cat_list);
list = (ListView)findViewById(R.id.cat_listing);
list.setAdapter(new ArrayAdapter<String>(this,
R.layout.categories, categories));
list.setTextFilterEnabled(true);
Cursor cursor = data.rawQuery("SELECT term, desc FROM words WHERE cat = '" + categories[position] + "'", null);
startManagingCursor(cursor);
String columns[] = new String[] { "term", "desc" };
int[] to = new int[] { R.id.cat_term, R.id.cat_desc };
SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.cat_result, cursor, columns, to);
this.setListAdapter(myAdapter);
list.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id){
CharSequence text = categories[position];
Toast toast = Toast.makeText(getApplicationContext(),
text, Toast.LENGTH_SHORT);
toast.show();
}
});
}
最佳答案
我的应用程序中有类似的功能。您将需要创建一个 XML 文件来定义列表的布局并在您的 Java 代码中调用它们。
这是示例 XML:
列表:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>
<TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/no_projects"
android:padding="10dp"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
</LinearLayout>
这是自定义布局 XML。我称之为 list_rows:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/text3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
这是JAVA代码:
String[] fields = new String[] { db.TABLE_PRJ_NAME, db.TABLE_PRJ_TYPE, db.TABLE_PRJ_DESC };
int[] views = new int[] { /*android.R.id.text1*/ R.id.text1, R.id.text2, R.id.text3 };
c = db.getAllProjects();
startManagingCursor(c);
// Set the ListView
SimpleCursorAdapter prjName = new SimpleCursorAdapter(
this,
R.layout.project_list_rows,
//android.R.layout.simple_list_item_1,
c, fields, views);
setListAdapter(prjName);
onListItemClickListener
// This section of code is for handling the Click Event of the Projects List
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor o = (Cursor) this.getListAdapter().getItem(position);
String projectName = o.getString(1);
Intent showProjectDetails = new Intent(this, ProjectDetails.class);
Bundle bundle = new Bundle();
bundle.putString("sendProjectName", projectName);
showProjectDetails.putExtras(bundle);
startActivity(showProjectDetails);
}
代码的新部分所做的只是通过 Intent 将所选项目发送到另一个 Activity 。然后在新 Activity 中,我使用从 bundle 中选择的项目名称查询数据库并显示结果。
请询问您是否需要进一步的解释。
关于android - 在 OnItemClick 之后在 ListView 中显示来自数据库的游标结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4413054/