mercredi 3 juin 2015

How to switch ListView into TextView to retrieve data from database

I'm looking for a way to retrieve one specific data from database but right now I only can retrieved the data by using ListView not TextView. Here is my java file.

   @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 version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    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://ift.tt/nIICcg"
    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 indicators to EditText but got errors on the SimpleCursorAdapter.

Aucun commentaire:

Enregistrer un commentaire