I'm looking for some direction on the possibility of filtering a listview based on spinner selections.
The app copies a SQLite database from the assets folder into the system path. It's able to access the data including an image URL to populate listviews using LazyAdapter. I want to set spinner tabs along the top of the listview to filter the data.
I'm at a loss on how to do this. I tried making a query to return just the specific size, it comes back showing the correct items, but when the bottom of the list is scrolled there is an ArrayIndexOutOfBoundsException. I've tried a lot of different things and I've searched high and low on the Internet. Here is my code if anybody has any suggestions I would greatly appreciate it!
----------------------------------------------------------------------------------------------------------------------------------------------------
Below is the code to copy and access the database
DB.java
import android.content.ContentValues;
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.location.Location;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
public class DB extends SQLiteOpenHelper {
//The Android's default system path of your application database.
private static String DB_PATH = "data/data/gofor.gofor/databases/";
private static String DB_NAME = "CurrentOrderDB1";
static final String SUPPORT_TABLE = "supports";
static final String KEY_SUPPORT_ROWID = "_id";
static final String KEY_SUPPORT_SIZE = "support_size";
static final String KEY_SUPPORT_TYPE = "support_type";
static final String KEY_SUPPORT_MANUFACTURER = "support_manufacturer";
static final String KEY_SUPPORT_PARTNUMBER = "support_partNumber";
static final String KEY_SUPPORT_IMAGE = "support_image";
private final Context context;
private static SQLiteDatabase db;
public DB(Context context) {
super( context , DB_NAME , null , 1);
this.context = context;
}
public void create() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exists
}else{
// By calling this method and empty database will be created into the default system path
// of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
// Check if the database exist to avoid re-copy the data
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String path = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
}catch(SQLiteException e){
// database don't exist yet.
e.printStackTrace();
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
// copy your assets db to the new system DB
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = context.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
//Open the database
public static boolean open() {
try {
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
return true;
} catch(SQLException sqle) {
db = null;
return false;
}
}
@Override
public synchronized void close() {
if(db != null)
db.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public static Cursor getAllSupports() {
return db.query(SUPPORT_TABLE, new String[]{KEY_SUPPORT_ROWID,
KEY_SUPPORT_SIZE, KEY_SUPPORT_TYPE,
KEY_SUPPORT_MANUFACTURER, KEY_SUPPORT_PARTNUMBER,
KEY_SUPPORT_IMAGE}, null, null, null, null, null);
}
public static Cursor getSupportSize() {
return db.query(SUPPORT_TABLE, new String[]{KEY_SUPPORT_SIZE}, null, null, null, null, null);
}
public static Cursor getSupportType() {
return db.query(SUPPORT_TABLE, new String[]{KEY_SUPPORT_TYPE}, null, null, null, null, null);
}
public static Cursor getSupportManufacturer() {
return db.query(SUPPORT_TABLE, new String[]{KEY_SUPPORT_MANUFACTURER}, null, null, null, null, null);
}
public static Cursor getSupportPartNumber() {
return db.query(SUPPORT_TABLE, new String[]{KEY_SUPPORT_PARTNUMBER}, null, null, null, null, null);
}
public static Cursor getSupportImage() {
return db.query(SUPPORT_TABLE, new String[]{KEY_SUPPORT_IMAGE}, null, null, null, null, null);
}
public static Cursor getItemSupport(long rowId) throws java.sql.SQLException {
Cursor bCursor = db.query(true, SUPPORT_TABLE, new String[]{KEY_SUPPORT_ROWID, KEY_SUPPORT_SIZE, KEY_SUPPORT_TYPE, KEY_SUPPORT_MANUFACTURER, KEY_SUPPORT_PARTNUMBER, KEY_SUPPORT_IMAGE}, KEY_SUPPORT_ROWID + "=" + rowId, null, null, null, null, null);
if (bCursor != null) {
bCursor.moveToFirst();
}
return bCursor;
}
}
Below is the activity that populates the listview
ViewSupports.java
import android.annotation.TargetApi;
import android.app.Activity;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Spinner;
import java.sql.SQLException;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class ViewSupports extends Activity {
ListView list;
LazyAdapter adapter;
DB db;
private Spinner spinner1, spinner2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_supports);
try {
openDB();
} catch (SQLException e) {
e.printStackTrace();
}
list = (ListView) findViewById(R.id.currentSupportList);
adapter = new LazyAdapter(this, populateImageList(), populateSizeList(), populateTypeList(), populateManufacturerList(), populatePartNumberList());
list.setAdapter(adapter);
}
@Override
public void onDestroy() {
list.setAdapter(null);
super.onDestroy();
}
public OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View arg0) {
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
}
};
private String[] populateSizeList(){
Cursor crs = DB.getSupportSize();
startManagingCursor(crs);
String[] array = new String[crs.getCount()];
int i = 0;
while(crs.moveToNext()){
String uname = crs.getString(crs.getColumnIndex(DB.KEY_SUPPORT_SIZE));
array[i] = uname;
i++;
}
Log.d("myTag", array[0]);
return array;
}
private String[] populateTypeList(){
Cursor crs = DB.getSupportType();
startManagingCursor(crs);
String[] array = new String[crs.getCount()];
int i = 0;
while(crs.moveToNext()){
String uname = crs.getString(crs.getColumnIndex(DB.KEY_SUPPORT_TYPE));
array[i] = uname;
i++;
}
Log.d("myTag", array[0]);
return array;
}
private String[] populateManufacturerList(){
Cursor crs = DB.getSupportManufacturer();
startManagingCursor(crs);
String[] array = new String[crs.getCount()];
int i = 0;
while(crs.moveToNext()){
String uname = crs.getString(crs.getColumnIndex(DB.KEY_SUPPORT_MANUFACTURER));
array[i] = uname;
i++;
}
Log.d("myTag", array[0]);
return array;
}
private String[] populatePartNumberList(){
Cursor crs = DB.getSupportPartNumber();
startManagingCursor(crs);
String[] array = new String[crs.getCount()];
int i = 0;
while(crs.moveToNext()){
String uname = crs.getString(crs.getColumnIndex(DB.KEY_SUPPORT_PARTNUMBER));
array[i] = uname;
i++;
}
Log.d("myTag", array[0]);
return array;
}
public String[] populateImageList(){
Cursor crs = DB.getSupportImage();
startManagingCursor(crs);
String[] array = new String[crs.getCount()];
int i = 0;
while(crs.moveToNext()){
String uname = crs.getString(crs.getColumnIndex(DB.KEY_SUPPORT_IMAGE));
array[i] = uname;
i++;
}
Log.d("myTag", array[0]);
return array;
}
private void openDB() throws SQLException {
db = new DB(this);
DB.open();
}
}
Below code is the listview and tabs XML
listview_supports.xml
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Spinner
android:layout_width="96dp"
android:layout_height="50dp"
android:id="@+id/spinner1"
android:layout_weight="1"
android:spinnerMode="dialog"
android:entries="@array/sizeArray"/>
<Spinner
android:layout_width="120dp"
android:layout_height="50dp"
android:id="@+id/spinner2"
android:spinnerMode="dialog"
android:entries="@array/typeArray"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/currentSupportList"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Below is the tab arrays
<strong> strings.xml</strong>
<resources>
<string name="app_name">Go For</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string-array name="sizeArray">
<item>1/2"</item>
<item>3/4"</item>
<item>1"</item>
<item>1-1/4"</item>
<item>1-1/2"</item>
<item>2"</item>
<item>2-1/2"</item>
<item>3"</item>
<item>4"</item>
<item>5"</item>
<item>6"</item>
</string-array>
<string-array name="typeArray">
<item>One-Hole</item>
<item>Two-Hole</item>
<item>One-Piece Strut</item>
<item>Two-Piece Strut</item>
<item>Mineralac</item>
</string-array>
Aucun commentaire:
Enregistrer un commentaire