2015-05-16 53 views
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; 
    } 
    } 

如果有人能指導我這個會很棒嗎?

謝謝!

回答

0

也許一種方法是隻獲得幾個項目,比如先獲得20或30個項目,然後在GridView滾動周圍實現某種行爲,所以當用戶到達las項目時,您再次調用數據庫以從拉斯你在網格中的元素,並獲取30多(例如)。

例如:

if (isScrollAtLastPosition){ 
    showLoading(); 
    dataList.addAll(getDetails(dataList.size(), 30)); 
    adapter.notifyDataSetChanged(); 
    hideLoading(); 
} 

這只是事情你可以做一個例子。

所以當用戶打算看到100或更少的時候,你不會進行sql查詢來獲取大約500個項目。

有了這個,你可以避免做一個沉重的查詢,從500行到500的DataDetails對象的一個​​沉重的地圖,我認爲這會改善一點點你的GridView

希望它有幫助。

問候函

+0

如果是的話,是否有可能知道lastscroll的位置? –

+0

嗨,你可以在你的gridview對象中使用gridView.setOnScrollListener(),檢查這個迴應關於同一個問題:http://stackoverflow.com/questions/20276396/android-how-can-i-know-when- gridview已經達到了底部 – SekthDroid

+0

我試過你的鏈接。每當我結束滾動時,都會一直保持呼叫。 –