3

I am trying to create a list view in a view pager using a mergeAdapter.So basically what i want is to fetch some dates from my database and and keeping the same dates in a sort of group (with some sort of header ).

so i am creating a Arraylist of cursors which will store data corresponding to unique dates and then try to merging them using merge adapter.

however the list is not getting created properly. instead it shows gaps between the two date and there is no content in the layout. just the header and gaps .moreover the dates are not getting grouped into unique date.

dbObj is my database object which contains the tables.

    Databases dbObj; // my database class
SQLiteDatabase ourDatabase; 
ArrayList<SimpleCursorAdapter> alladapters;
ArrayList<Cursor> incomeCursor; 
MergeAdapter listAdapter;

the view is inflated here inside the ViewPager here .

    public Object instantiateItem(View collection, int position)
    {

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = null;
        switch (position)
        {
            case 0:
                ListView daily = new ListView(getActivity()
                        .getApplicationContext());
                dates.clear();
                alladapters.clear();
                listAdapter.notifyDataSetInvalidated();
                getDates();
                buildAdapters();
                daily.setCacheColorHint(0);
                dbObj.open();
                ourDatabase = dbObj.getDatabase();

                setMergeAdapter();

                daily.setAdapter(listAdapter);

                view = daily;
                ((ViewPager) collection).addView(view, 0);
                dbObj.close();
                break;
              }
              return view;

these are my getdates and buildadapters methods

    private void getDates() throws SQLException
{
    dbObj.open();
    ourDatabase = dbObj.getDatabase();

    String[] columns = { Databases.KEY_ROWID, Databases.KEY_DATE };
    Cursor c = ourDatabase.query(true, Databases.DATABASE_TABLE_INCOME,
            columns, null, null, null, null, Databases.KEY_DATE, null);
    int i = 0;

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
    {
        dates.add(c.getString(c.getColumnIndex(Databases.KEY_DATE)));

        i++;
    }
    dbObj.close();
}

    private void buildAdapters() throws SQLException
{
    dbObj.open();
    ourDatabase = dbObj.getDatabase();

    Cursor c;

    String[] columns = { Databases.KEY_ROWID, Databases.KEY_DATE,
            Databases.KEY_AMOUNT, Databases.KEY_CATEGORY,
            Databases.KEY_DESCRIPTION };

    // Log.d("Size of Date ", msg)
    for (int i = 0; i < dates.size(); i++)
    {
        incomeCursor.add(ourDatabase.query(Databases.DATABASE_TABLE_INCOME,
                columns, Databases.KEY_DATE + "=" + dates.get(i), null,
                null, null, null));

        alladapters.add(new SimpleCursorAdapter(getActivity()
                .getApplicationContext(), R.layout.listlayout, incomeCursor
                .get(i), new String[] { Databases.KEY_AMOUNT,
                Databases.KEY_CATEGORY, Databases.KEY_DESCRIPTION },
                new int[] { R.id.ListAmount, R.id.ListCategory,
                        R.id.ListDescript },
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER));
    }

    dbObj.close();
}

setmerger adapter method

    private void setMergeAdapter()
    {
        // TODO Auto-generated method stub
        TextView tv = new TextView(getActivity().getApplicationContext());
        for (int i = 0; i < alladapters.size(); i++)
        {
            tv.setText(dates.get(i));
            listAdapter.addView(tv);
            listAdapter.addAdapter(alladapters.get(i));
        }
    }
dmon
  • 30,048
  • 8
  • 87
  • 96
Rajul
  • 5,906
  • 2
  • 22
  • 26
  • Step #1: Delete all occurrences of `.getApplicationContext()`. Step #2: Temporarily get rid of the `ViewPager` and get your code working in a single `ListView`. – CommonsWare Jan 29 '12 at 20:44
  • Thanx for the prompt reply.Could you please tell me why not use .getApplicationContext() as I am doing all this inside a Fragment(Compatibility Package).Ill try it without the View Pager. – Rajul Jan 30 '12 at 07:29
  • 1
    Only use `getApplicationContext()` when you **know precisely why** you are using `getApplicationContext()`. You never need it for GUI work, and it is frequently the wrong answer with GUI work, as it does not seem to always handle resources well. – CommonsWare Jan 30 '12 at 12:25
  • The code had a small mistake that in `buildAdapter()` on quering I was not doing `c.movetofirst()`.Now its working perfextly without `ViewPager`.But completely messed up with `ViewPager`.I am absalutely clueless how to get `MergeAdapter` working in `ViewPager`.The scrolling is also completely messed up. – Rajul Jan 30 '12 at 16:41
  • Does anyone have an Idea how to use `MergeAdapter` in `ViewPager`.Is setting the adapter inside the pager causing the problem?? – Rajul Jan 31 '12 at 14:13
  • Please stop using `MergeAdapter` for the moment. Go with a **single ordinary adapter** in your `ListView`. Go with a **single ordinary** `PagerAdapter` in your `ViewPager`. Get all of that working. Then, **and only then**, do you start blending in other stuff like `MergeAdapter` into your `ListViews`. – CommonsWare Jan 31 '12 at 14:29
  • The thing is Sir that the `ListView` is working just fine with a single `SimpleCursorAdapter` and the `MergeAdapter` is working fine without a `ViewPager`.Something is getting screwed with even a single view in `PageAdapter`.The list seems to get screwed when scrolling even disappearing while scrolling.It appears to be fine when the activity starts up. – Rajul Jan 31 '12 at 17:42
  • You can look at the source to `MergeAdapter`. There is very little to it. And, if you can conclusively find a bug, or you can create a *simple* project that demonstrates a possible bug, file an issue on the project's GitHub repo. – CommonsWare Jan 31 '12 at 18:05

0 Answers0