2016-03-02 150 views
2

我想添加卡片視圖與回收站視圖。但它沒有顯示任何內容,只是一個空白的視圖。卡片視圖不可見

我在這裏看到了這方面的問題,它有回答爲回收者視圖和卡視圖添加依賴關係。這是在我的gradle中。另一種是用getItemCount這樣的:

@Override 
    public int getItemCount() { 
     return mImage.length; 
    } 

這也是我認爲是對的我已經使用則爲list.size(); 我不明白髮生了什麼事。我想從數據庫中檢索數據,可能會出錯。任何人都可以請幫忙。

TableListAdapter

public class TableListAdapter extends RecyclerView.Adapter<TableListAdapter.TableViewHolder> { 

    TimeTableHelper db; 

    private List<TimeTable> List; 

    public TableListAdapter(List<TimeTable> List) { 
      this.List = List; 
    } 

    @Override 
    public int getItemCount() { 
      return List.size(); 
    } 

    @Override 
    public void onBindViewHolder(TableViewHolder contactViewHolder, int i) { 

     TimeTable table = db.getTables(i); 
     TimeTable ci = List.get(i); 
     contactViewHolder.tableTitle.setText(table.getTitle()); 
    } 

    @Override 
    public TableViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
     View itemView = LayoutInflater. 
        from(viewGroup.getContext()). 
        inflate(R.layout.table_card, viewGroup, false); 

     return new TableViewHolder(itemView); 
    } 

    public static class TableViewHolder extends RecyclerView.ViewHolder { 

     protected TextView tableTitle; 
     protected CardView cv; 
     protected Switch aSwitch; 


     public TableViewHolder(View v) { 
      super(v); 
      tableTitle = (TextView) v.findViewById(R.id.tableTitle); 
      cv = (CardView) v.findViewById(R.id.card_view); 
      aSwitch = (Switch) v.findViewById(R.id.switch2); 
     } 
    } 
} 

TimeTableList活動

public class TimeTableList extends AppCompatActivity { 

    private RecyclerView recyclerView; 
    public ArrayList<TimeTable> items; 
    public TableListAdapter adapter; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_time_table_list); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setTitle("Time Table"); 
     toolbar.setTitleTextColor(Color.parseColor("#FFFFFF")); 

     toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp); 
     setSupportActionBar(toolbar); 
     toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       onBackPressed(); 
      } 
     }); 

     recyclerView = (RecyclerView)findViewById(R.id.RecyclerView); 
     LinearLayoutManager llm = new LinearLayoutManager(this); 

     items=new ArrayList<>(); 
     recyclerView.setLayoutManager(llm); 

     recyclerView.setHasFixedSize(true); 

     initRecyclerAdapter(); 

     ImageButton fab = (ImageButton) findViewById(R.id.imgbtn_fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent i = new Intent(getApplicationContext(),NewTimeTable.class); 
       startActivity(i); 
      } 
     }); 
    } 

    private void initRecyclerAdapter() { 
     adapter = new TableListAdapter(items); 
     recyclerView.setAdapter(adapter); 
    } 
} 

列表XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:fitsSystemWindows="true" 
    tools:context="com.example.siddhi.timetablelayout.activities.TimeTableList"> 

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" 
     android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay" 
     android:id="@+id/view5"> 

     <android.support.v7.widget.Toolbar android:id="@+id/toolbar" 
      android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" 
      app:titleTextAppearance="@style/Toolbar.TitleText"/> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v7.widget.RecyclerView 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:id="@+id/RecyclerView" 
     android:background="@android:color/white" 
     android:layout_below="@+id/view5" /> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_gravity="bottom|end"> 

     <include layout="@layout/myfab"/> 
    </LinearLayout> 
</RelativeLayout> 

table_card XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="1" 
    android:orientation="vertical" 
    android:measureWithLargestChild="false"> 
    <!-- A CardView that contains a TextView --> 

    <android.support.v7.widget.CardView 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/card_view" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     card_view:cardCornerRadius="4dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:color/white"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_marginLeft="20dp" 
       android:textAppearance="@android:style/TextAppearance.Medium" 
       android:id="@+id/tableTitle" /> 

      <Switch 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/switch2" 
       android:layout_centerVertical="true" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentEnd="true" 
       android:layout_marginRight="20dp" /> 


     </RelativeLayout> 

     </android.support.v7.widget.CardView> 

