我試圖將SQLite
中的數據顯示爲自定義ListView
。我得到的數據,我可以顯示它到ListView
,但我有一個問題。在自定義ListView中顯示SQLite數據
每當我滾動到ListView
的最後一個數據時,它總是出錯。它說的ArrayIndexOutOfBoundsException。我已經在搜索它,但仍找不到解決方案。
所以我想問的是,如何解決這個錯誤,或者是否有另一種方法可以用來顯示ListView
中的數據?
這裏是我的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;
}
這裏是我的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);
這是我的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;
}
}
有沒有人可以幫助我?由於前:d
這必須是與這塊您的代碼:items.get(3 *位置)。你有什麼打算呢? – Opiatefuchs 2014-09-05 09:52:45
使用SimpleCursorAdapter而不是ArrayAdapter – pskink 2014-09-05 09:56:04
它用於位置和多少列我用它。 – Matthew 2014-09-05 09:56:05