简体   繁体   English

如何将ListView切换为TextView以从数据库检索数据

[英]How to switch ListView to TextView to retrieve data from database

I'm looking for a way to retrieve one specific datum from the database, but right now I only can retrieve the data by using a ListView - not a TextView . 我正在寻找一种从数据库中检索特定数据的方法,但是现在我只能使用ListView而不是TextView来检索数据。
Here is my java file. 这是我的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);
    }

My XML files: main.xml 我的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 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>

I tried to change all the specific ListView references to TextView but I got errors on the SimpleCursorAdapter . 我试图将所有特定的ListView引用更改为TextView,但是在SimpleCursorAdapter上遇到错误。

You don't need an adapter anymore. 您不再需要适配器。 So, now, instead of this: 所以,现在,代替这个:

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

use this 用这个

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

Output: Mr. Abdul Hanan 产出: Mr. Abdul Hanan


[EDIT] [编辑]

If you prefer an output like: Mr. Hanan, Abdul 如果您喜欢以下输出: Mr. Hanan, Abdul

Then, simply change the string format and the parameters order to 然后,只需更改字符串格式和参数顺序即可

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio,如何从Sqlite数据库中检索数据并将其显示到textview中? - Android Studio, how to retrieve data from Sqlite database and display it into textview? 如何从SQLite检索数据并在TextView中查看数据? - How to retrieve a data from SQLite and view it in a TextView? 我想从数据库中检索值而不使用listview并在textview领域中显示 - I want to retrieve values from database without using listview and display in textview feilds 如何使用从数据库加载的数字ListView的总和设置TextView值 - How set TextView value with sum of numeric ListView loaded from database 如何从 Firebase 检索 ListView 中的数据 - How can Retrieve data in ListView from Firebase 从 Android 中的 Firebase 实时数据库检索 ListView 上的数据不起作用 - retrieve data on ListView from Firebase Realtime Database in android not working 如何将数据从textview传递到listview项目选择 - how to pass data from textview to listview items selection 如何从json数组检索数据并在textview中显示? - How to retrieve data from json array and display in textview? 如何从SQLite检索整数数据并在textview中显示它? - How to retrieve integer data from SQLite and show it in textview? Android:如何从网络服务器检索单个数据并将其显示在textview中? - Android: How to retrieve a single data from a web server and display it to a textview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM