I am trying to show data from SQLite
into a custom ListView
. I got the data and I can show it into ListView
, But I have a problem.
Everytime I scroll it into the last data in ListView
its always error. Its said ArrayIndexOutOfBoundsException. I already search it but still can't find the solution.
So what I want to ask is, how to fix that error or is there another method that I can use to show the data in ListView
?
Here's My DatabaseHandler.java
// Getting All search result
public List<AllItem> getAllSearchResult(String searchResult) {
List<AllItem> allsearchResultList = new ArrayList<AllItem>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_ALLITEM + " WHERE " + KEY_ITEM_NAME_ALLITEM + " LIKE '%"+searchResult+"%' ";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
AllItem allitem = new AllItem();
allitem.setTableID(cursor.getInt(0));
allitem.setID(cursor.getString(1));
allitem.setCategory_name(cursor.getString(2));
allitem.setItem_Name(cursor.getString(3));
allitem.setDescription(cursor.getString(4));
allitem.setAddress(cursor.getString(5));
allitem.setArea (cursor.getString(6));
allitem.setAreaName (cursor.getString(7));
// Adding searchResult to list
allsearchResultList.add(allitem);
} while (cursor.moveToNext());
}
// return searchResult list
return allsearchResultList;
}
Here's My Search.java
Intent search = getIntent();
String searchResult = search.getStringExtra("TAG_SEARCH");
DatabaseHandler db = new DatabaseHandler(this);
//Cursor mCur = (Cursor) db.getAllSearchResults(searchResult);
//CurAdapter Cur = new CurAdapter(Search.this, mCur,0);
//final ListView lv = (ListView)findViewById(R.id.listSearch);
//lv.setFastScrollEnabled(true);
//lv.setAdapter(Cur);
/**
* CRUD Operations
* */
Log.d("Reading: ", "Reading all contacts..");
List <AllItem> allItems = new ArrayList<AllItem>();
allItems = db.getAllSearchResult(searchResult);
ArrayList <String> allItems2 = new ArrayList<String>();
for (AllItem cn : allItems) {
allItems2.add(cn.getItem_name());
allItems2.add(cn.getAreaNAme());
allItems2.add(cn.getCategory_name());
}
CustomAdapterSearch adapter = new CustomAdapterSearch(Search.this, allItems2);
listview.setAdapter(adapter);
And here's my CustomAdapter.java
public class CustomAdapterSearch extends ArrayAdapter<String> {
private final Activity context;
private final List<String> items;
public CustomAdapterSearch (Activity context, List<String> items) {
super(context, R.layout.searchlayout, items);
this.context = context;
this.items = items;
}
@Override
public View getView (final int position, View view, ViewGroup parent)
{
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.searchlayout, null, true);
TextView itemName = (TextView) rowView.findViewById(R.id.item_name);
TextView areaName = (TextView) rowView.findViewById(R.id.area_name);
TextView priceCatergory = (TextView) rowView.findViewById(R.id.price_category);
itemName.setText(items.get(3*position));
areaName.setText(items.get(3*position + 1));
priceCatergory.setText(items.get(3*position + 2));
return rowView;
}
}
Is there anyone can help me? Thanks before :D
0 comments:
Post a Comment