OnItemClick 后在 ListView 中显示数据库中的光标结果

发布于 2024-10-06 18:16:45 字数 1646 浏览 2 评论 0原文

我有一个活动,我希望它使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声无音无过去 2024-10-13 18:16:45

我的应用程序中有类似的功能。您将需要创建一个 XML 文件来定义列表的布局并在 java 代码中调用它们。

下面是示例 XML:

LIST:

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

代码的新部分所做的只是通过意图将所选项目发送到另一个活动。然后在新活动中,我使用包中选定的项目名称查询数据库并显示结果。

请询问您是否需要进一步解释。

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:

<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>

This is the custom layout XML. I call it 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>

And this is the JAVA code:

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

The 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);
}

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.

ㄟ。诗瑗 2024-10-13 18:16:45

我正在制作一个非常相似的程序。我在使用它时遇到了麻烦,因为它是我的第一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文