jeudi 24 décembre 2015

Failed to retrieve data in Search View in android

I'm trying to do a simple retrieval of data from SQLite Database to the SearchView.

My problem is the SearchView is not populated with respect to the data stored in the database, although it always shows the green signal of

successfully created the database

Below is the code.

fragment_search.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://ift.tt/nIICcg"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#E6E6E6"
        android:orientation="vertical" >

        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:background="#FFFFFF" />

        <SearchView
            android:id="@+id/searchView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="3dp" >
        </SearchView>

        <ListView
            android:id="@+id/listview_search"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/searchView"
            android:layout_centerHorizontal="true"
            android:divider="#E6E6E6"
            android:dividerHeight="5dp" />

        <LinearLayout
            android:id="@+id/rightLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/view1"
            android:layout_alignTop="@+id/view1"
            android:orientation="vertical"
            android:paddingTop="25dp" >

            </LinearLayout>

    </RelativeLayout>

Search_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/hotelLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/section_search"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >

            <GridLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:columnCount="2" >

                <ImageView
                    android:id="@+id/hotel_image"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:src="@drawable/aaa" />

                <EditText
                    android:id="@+id/hotel_name"
                    android:layout_width="209dp"
                    android:layout_height="56dp"
                    android:layout_gravity="fill_horizontal" >

                    <requestFocus />
                </EditText>

                <EditText
                    android:id="@+id/hotel_city"
                    android:layout_width="96dp"
                    android:layout_column="1"
                    android:layout_gravity="left|bottom"
                    android:layout_row="0"
                    android:ems="10" />

                <EditText
                    android:id="@+id/hotel_country"
                    android:layout_width="106dp"
                    android:layout_column="1"
                    android:layout_gravity="right|bottom"
                    android:layout_row="0"
                    android:ems="10" />

            </GridLayout>

        </TableRow>

    </LinearLayout>

</RelativeLayout>

TableData.java

    package com.mytry.test;

    import android.provider.BaseColumns;

    public class TableData 
    {
        public TableData()
        {

        }

        public static abstract class TableInfo implements BaseColumns
        {
            public static final String DATABASE_NAME = "tourDguide";
            public static final String TABLE_NAME = "Hotels";
            public static final String HOTEL_ID = "id";
            public static final String HOTEL_NAME = "hotel_name";
            public static final String HOTEL_ADDRESS = "hotel_address";
            public static final String HOTEL_CITY = "hotel_city";
            public static final String HOTEL_COUNTRY = "hotel_country";
            public static final String HOTEL_POSTAL = "postal_code";

        }

    }

