Java 如何将ListView切换到TextView以从数据库检索数据
我正在寻找一种从数据库中检索一个特定数据的方法,但现在我只能使用列表视图-而不是文本视图检索数据Java 如何将ListView切换到TextView以从数据库检索数据,java,android,sqlite,listview,Java,Android,Sqlite,Listview,我正在寻找一种从数据库中检索一个特定数据的方法,但现在我只能使用列表视图-而不是文本视图检索数据 这是我的java文件 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); db = (new DatabaseHelper(this)).
这是我的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"))))
}
输出:Abdul Hanan先生
[编辑] 如果您喜欢这样的输出:
哈南先生,阿卜杜勒
然后,只需将字符串格式和参数更改为
txtMyTextView.setText(String,format("%s %s, %s",
cursor.getString(cur.getColumnIndex("title")),
cursor.getString(cur.getColumnIndex("lastName")),
cursor.getString(cur.getColumnIndex("firstName"))))
你不再需要适配器了。将光标移动到第一条记录后,只需使用返回的数据即可。什么是
如何将ListView转换为TextView
?如何将列表视图
转换为文本视图
@Nilesh我也对这个标题感到惊讶。然后我读了问题。OP只需要1条记录中的1列。所以他们想从列表视图切换到文本视图。@DerGolem抱歉英语不是我的母语。SimpleCursorAdapter与TextView配合得好吗?不。正如我所说,您不再需要适配器(不是任何适配器)。因为你不再需要使用ListView了。现在,您的数据位于光标中。所以txtMyTextView是从main.xml检索到的新文本视图,不是吗?它只是一个示例名称。使用main.xml布局中的一个。是的,您必须先检索它,然后才能使用它(通常使用findViewById()
)