OnItemClick 后在 ListView 中显示数据库中的光标结果
我有一个活动,我希望它使用 ListView 显示数据库的结果。该表具有三列:单词、描述和类别。在活动页面上,我有一个从数组中读取的类别列表。我想这样设置,如果您单击列表中的某个项目(例如 Cat1),从光标返回的结果将是数据库中类别 Cat1 的所有单词/描述。目前,我只是在单击时显示一个带有该类别名称的 Toast。
就目前而言,该活动无法正常运行。我在网上阅读了相关内容,但不知道如何继续。这是我到目前为止的代码。如果有人可以帮助我,我将非常感激。
公共类类别扩展ListActivity { DataBaseHelper db = new DataBaseHelper(this); 私有 SQLiteDatabase 数据; int 位置;
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();
}
});
}
}
I have an activity where I want it to display results from a database using ListView. The table has three columns: word, description, and category. On the activity page, I have a list of categories being read from an array. I want to set it up so that if you click on an item on the list (for example Cat1), the results returned from the cursor will be all words/descriptions with the category Cat1 in the DB. Currently I simply have a Toast with that category's name appear when clicked.
As it is right now, the activity does not run properly. I have read around on the web, and am not sure how to proceed. Here is the code I have so far. If anyone can help me, I would really appreciate it.
public class Categories extends ListActivity {
DataBaseHelper db = new DataBaseHelper(this);
private SQLiteDatabase data;
int position;
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();
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的应用程序中有类似的功能。您将需要创建一个 XML 文件来定义列表的布局并在 java 代码中调用它们。
下面是示例 XML:
LIST:
这是自定义布局 XML。我称之为 list_rows:
这是 JAVA 代码:
onListItemClickListener
代码的新部分所做的只是通过意图将所选项目发送到另一个活动。然后在新活动中,我使用包中选定的项目名称查询数据库并显示结果。
请询问您是否需要进一步解释。
I have a similar function in my app. You will need to create a XML file to define the layout for the list and call them in your java code.
Here is the example XML:
LIST:
This is the custom layout XML. I call it list_rows:
And this is the JAVA code:
The onListItemClickListener
What the new part of the code is doing is just sending the selected item to another activity through an intent. Then in the new activity, I am querying the DB using the selected item name from the bundle and displaying the result.
Do ask if you need further explanation.
我正在制作一个非常相似的程序。我在使用它时遇到了麻烦,因为它是我的第一个 android/java 程序。我得到的帮助来自网络和我教java的继父。
在帮助我设置数据库时,他解释说要设置我的表格,以便类别与其下的项目配对。因此,您将拥有一个类别表,其中包括将为该类别显示的标题以及主键。然后,您需要一个类别下一级的表格,例如标题。这将包括标题和主键的文本。接下来,您需要制作另一张桌子来将它们配对。标题键、类别键和主键。
从那里您需要告诉 java 根据最后一个表将它们配对。
我是一个菜鸟,很抱歉我无法提供更多/更好的信息。但我在这个问题上发现的任何内容我都会发布在这里,祝你好运。
I am making a very similar program. I'm having trouble with it as its my first program for android/java. What help I have got is from web and my step father who use to teach java.
When helping me setup a database he explained to setup my tables so categories is paired with items under it. So you would have a table for your categories including the title you will display for that category as well as your primary key. Then you need a table for the next level down from category, say title. This would include a text for title and primary key. Next you need to make another table to pair them up. A title key, category key, and primary key.
From there you will need to tell java to pair them up based off that last table.
I'm a noob so sorry I couldn't give more/better info. But anything I find on this issue I'll post here and good luck.