DataBaseHandler.java

    package com.mytry.test;

    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;


    import com.mytry.test.TableData.TableInfo;

    public class DataHandler 
    {

        public static final int DATABASE_VERSION = 1;


        SQLiteDatabase db;
        DataBaseHelper dbhelper;
        Context ctx;

        private static class DataBaseHelper extends SQLiteOpenHelper
        {
            public String CREATE_QUERY = "CREATE TABLE "+TableInfo.TABLE_NAME+"("+TableInfo.HOTEL_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+TableInfo.HOTEL_NAME+" VARCHAR,"+TableInfo.HOTEL_ADDRESS+" VARCHAR,"+TableInfo.HOTEL_CITY+" VARCHAR,"+TableInfo.HOTEL_COUNTRY+" VARCHAR,"+TableInfo.HOTEL_POSTAL+" INT );";

            public DataBaseHelper(Context ctx) {
                super(ctx,TableInfo.DATABASE_NAME, null, DATABASE_VERSION);
                Log.d("Database Operations", "Successfully Created Database");
                // TODO Auto-generated constructor stub
            }

            @Override
            public void onCreate(SQLiteDatabase db) {
                // TODO Auto-generated method stub
                try{
                db.execSQL(CREATE_QUERY);
                Log.d("Database Operations", "Successfully Created Table");
                }
                catch(SQLException e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

                db.execSQL("Drop table if exists "+TableInfo.TABLE_NAME);
                onCreate(db);


            }

        }

        public DataHandler(Context ctx) {
            this.ctx = ctx;
            dbhelper = new DataBaseHelper(ctx);
            // TODO Auto-generated constructor stub
        }

        public DataHandler open()
        {
            dbhelper = new DataBaseHelper(ctx);
            db = dbhelper.getReadableDatabase();
            return this;

        }

        public void close()
        {
            dbhelper.close();
        }

        public Cursor searchHotels(String inputText) throws SQLException
        {

            String query = "Select "+TableInfo.HOTEL_ID+" as _id,"+TableInfo.HOTEL_NAME+","+TableInfo.HOTEL_ADDRESS+","+TableInfo.HOTEL_CITY+","+TableInfo.HOTEL_COUNTRY+","+TableInfo.HOTEL_POSTAL+" from "+TableInfo.TABLE_NAME+" where "+TableInfo.HOTEL_NAME+" LIKE '" + inputText + "';";
            Log.d("table operations","Successfully transferred query");
            Cursor cr = db.rawQuery(query, null);

            if(cr!=null)
            {
                cr.moveToFirst();
            }

            return cr;

        }
    }

SeachViewActivity.java

    package com.mytry.test;

    import com.mytry.test.TableData.TableInfo;

    import android.R.anim;
    import android.app.Activity;
    import android.app.DownloadManager.Query;
    import android.content.Context;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.SearchView;
    import android.widget.SimpleAdapter;
    import android.widget.SimpleCursorAdapter;
    import android.widget.TextView;
    import android.widget.AdapterView.OnItemClickListener;

    public class SearchViewActivity extends Activity implements SearchView.OnQueryTextListener,SearchView.OnCloseListener
    {
        private ListView list;
        private SearchView search;
        private DataHandler dbHandler;
        private TextView name,city,country;
        private EditText edit;
        Context ctx=this;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_search);

            search = (SearchView) this.findViewById(R.id.searchView);

            list = (ListView) this.findViewById(R.id.listview_search);

            name = (TextView)this.findViewById(R.id.hotel_name);
            city = (TextView)this.findViewById(R.id.hotel_city);
            country = (TextView)this.findViewById(R.id.hotel_country);

            search.setIconifiedByDefault(false);

            search.setOnQueryTextListener(this);
            search.setOnCloseListener(this);

            dbHandler = new DataHandler(getBaseContext());
            dbHandler.open();


        }

        public boolean onQueryTextSubmit(String query) 
        {
            showResults(query + "*");
            return false;
        }

        public boolean onQueryTextChange(String newText) 
        {
            showResults(newText + "*");
            return false;
        }

        public boolean onClose() 

        {
            showResults("");

            return false;
        }

        private void showResults(String query) 
        {

        Cursor cr = dbHandler.searchHotels((query!=null?query.toString(): "@@@@"));

        if(cr==null)
        {

        }
        else
        {
            String[] from = new String[]
            {TableInfo.HOTEL_NAME,TableInfo.HOTEL_ADDRESS,TableInfo.HOTEL_CITY,TableInfo.HOTEL_COUNTRY,TableInfo.HOTEL_POSTAL};

            int[] to = new int[]{R.id.hotel_name,R.id.hotel_city,R.id.hotel_country};

            SimpleCursorAdapter hotels = new SimpleCursorAdapter(this,R.layout.search_list, cr, from, to);      
            list.setAdapter(hotels);

             list.setOnItemClickListener(new OnItemClickListener() 
             {
                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
                 {
                    Cursor cr = (Cursor)list.getItemAtPosition(position);

                    String hotel_name = cr.getString(cr.getColumnIndexOrThrow("hotel_name"));
                    String hotel_city = cr.getString(cr.getColumnIndexOrThrow("hotel_city"));
                    String hotel_country = cr.getString(cr.getColumnIndexOrThrow("hotel_country"));

                    LinearLayout hotelLayout = (LinearLayout)findViewById(R.id.hotelLayout);

                    if(hotelLayout == null){
                        //Inflate the Customer Information View 
                        LinearLayout leftLayout = (LinearLayout)findViewById(R.id.rightLayout);
                        View hotelInfo = getLayoutInflater().inflate(R.layout.search_list, leftLayout, false);
                        leftLayout.addView(hotelInfo);
                    }

                    name.setText(hotel_name);
                    city.setText(hotel_city);
                    country.setText(hotel_country);

                    search.setQuery("", true);

                }
            });

        }
        }
    }

Aucun commentaire:

Enregistrer un commentaire