2
我完全陌生於android。我在sqlite數據庫中有大約500個數據。我試圖在gridView上訪問它,我正在使用自定義arrayAdapter,但它們在加載數據時速度很慢。我想要更快地顯示數據。任何人都可以推薦我最好的方法嗎?使用ArrayAdapter的GridView SQLite
這裏是我做了什麼:
在DB:
public ArrayList<DataDetails> getDetails()
{
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_DATA_DETAILS_LOCAL;
Cursor cursor = db.rawQuery(selectQuery, null);
ArrayList<DataDetails> datalist = new ArrayList<DataDetails>();
if (cursor.moveToFirst())
{
DataDetails valueID = null;
do
{
valueID = new DataDetails(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7));
datalist.add(valueID);
}
while (cursor.moveToNext());
cursor.close();
db.close();
}
return datalist;
}
對於獲取數據從DB:
for (int i = 0; i < DetailsDataCount; i++)
{
String dataId = db.getDetails().get(i).getdataID();
String dataType = db.getDetails().get(i).getDataType();
String dataTypeImage = db.getDetails()get(i).getDataTypeImage();
String dataDate = db.getDetails().get(i).getDataDate();
String dataWeek = db.getDetails().get(i).getDataWeek();
String dataAcount = db.getDetails().get(i).getDataAcount();
String dataAccountImage = db.getDetails().get(i).getDataAccountImage();
String dataAccounttextData = db.getDetails().get(i).getAccountData();
listofDataDetails = new dataDetails(dataId, dataType, dataTypeImage, dataDate, dataWeek, dataAcount, dataAccountImage, dataAccounttextData);
dataList.add(listofDataDetails);
}
gridView.setVisibility(View.VISIBLE);
noDataText.setVisibility(View.GONE);
adapterfordata = new DataAdapter(getActivity(), R.layout.data_main_row, dataList);
gridView.setAdapter(adapterfordata);
我的客戶適配器看起來象是這樣的:
public class DataAdapter extends ArrayAdapter<DataDetails>
{
ArrayList<DataDetails> dataList;
private ArrayList<DataDetails> originalList;
private AccountFilter filter;
private LayoutInflater vi;
private Context mContext;
private String datadate;
public DataAdapter(Context context, int textViewResourceId, ArrayList<DataDetails> nameList)
{
super(context, textViewResourceId, nameList);
this.dataList = new ArrayList<DataDetails>();
this.dataList.addAll(nameList);
this.originalList = new ArrayList<DataDetails>();
this.originalList.addAll(nameList);
mContext = context;
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private class ViewHolder
{
public TextView dataDate;
public ImageView dataTypeimage;
public TextView dataType;
public TextView dataAccount;
}
@Override
public View getView(int position, View convertView, final ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
convertView = vi.inflate(R.layout.data_main_row, null);
holder = new ViewHolder();
holder.datatypeimage = (ImageView) convertView.findViewById(R.id.data_type_Image);
holder.dataType = (TextView) convertView.findViewById(R.id.data_Type);
holder.dataDate = (TextView) convertView.findViewById(R.id.data_Account);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
DataDetails bd = dataList.get(position);
String tempImage = bd.getdataTypeImage();
int resID = mContext.getResources().getIdentifier(tempImage, "drawable", mContext.getPackageName());
holder.dataTypeimage.setImageResource(resID);
holder.dataType.setText(bd.getDataType());
return convertView;
}
}
如果有人能指導我這個會很棒嗎?
謝謝!
如果是的話,是否有可能知道lastscroll的位置? –
嗨,你可以在你的gridview對象中使用gridView.setOnScrollListener(),檢查這個迴應關於同一個問題:http://stackoverflow.com/questions/20276396/android-how-can-i-know-when- gridview已經達到了底部 – SekthDroid
我試過你的鏈接。每當我結束滾動時,都會一直保持呼叫。 –