mercredi 9 mars 2016

integration of search view with list view

I am trying to itegrate search view (filter) with list view. the list is appearing, but the search functionality is not working. If someone can look into the code. Error is in the top fragment.java. also we have followed the following tutorial:

http://ift.tt/1j6RKFN

Fragment class:

     package com.example.student.smartshoppingcart;

import android.app.Fragment;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SearchView;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import java.lang.String;

/**
 * Created by Student on 01/03/2016.
 */
public class TopFragment extends Fragment {

    ListView listView;



    SQLiteDatabase sqLiteDatabase;
    DatabaseHelper databaseHelper;
    Cursor cursor;
    ListDataAdapter listDataAdapter;


    public interface itemSearcher{
    }


    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){
       View rootview =  inflater.inflate(R.layout.fragment_top,container,false);

        listView = (ListView)rootview.findViewById(R.id.list_view);

        listDataAdapter = new ListDataAdapter(getActivity().getApplicationContext(),R.layout.row_layout);
        listView.setAdapter(listDataAdapter);
        databaseHelper = new DatabaseHelper(getActivity().getApplicationContext());
        sqLiteDatabase = databaseHelper.getReadableDatabase();
        cursor =  databaseHelper.getInformation(sqLiteDatabase);
        if(cursor.moveToFirst()){
            do{

                String itemname, location;
                String price;

                itemname = cursor.getString(0);
                price=cursor.getString(1);
                location=cursor.getString(2);
                DataProvider dataProvider = new DataProvider(itemname,price,location);
                listDataAdapter.add(dataProvider);

            }
            while (cursor.moveToNext());

        }

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                // Get the cursor, positioned to the corresponding row in the result set
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);

                // Get the state's capital from this row in the database.
                String srno =
                        cursor.getString(cursor.getColumnIndexOrThrow("srno"));
                Toast.makeText(getActivity().getApplicationContext(),
                        srno, Toast.LENGTH_SHORT).show();

            }
        });

        EditText myFilter = (EditText) getActivity().findViewById(R.id.myFilter);
        myFilter.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }
//line of error
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                listDataAdapter.getFilter().filter(s.toString());
            }
        });

        listDataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {
                return databaseHelper.getInformation(constraint.toString());
            }
        });

        return  rootview;

    }






}

ListDataAdapter Class

    package com.example.student.smartshoppingcart;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Student on 04/03/2016.
 */
public class ListDataAdapter extends ArrayAdapter {

    List list = new ArrayList();

    public ListDataAdapter(Context context, int resource){
        super(context,resource);
    }
static class layoutHandler{

    TextView ITEMNAME,PRICE,LOCATION;

}

    @Override
    public void add(Object object){
        super.add(object);
        list.add(object);
    }

    @Override

    public int getCount(){
        return list.size();
    }

    @Override
    public Object getItem(int position){

        return (String)list.get(position);
    }

    @Override
    public View getView(int position, View convertview, ViewGroup parent){
        View row = convertview;
        layoutHandler layoutHandler;
        if(row==null){
            LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row= layoutInflater.inflate(R.layout.row_layout,parent,false);
            layoutHandler = new layoutHandler();
            layoutHandler.ITEMNAME= (TextView)row.findViewById(R.id.itemdescription);
            layoutHandler.PRICE = (TextView)row.findViewById(R.id.itemprice);
            layoutHandler.LOCATION=(TextView)row.findViewById(R.id.itemlocation);
            row.setTag(layoutHandler);


        }

        else {
            layoutHandler = (layoutHandler)row.getTag();


        }

        DataProvider dataProvider = (DataProvider)this.getItem(position);
        layoutHandler.ITEMNAME.setText(dataProvider.getItemname());
        layoutHandler.PRICE.setText(String.valueOf(dataProvider.getPrice()));
        layoutHandler.LOCATION.setText(dataProvider.getLocation());




        return row;


    }

}

DatabaseHelper Class:

      package com.example.student.smartshoppingcart;

        import android.content.ContentValues;
        import android.content.Context;
        import android.database.Cursor;
        import android.database.sqlite.SQLiteDatabase;
        import android.database.sqlite.SQLiteOpenHelper;
        import android.database.sqlite.SQLiteException;
        import java.sql.SQLException;
