mardi 15 septembre 2015

search by name from sqlite listview

I have already listview populating from database.What i want is search filter i.e when the user enters letter search all names entered by user . i have gone through many websites but dint find any solution . i am new to android development please help me in fix this . I have tried this code but the app is quiting . i dont know what is wrong with the code

activity_main.xml
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <!-- Editext for Search -->
    <EditText android:id="@+id/inputSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Search Choultry."
        android:inputType="text"/>

    <ListView
        android:id="@+id/listViewshowall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/inputSearch">

    </ListView>

</RelativeLayout>

MainActivity.java
package com.naman.namrathasrinivas.choultriesbangalore;

import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private DatabaseHandler  databaseHandler;

    private SQLiteDatabase db;
    private ListView listview;
    EditText inputSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            databaseHandler = new DatabaseHandler(MainActivity.this);
            databaseHandler.createdatabase();

        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }
        try {
            databaseHandler.opendatabase();
        } catch (SQLException sqle) {
            throw sqle;
        }


        listview=(ListView)findViewById(R.id.listViewshowall);



        // TODO Auto-generated method stub


        ArrayList<StudentEntity> list=databaseHandler.Getvalue(this.toString());
        ListViewAdapter adapter=new ListViewAdapter(MainActivity.this, list);
        listview.setAdapter(adapter);



        inputSearch = (EditText) findViewById(R.id.inputSearch);


        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
               // MainActivity.this.adapter.getFilter().filter(cs);

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub

                ArrayList<StudentEntity> listentity = new ArrayList<StudentEntity>();

                String selectQuery = "SELECT * FROM hall where name like 'b%' ";

                Cursor cursor = db.rawQuery(selectQuery, null);


                if (cursor.moveToFirst()) {
                    do {
                        StudentEntity sentity = new StudentEntity();
                        sentity.setName(cursor.getString(1));
                        sentity.setLocality(cursor.getString(12));


                        listentity.add(sentity);
                    } while (cursor.moveToNext());
                }

                // return contact list
              //  return listentity;


            }
        });
    }



}

Aucun commentaire:

Enregistrer un commentaire