gpt4 book ai didi

java - 如何将ListView切换到TextView以从数据库检索数据

转载 作者:行者123 更新时间:2023-12-02 04:36:53 24 4
gpt4 key购买 nike

我正在寻找一种从数据库中检索特定数据的方法,但现在我只能使用 ListView 检索数据 - 而不是 TextView .
这是我的 java 文件。

   @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = (new DatabaseHelper(this)).getWritableDatabase();
searchText = (EditText) findViewById (R.id.searchText);
employeeList = (ListView) findViewById (R.id.list);
}

public void search(View view) {
// || is the concatenation operation in SQLite
cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName = ?",
new String[]{searchText.getText().toString()});
adapter = new SimpleCursorAdapter(
this,
R.layout.employee_list_item,
cursor,
new String[] {"title"},
new int[] {R.id.title});
employeeList.setAdapter(adapter);
}

我的 XML 文件:main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/searchText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

<Button
android:id="@+id/searchButton"
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="search"/>

</LinearLayout>

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

</LinearLayout>

employee_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8px">

<TextView
android:id="@+id/firstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/lastName"
android:layout_marginLeft="6px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/firstName"/>

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/firstName"/>

</RelativeLayout>

我尝试将所有特定的 ListView 引用更改为 TextView,但在 SimpleCursorAdapter 上出现错误。

最佳答案

您不再需要适配器。所以,现在,而不是这个:

    adapter = new SimpleCursorAdapter(
this,
R.layout.employee_list_item,
cursor,
new String[] {"title"},
new int[] {R.id.title});
employeeList.setAdapter(adapter);

用这个

if (cursor != null) && (cursor.moveToFirst)
{
txtMyTextView.setText(String,format("%s %s %s",
cursor.getString(cur.getColumnIndex("title")),
cursor.getString(cur.getColumnIndex("firstName")),
cursor.getString(cur.getColumnIndex("lastName"))))
}

输出:先生。阿卜杜勒·哈南

<小时/>

[编辑]

如果您喜欢这样的输出:Mr.阿卜杜勒·哈南

然后,只需将字符串格式和参数顺序更改为

    txtMyTextView.setText(String,format("%s %s, %s",
cursor.getString(cur.getColumnIndex("title")),
cursor.getString(cur.getColumnIndex("lastName")),
cursor.getString(cur.getColumnIndex("firstName"))))

关于java - 如何将ListView切换到TextView以从数据库检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30636795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com