/**
 * Created by priya on 4/2/2015.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "contacts.db";
    private static final String TABLE_NAME = "contacts";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_EMAIL = "email";
    private static final String COLUMN_UNAME = "uname";
    private static final String COLUMN_PASS = "pass";


    private static final String TAG = "CountriesDbAdapter";
    private DatabaseHelper mDbHelper;
    private final Context mCtx;

    SQLiteDatabase db;
    private static final String TABLE_CREATE = "create table contacts (id integer primary key not null , " +
            "name text not null , email text not null , uname text not null , pass text not null);";
    //private static final String TABLE_CREATE2 = "create table iteminfo (srno integer primary key not null , " +
    //      "itemname text not null , barcode text not null , description text not null , department text not null,category text not null," +
    //    " subcategory text not null, brand text not null, tp numeric not null, rp numeric not null, location text not null );";

    private static final String TABLE_CREATE2 =
            "CREATE TABLE IF NOT EXISTS "+ UserContract.NewItemInfo.TABLE_NAME2+"("+UserContract.NewItemInfo.SRNO+" TEXT, "+
                    UserContract.NewItemInfo.BARCODE+" TEXT, "+
                    UserContract.NewItemInfo.DESCRIPTION+" TEXT, "+
                    UserContract.NewItemInfo.DEPARTMENT+" TEXT, "+
                    UserContract.NewItemInfo.CATEGORY+" TEXT, "+
                    UserContract.NewItemInfo.SUBCATEGORY+" TEXT, "+
                    UserContract.NewItemInfo.BRAND+" TEXT, "+
                    UserContract.NewItemInfo.TP+" NUMERIC, "+
                    UserContract.NewItemInfo.RP+" NUMERIC, "+
                    UserContract.NewItemInfo.LOCATION+" TEXT);";


    public DatabaseHelper(Context mCtx) {
        super(mCtx, DATABASE_NAME, null, DATABASE_VERSION);
        this.mCtx = mCtx;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        this.db = db;
        db.execSQL(TABLE_CREATE);
        db.execSQL(TABLE_CREATE2);

    }

    public void addInformation (String srno, String barcode, String description, String department, String category, String subcategory, String brand, Float tp, Float rp, String location, SQLiteDatabase db2){
        ContentValues contentValues = new ContentValues();
        contentValues.put(UserContract.NewItemInfo.SRNO,srno);
        contentValues.put(UserContract.NewItemInfo.BARCODE,barcode);
        contentValues.put(UserContract.NewItemInfo.DESCRIPTION,description);
        contentValues.put(UserContract.NewItemInfo.DEPARTMENT,department);
        contentValues.put(UserContract.NewItemInfo.CATEGORY,category);
        contentValues.put(UserContract.NewItemInfo.SUBCATEGORY,subcategory);
        contentValues.put(UserContract.NewItemInfo.BRAND,brand);
        contentValues.put(UserContract.NewItemInfo.TP,tp);
        contentValues.put(UserContract.NewItemInfo.RP,rp);
        contentValues.put(UserContract.NewItemInfo.LOCATION, location);
        db2.insert(UserContract.NewItemInfo.TABLE_NAME2, null, contentValues);



    }

    public Cursor getInformation(SQLiteDatabase db){

        Cursor cursor;
        String[] projections = {UserContract.NewItemInfo.DESCRIPTION,UserContract.NewItemInfo.RP,UserContract.NewItemInfo.LOCATION};
        cursor= db.query(UserContract.NewItemInfo.TABLE_NAME2,projections,null,null,null,null,null);
        return cursor;


    }

    public void insertContact(Contact c) {
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        String query = "select * from contacts";
        Cursor cursor = db.rawQuery(query , null);
        int count = cursor.getCount();

        values.put(COLUMN_ID , count);
        values.put(COLUMN_NAME, c.getName());
        values.put(COLUMN_EMAIL, c.getEmail());
        values.put(COLUMN_UNAME, c.getUname());
        values.put(COLUMN_PASS, c.getPass());

        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public String searchPass(String uname)
    {
        db = this.getReadableDatabase();
        String query = "select uname, pass from "+TABLE_NAME;
        Cursor cursor = db.rawQuery(query , null);
        String a, b;
        b = "not found";
        if(cursor.moveToFirst())
        {
            do{
                a = cursor.getString(0);

                if(a.equals(uname))
                {
                    b = cursor.getString(1);
                    break;
                }
            }
            while(cursor.moveToNext());
        }

        return b;
    }






    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
        //db.execSQL(query);
        this.onCreate(db);
    }

    //public DatabaseHelper(Context ctx) {

    //}

    public DatabaseHelper open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        db = mDbHelper.getWritableDatabase();
        return this;
    }
    public void close() {
        if (mDbHelper != null) {
            mDbHelper.close();
        }
    }

}

Aucun commentaire:

Enregistrer un commentaire