</LinearLayout> 

搖籃:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion '23.0.1' 

    defaultConfig { 
     applicationId "com.example.siddhi.timetablelayout" 
     minSdkVersion 16 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    compile 'com.android.support:design:23.1.1' 
    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.android.support:cardview-v7:23.1.1' 
    compile 'com.android.support:recyclerview-v7:23.1.1' 
    compile 'com.google.android.gms:play-services:8.3.0' 
    compile 'com.google.android.gms:play-services-wearable:8.3.0' 
    compile 'com.google.android.gms:play-services-maps:7.5.0' 
    compile 'com.google.android.support:wearable:1.3.0' 
} 

TimeTableHelper:

public class TimeTableHelper extends SQLiteOpenHelper{ 


    private static final String TABLE_TIME_TABLE = "timetables"; 
    private static final String KEY_TABLE_TITLE = "tabletitle"; 
    private static final String KEY_STATUS = "status"; 
    private static final String KEY_TABLE_ID = "tableid"; 
    private static final String KEY_TABLE_COLOR = "tablecolor"; 

    public TimeTableHelper(Context context) { 
     super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION); 

    } 


    @Override 
    public void onCreate(SQLiteDatabase db) { 
    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

     db.execSQL("DROP TABLE IF EXISTS " + TABLE_TIME_TABLE); 

     // createTable(db); 
     // onCreate(db); 
    } 
    public void addTimeTable(TimeTable table) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 

     values.put(KEY_TABLE_TITLE, table.getTitle()); 
     values.put(KEY_STATUS, table.getStatus()); 
     values.put(KEY_TABLE_COLOR, table.getTableColor()); 

     db.insert(TABLE_TIME_TABLE, null, values); 

     db.close(); 
    } 

    public TimeTable getTables(int id) { 

     SQLiteDatabase db = this.getReadableDatabase(); 

     Cursor cursor = db.query(TABLE_TIME_TABLE, new String[] { KEY_TABLE_ID, 
         KEY_TABLE_TITLE, KEY_TABLE_COLOR ,KEY_STATUS}, KEY_TABLE_ID + "=?", 
       new String[] { String.valueOf(id) }, null, null, null, null); 

     cursor.moveToFirst(); 

     TimeTable table = new TimeTable(Integer.parseInt(cursor.getString(0)), 
       cursor.getString(1), Integer.parseInt(cursor.getString(2)),Integer.parseInt(cursor.getString(3))); 

     return table; 

    } 

    public List<TimeTable> getAllTables() { 
     List<TimeTable> conList = new ArrayList<TimeTable>(); 

     String selectQuery = "SELECT * FROM " + TABLE_TIME_TABLE; 

     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     if (cursor.moveToFirst()) { 
      do { 

       TimeTable table = new TimeTable(); 

       table.setId(Integer.parseInt(cursor.getString(0))); 
       table.setTitle(cursor.getString(1)); 
       table.setStatus(cursor.getInt(2)); 
       table.setTableColor(Integer.parseInt(cursor.getString(3))); 

       conList.add(table); 
      } while (cursor.moveToNext()); 
     } 

     return conList; 
    } 
} 

創建表:

String CREATE_TIME_TABLE = "CREATE TABLE " + TABLE_TIME_TABLE + "(" 
      + KEY_TABLE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
      + KEY_TABLE_TITLE + " TEXT," 
      + KEY_STATUS + " TEXT," 
      + KEY_TABLE_COLOR + " TEXT" + ")"; 

    db.execSQL(CREATE_TIME_TABLE); 

謝謝。

+0

'私人列表清單;'只是從我身邊有輕微強迫症的問題,但請不要使用Pascal大小寫命名變量名,唯一的類名應該有名字以大寫字母 – Bhargav

+0

開頭在'onCreateViewHolder()'中,您正在爲'R.layout.table_card'充氣,但您已在此發佈'card.xml'代碼,您是否也可以在此處發佈'table_card.xml'文件的代碼 – Bhargav

+0

只有table_card。對不起,我會編輯名稱。@ Bhargav – Sid

回答

2

