卡视图不可见

席德

我正在尝试将卡片视图与回收者视图一起添加。但是它没有显示任何内容,只是一个空白视图。

我在这里看到了有关此问题的信息,它提供了为回收者视图和卡片视图添加依赖项的答案。这存在于我的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);
      }
  }
}

时间表列表活动

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

时间表助手:

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

谢谢..

阿努·夏尔马(Anuj Sharma)

您没有在列表中添加内容,正如我在您的代码中看到的那样,这就是为什么它显示为空白的原因,因为列表大小当时为零

您只需初始化列表并设置适配器即可。在初始化适配器之前,在列表中添加一些数据。

您的验证码

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

卡视图不可见

来自分类Dev

卡视图不可见

来自分类Dev

自定义视图不可见

来自分类Dev

XIB子视图不可见

来自分类Dev

CAGradientLayer导致子视图不可见

来自分类Dev

CAGradientLayer导致子视图不可见

来自分类Dev

相对布局中不可见的视图

来自分类Dev

CoordinatorLayout 在设计视图中不可见

来自分类Dev

文档在嵌入视图中不可见

来自分类Dev

Primefaces-选项卡不可见

来自分类Dev

Primefaces-选项卡不可见

来自分类Dev

APEX:选项卡容器中的图表不可见

来自分类Dev

选项卡图标在离子 3 中不可见

来自分类Dev

当前不可见时展开/选择树视图节点

来自分类Dev

Spring Webflow requestScope变量在视图中不可见

来自分类Dev

Android是否会重绘不可见的视图?

来自分类Dev

滚动视图内容在底部栏不可见

来自分类Dev

如何使一组视图在android中不可见

来自分类Dev

Radasync控制选择按钮在兼容视图中不可见

来自分类Dev

图片在列表视图中不可见

来自分类Dev

Android是否会重绘不可见的视图?

来自分类Dev

Spring Webflow requestScope变量在视图中不可见

来自分类Dev

如何更新不可见/屏幕外的视图?

来自分类Dev

oracle 12c视图中的不可见列

来自分类Dev

列表视图无法正确填充/不可见

来自分类Dev

Android-不应显示的视图不可见(旧设备)

来自分类Dev

如何使一组视图在android中不可见

来自分类Dev

创建可快速交互的不可见视图

来自分类Dev

如何在设计器中显示不可见的视图?