我正在寻找一种从数据库中检索一个特定数据的方法,但是现在我只能使用 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 上遇到错误。
答案 0 :(得分:2)
你不再需要适配器了。那么,现在,而不是这个:
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. Abdul Hanan
<强> [编辑] 强>
如果您更喜欢以下输出:Mr. Hanan, Abdul
然后,只需将字符串格式和参数顺序更改为
即可 txtMyTextView.setText(String,format("%s %s, %s",
cursor.getString(cur.getColumnIndex("title")),
cursor.getString(cur.getColumnIndex("lastName")),
cursor.getString(cur.getColumnIndex("firstName"))))