2016-11-08 14 views
1

Sample Image
上記のタイプのRecyclerView(私の場合)を作成したいと思います。上のサンプル写真では、費用はゼブラ線で赤色で、収入は緑色で表示されます。私は経費表の値のコードを書いて、RecyclerViewに正常に表示しました。今私は収入表から収入を見せて、サンプル写真と同じように見たいと思います。親切に助けてください。消費値と異なる色の値を持つ1つのRecyclerViewに2つのテーブルを追加する方法

RecyclerView:

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

データベース:

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

答えて

0

必要なのは、あなたがrecyclerview内の複数のビューを膨らませることができますgetItemViewTypeです。

例:thisまたはthis

+0

ありがとうございました。 – Zohaib

+0

これは本当にこれを行う唯一の方法です。アダプタでif elseを使用しないでください。 – kgandroid

+0

ありがとう。私は試して戻ってくるだろう。 – Zohaib

0

が、それは広大であるかのように知らせ、あなたに情報を提供するタグの使用を参照してください。

ハッシュマップにそのタグを追加します。

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

であり、それに応じてhashView値とsetTextColorを取得します。

+0

支出変数に価値を与える方法は? – Zohaib

+0

どのようにデータを取得しますか、それはサーバから来ていますか? –

+0

いいえ、ローカルデータベース – Zohaib

関連する問題