2

I am totally new to android. I have about 500 data in a sqlite DB. I am trying to access this on gridView for which, I am using custom arrayAdapter, but they are very slow in loading the data. I would like to get the data shown faster. Can anybody recommend me the best way to do so?

Here is what I have done:

In 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;
    }

For Fetching data from 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);

My Customer Adapter Looks like this:

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;
    }
  }

It will be great if anybody can guide me on this?

Thanks!

Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
Sanjana Nair
  • 2,663
  • 6
  • 26
  • 44

1 Answers1

0

Maybe one approach is getting only a few items, like 20 or 30 items first, and implement some kind of behavior around the GridView scroll, so when the user reach the las item, you call again the database to query from the las element you have in the grid, and fetch 30 more (just as example).

For example:

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

It's just an example of something you can do.

So you will not make an sql query to fetch around 500 items, when maybe the user are going to see 100 or less.

With this you avoid make a heavy query, a heavy map from 500 rows to 500 of your DataDetails objects, I think this will improve a little bit your GridView.

Hope it helps.

Best regards

SekthDroid
  • 140
  • 1
  • 9
  • Is it possible know the lastscroll position if so how? – Sanjana Nair May 16 '15 at 16:49
  • Hi, you can use the gridView.setOnScrollListener() in the your gridview object, check this response about the same question: http://stackoverflow.com/questions/20276396/android-how-can-i-know-when-gridview-has-reached-the-bottom – SekthDroid May 16 '15 at 17:31
  • I tried your link. This keep calling every time not just when I end the scroll. – Sanjana Nair May 18 '15 at 02:50
  • Of course, its a ScrollListener, it will be called whenever the action of a "scroll" occurs in the GridView, it's your obligation to implement what you need there, using his parameters, and the objects you got in memory. So you will need to use the 4th param that the method onScroll brings to you, and compare with your dataList.size(), to know when you have reached the end. Hope it helps ;) – SekthDroid May 18 '15 at 18:11