I have a listView that I am populating with data from my existing SQLite database, but right now, it's just displaying every item from my database. I also have a searchView in my xml file below called activity_shirts.xml, it has a searchView and then below it the listView that's being populated. But I want to make it where the user enters something into the searchView and then I will query what they typed into the searchView and then just display those items from the database using the value they typed in. Basically I need to filter this listView, they type in something and then below it'll populate the listView. Does anyone know how I can use the searchView to make that happen? Any help will be appreciated.
package ankitkaushal.app.healthysizing;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
public static String DB_PATH = "/data/data/ankitkaushal.app.healthysizing/databases/";
public static String DB_NAME = "HealthySizing";
public static final int DB_VERSION = 1;
public static final String TB_USER = "Shirts";
private SQLiteDatabase myDB;
private Context context;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
@Override
public synchronized void close(){
if(myDB!=null){
myDB.close();
}
super.close();
}
public List<String> getAllUsers() {
List<String> listUsers = new ArrayList<String>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor c;
try {
c = db.rawQuery("SELECT * FROM " + TB_USER , null);
if(c == null) return null;
String name;
c.moveToFirst();
do {
name = c.getString(5);
listUsers.add(name);
} while (c.moveToNext());
c.close();
} catch (Exception e) {
Log.e("tle99", e.getMessage());
}
db.close();
return listUsers;
}
/*public List<Item> getAllShirts() {
List<Item> shirtList = new ArrayList<Item>();
String query = "SELECT * FROM " + TB_USER; //query to get all the shirts
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()){
do {
Item item = new Item();
item.setPrice(cursor.getString(2));
item.setBrand(cursor.getString(1));
item.setStore(cursor.getString(3));
item.setDescription(cursor.getString(5));
shirtList.add(item);
} while (cursor.moveToNext());
}
return shirtList;
}*/
public ArrayList<Item> getAllShirts() {
ArrayList<Item> shirtList = new ArrayList<Item>();
String query = "SELECT * FROM " + TB_USER; //query to get all the shirts
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()){
do {
Item item = new Item();
item.setPrice(cursor.getString(2));
item.setBrand(cursor.getString(1));
item.setStore(cursor.getString(3));
item.setDescription(cursor.getString(5));
shirtList.add(item);
} while (cursor.moveToNext());
}
return shirtList;
}
public Cursor getShirtsData() {
String SQLQuery = "SELECT * FROM" + TB_USER;
return myDB.rawQuery(SQLQuery, null);
}
/***
* Open database
* @throws android.database.SQLException
*/
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
/**
* Copy database from source code assets to device
* @throws java.io.IOException
*/
public void copyDataBase() throws IOException {
try {
InputStream myInput = context.getAssets().open(DB_NAME);
String outputFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outputFileName);
byte[] buffer = new byte[1024];
int length;
while((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
Log.e("tle99 - copyDatabase", e.getMessage());
}
}
/***
* Check if the database doesn't exist on device, create new one
* @throws IOException
*/
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.e("tle99 - create", e.getMessage());
}
}
}
// ---------------------------------------------
// PRIVATE METHODS
// ---------------------------------------------
/***
* Check if the database is exist on device or not
* @return
*/
private boolean checkDataBase() {
SQLiteDatabase tempDB = null;
try {
String myPath = DB_PATH + DB_NAME;
tempDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
Log.e("tle99 - check", e.getMessage());
}
if (tempDB != null)
tempDB.close();
return tempDB != null ? true : false;
}
}
package ankitkaushal.app.healthysizing;
public class Item {
private String brand;
//private String item;
private String price;
private String store;
private String description;
public Item() {
}
public void setBrand(String brand) {
this.brand = brand;
}
//public void setItem(String item) {
// this.item = item;
//}
public void setPrice(String price) {
this.price = price;
}
public void setStore(String store) {
this.store = store;
}
public void setDescription(String description) {this.description = description;}
public String getStore() {
return store;
}
public String getPrice() {
return price;
}
//public String getItem() {
// return item;
//}
public String getBrand() {
return brand;
}
public String getDescription() {return description; }
@Override
public String toString() {
return (this.brand + " " + this.store + " " + this.price + " " + this.description);
}
}
package ankitkaushal.app.healthysizing;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public final class ListItemAdapter extends ArrayAdapter<Item> implements View.OnClickListener{
public ListItemAdapter(Context context, ArrayList<Item> shirtItems) {
super(context, 0, shirtItems);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Item item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_layout_shirts, parent, false);
}
// Lookup view for data population
TextView brand = (TextView) convertView.findViewById(R.id.txt_shirt_brand);
TextView price = (TextView) convertView.findViewById(R.id.txt_shirt_price);
TextView store = (TextView) convertView.findViewById(R.id.txt_shirt_store);
TextView description = (TextView) convertView.findViewById(R.id.txt_shirt_description);
// Populate the data into the template view using the data object
brand.setText("Brand:" + " " + item.getBrand());
price.setText("Price:" + " " + item.getPrice());
store.setText("Store:" + " " + item.getStore());
description.setText("Description:" + " " + item.getDescription());
// Return the completed view to render on screen
return convertView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
package ankitkaushal.app.healthysizing;
import android.app.ActionBar;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.SearchView;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.logging.Handler;
public class shirtsActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shirts);
final DatabaseHelper dbhelper;
final ListView listView;
final ListAdapter shirtsAdapter;
dbhelper = new DatabaseHelper(getApplicationContext());
try {
dbhelper.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
listView = (ListView) findViewById(R.id.listViewShirts);
//List<Item> shirtsList = dbhelper.getAllShirts();
ArrayList<Item> shirtsList = dbhelper.getAllShirts();
if (shirtsList != null) {
//shirtsAdapter = new ArrayAdapter<Item>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, shirtsList);
shirtsAdapter = new ListItemAdapter(getApplicationContext(), shirtsList);
listView.setAdapter(shirtsAdapter);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#29A9D2"
android:weightSum="1"
android:id="@+id/shirt"
android:onClick="onClickSearch">
<SearchView
android:layout_width="352dp"
android:layout_height="wrap_content"
android:id="@+id/searchView3"
android:background="#ffffffff"
android:queryHint="Search for a specific brand" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listViewShirts"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/txt_shirt_price"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/button"
android:layout_alignEnd="@+id/button"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/txt_shirt_brand"
android:layout_below="@+id/txt_shirt_price"
android:layout_alignLeft="@+id/txt_shirt_price"
android:layout_alignStart="@+id/txt_shirt_price"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/txt_shirt_store"
android:layout_below="@+id/txt_shirt_brand"
android:layout_alignLeft="@+id/txt_shirt_brand"
android:layout_alignStart="@+id/txt_shirt_brand"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/txt_shirt_description"
android:layout_below="@+id/txt_shirt_store"
android:layout_alignLeft="@+id/txt_shirt_store"
android:layout_alignStart="@+id/txt_shirt_store" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add item to Cart"
android:id="@+id/button"
android:layout_below="@+id/txt_shirt_description"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/button"
android:layout_alignBottom="@+id/txt_shirt_description"
android:layout_alignParentTop="true"
android:src="@drawable/shirts"
android:layout_toLeftOf="@+id/button"
android:layout_marginRight="-20dp"
android:layout_marginLeft="10dp" />
</RelativeLayout>
</LinearLayout>
Aucun commentaire:
Enregistrer un commentaire