你沒有添加的內容調用它,因爲我可以在你的代碼中看到的,這就是爲什麼它顯示空白因爲當時的清單大小爲ZERO

您剛剛初始化您的列表並設置適配器。在初始化適配器之前,在列表中添加一些數據。

您的代碼

recyclerView = (RecyclerView)findViewById(R.id.RecyclerView); 
     LinearLayoutManager llm = new LinearLayoutManager(this); 

     items=new ArrayList<>(); // **Add data to list after initialization** 
     recyclerView.setLayoutManager(llm); 

     recyclerView.setHasFixedSize(true); 

     initRecyclerAdapter(); 

     ImageButton fab = (ImageButton) findViewById(R.id.imgbtn_fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent i = new Intent(getApplicationContext(),NewTimeTable.class); 
       startActivity(i); 
      } 
     }); 
+0

我改變了這個timeTableHelper = new TimeTableHelper(this); items = new ArrayList (timeTableHelper.getAllTables()); ,,,,工作...比你.. @ Anuj Sharma – Sid

+0

乾杯!快樂編碼:) –

1

從我所看到的,要傳遞一個空列表進入TimeTableList(活動)的TableListAdapter。 由於在創建適配器期間列表大小爲0,因此不會爲RecyclerView創建任何cardView。

要麼你應該添加一些數據點到列表中創建實例的TableListAdapter

OR

TableListAdapter有一個公共無效updateRecyclerAdapter(名單itemList中),你可以調用後創建適配器的實例。 該方法可以可能類似於下面的代碼

public void updateAdapaterList(List<TimeTable> newTimeTableList) { 
    //Replace the current list with new list 
    this.List = newTimeTableList; 
    //notify the adapter that the data set has changed 
    notifyDataSetChanged(); 
} 

ALSO

確保你重寫getItemCount()方法在RecyclerAdapter如下。

@Override 
public int getItemCount() { 
    return List.size(); 
} 

而且我會強烈建議使用合適的駝峯規則而編碼。:)

+0

我試過使用updateRecyclerAdapter,但沒有工作.. @ Vaibhav Singhal – Sid

+0

你重寫了getItemCount()方法來返回timeTableList的大小嗎? –

+0

是的,我已經做了.. @Override public int getItemCount(){ return tableList.size(); } – Sid

2

從你的代碼我所看到的就是你忘了的數據點添加到items變量

只要改變你的活動代碼到這一點,在這裏我也做了改變,通過添加給出@VaibhavSinghal功能更新您的回收觀點適配器,然後通過傳遞你的列表中,您從TimeTableHelper.getAllTables(獲取列表)

public class TimeTableList extends AppCompatActivity { 

    private RecyclerView recyclerView; 
    public ArrayList<TimeTable> items; 
    public TableListAdapter adapter; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_time_table_list); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setTitle("Time Table"); 
     toolbar.setTitleTextColor(Color.parseColor("#FFFFFF")); 

     toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp); 
     setSupportActionBar(toolbar); 
     toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       onBackPressed(); 
      } 
     }); 

     recyclerView = (RecyclerView)findViewById(R.id.RecyclerView); 
     LinearLayoutManager llm = new LinearLayoutManager(this); 

     items=new ArrayList<>(); 
     recyclerView.setLayoutManager(llm); 

     recyclerView.setHasFixedSize(true); 

     initRecyclerAdapter(); 
     // Get reference to your time tableHelper 
     TimeTableHelper timeTableHelper = new TimeTableHelper(this); 
     // Update your adapter with elements returned by getAllTables() function 
     updateAdapterList(timeTableHelper.getAllTables()); 

     ImageButton fab = (ImageButton) findViewById(R.id.imgbtn_fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent i = new Intent(getApplicationContext(),NewTimeTable.class); 
       startActivity(i); 
      } 
     }); 
    } 

    private void initRecyclerAdapter() { 
     adapter = new TableListAdapter(items); 
     recyclerView.setAdapter(adapter); 
    } 

    public void updateAdapaterList(List<TimeTable> newTimeTableList) { 
     //Replace the current list with new list 
     this.List = newTimeTableList; 
     //notify the adapter that the data set has changed 
     notifyDataSetChanged(); 
    } 


} 
+1

謝謝你..總結所有的答案和工作.. :-) @Bhargav – Sid