1

Sample Image
I want to make above type of RecyclerView (in my case). In above sample pic, the expenses show in red colours with zebra lines and income shows in green colours. I've write code for expense table values and successfully shown in RecyclerView. Now I want to show income from income table and make view same as sample pic. kindly help.

RecyclerView with expense values:

arrayListExpense = new ArrayList<>();
        AdapterViewItems adapter = new AdapterViewItems(MainActivity.this, arrayListExpense);
        recyclerView = (RecyclerView) findViewById(R.id.view_item_recycle_view);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(adapter);

Cursor cursor = db.selectExpense();
 cursor.moveToFirst();
        if (cursor.getCount() == 0) {
            Toast.makeText(this, "Empty list", Toast.LENGTH_SHORT).show();
        } else {
            for (int i = 0; i < cursor.getCount(); i++) {
                HashMap<String, Object> hm = new HashMap<>();
                hm.put(ID_EXPENSE, cursor.getString(cursor.getColumnIndex(ID_EXPENSE)));
                hm.put(NAME_EXPENSE, cursor.getString(cursor.getColumnIndex(NAME_EXPENSE)));

                long valueExpense = cursor.getLong(cursor.getColumnIndex(VALUE_EXPENSE));
                showExpense(valueExpense);

                hm.put(VALUE_EXPENSE, String.valueOf(valueExpense));
                hm.put(DATE_EXPENSE, Utility.dateFormat(cursor.getLong(cursor.getColumnIndex(DATE_EXPENSE))));
                String id = cursor.getString(cursor.getColumnIndex(TYPE_ID_EXPENSE));
                hm.put("type", db.selectTypeById(id));
                arrayListExpense.add(hm);
                cursor.moveToNext();
            }
            cursor.close();
        }

Database:

Cursor selectExpense() {
        Cursor cursor = null;
        try {
            SQLiteDatabase db = this.getWritableDatabase();
            cursor = db.rawQuery("SELECT " + ID_EXPENSE + "," + NAME_EXPENSE + "," + VALUE_EXPENSE
                    + "," + DATE_EXPENSE + "," + TYPE_ID_EXPENSE + " FROM " + TABLE_EXPENSE
                    + " ORDER BY " + DATE_EXPENSE + " DESC", null);
        } catch (Exception e) {
            Log.d("selectExpenses", " error " + e.getMessage());
        }

        return cursor;
    }
Nikhil
  • 3,711
  • 8
  • 32
  • 43
Zohaib
  • 189
  • 1
  • 5
  • 16

2 Answers2

0

All you need is getItemViewType which will allow you to inflate multiple views within a recyclerview.

Example : see this or this

Community
  • 1
  • 1
kgandroid
  • 5,507
  • 5
  • 39
  • 69
  • This is indeed the only way to do this.Avoid using if else in adapter. – kgandroid Nov 08 '16 at 10:37
  • I've succesuffly implements complex view by this way that first i add income object with income table values to arraylist and after expense object with expense table. The output is become like as, the expense values appeard at bottom and all the income values appeared at top of expense in RecyclerView. but i want to show it randomly same as sample pic. How to get that? – Zohaib Nov 08 '16 at 15:40
0

Use a tag that gives you information like it is expanses or inform.

add that tag in hashmap.

if(expanses){ // use variable to check expanses or income
      hm.put(INFO_EXPENSE_INCOME, "red");
}else{
       hm.put(INFO_EXPENSE_INCOME, "green");
}

and in bindViewHolder get the hashmap value and setTextColor accordingly.

Deepak Sachdeva
  • 950
  • 8
  • 16
  • how to give value to expenses variable? – Zohaib Nov 08 '16 at 10:26
  • how do you get the data, Is it coming from server ? – Deepak Sachdeva Nov 08 '16 at 10:28
  • Can you find the data is expense or income ? – Deepak Sachdeva Nov 08 '16 at 10:32
  • please see my code that describe how i get data from database and save to arrayList. I don't think so that we can differentiate is it expense or income. If you think so please show me a example or write code. – Zohaib Nov 08 '16 at 10:35
  • add a column in db to differentiate that and get that data from db same as you are doing for others. – Deepak Sachdeva Nov 08 '16 at 10:38
  • its ok but how to diffrentiate it when data went into arraylist? – Zohaib Nov 08 '16 at 10:39
  • It is very simple. As I have seen you are adding VALUE_EXPENSE, DATE_EXPENSE in hashmap and then adding hashmap in arraylist. Same you can add for expense or income also. And I think you are also getting value of VALUE_EXPENSE, DATE_EXPENSE from arraylist in bindviewholder. Same as you can fetch the data for expense or income. – Deepak Sachdeva Nov 08 '16 at 10:48
  • it will be working fine in case of one value. I put code of just expense table and add to arraylist. How can i diffrentiate that whether this package of expense or income? – Zohaib Nov 08 '16 at 10:56
  • VALUE_EXPENSE, DATE_EXPENSE are all for expense – Zohaib Nov 08 '16 at 10:56