Recycler View不显示任何内容

时间:2017-11-09 11:47:39

标签: java android sqlite

这些是我所做的课程

logcat的

11-09 11:34:49.183 27661-27661/? I/art: Late-enabling -Xcheck:jni
11-09 11:34:49.193 27661-27661/? D/TidaProvider: TidaProvider()
11-09 11:34:49.197 27661-27661/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@ce504
11-09 11:34:49.348 27661-27661/nischayvaish.com.test1 W/System: ClassLoader referenced unknown path: /data/app/nischayvaish.com.test1-1/lib/arm64
11-09 11:34:49.360 27661-27661/nischayvaish.com.test1 I/InstantRun: starting instant run server: is main process
11-09 11:34:49.463 27661-27661/nischayvaish.com.test1 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-09 11:34:49.484 27661-27661/nischayvaish.com.test1 D/AccessibilityManager: current package=nischayvaish.com.test1, accessibility manager mIsFinalEnabled=false, mOptimizeEnabled=true, mIsUiAutomationEnabled=false, mIsInterestedPackage=false
11-09 11:34:49.512 27661-27661/nischayvaish.com.test1 V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@d00846
11-09 11:34:49.512 27661-27661/nischayvaish.com.test1 V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@73d0a07
11-09 11:34:49.593 27661-27661/nischayvaish.com.test1 V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@ed682f7
11-09 11:34:49.593 27661-27661/nischayvaish.com.test1 V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@f473564
11-09 11:34:49.647 27661-27661/nischayvaish.com.test1 E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so
11-09 11:34:49.647 27661-27661/nischayvaish.com.test1 E/HAL: PATH2 /vendor/lib64/hw/gralloc.qcom.so
11-09 11:34:49.647 27661-27661/nischayvaish.com.test1 E/HAL: PATH1 /system/lib64/hw/gralloc.qcom.so
11-09 11:34:49.647 27661-27661/nischayvaish.com.test1 E/HAL: PATH3 /odm/lib64/hw/gralloc.msm8953.so
11-09 11:34:49.647 27661-27661/nischayvaish.com.test1 E/HAL: PATH2 /vendor/lib64/hw/gralloc.msm8953.so
11-09 11:34:49.647 27661-27661/nischayvaish.com.test1 E/HAL: PATH1 /system/lib64/hw/gralloc.msm8953.so
11-09 11:34:49.654 27661-27661/nischayvaish.com.test1 D/ActivityThreadInjector: clearCachedDrawables.
11-09 11:34:49.673 27661-27691/nischayvaish.com.test1 I/Adreno: QUALCOMM build                   : 01d2d27, I3d52eaf367
                                                                Build Date                       : 12/10/16
                                                                OpenGL ES Shader Compiler Version: XE031.09.00.03
                                                                Local Branch                     : 
                                                                Remote Branch                    : 
                                                                Remote Branch                    : 
                                                                Reconstruct Branch               : 
11-09 11:34:49.679 27661-27691/nischayvaish.com.test1 I/OpenGLRenderer: Initialized EGL, version 1.4
11-09 11:34:49.679 27661-27691/nischayvaish.com.test1 D/OpenGLRenderer: Swap behavior 1
11-09 11:34:49.706 27661-27661/nischayvaish.com.test1 W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView

这是我的 mainactivity

public class MainActivity extends AppCompatActivity {
private TextView tv1;
private RecyclerView recycler_view;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<IncomingNumber> arrayList = new ArrayList<>();
private RecyclerAdapter adapter;
private BroadcastReceiver broadcastReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1 = (TextView) findViewById(R.id.emptyTxt);
    recycler_view = (RecyclerView) findViewById(R.id.recyclerView);
    layoutManager = new LinearLayoutManager(this);
    adapter = new RecyclerAdapter(arrayList);
    recycler_view.setLayoutManager(layoutManager);
    recycler_view.setHasFixedSize(true);
    recycler_view.setAdapter(adapter);
    readFromDb();
    broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            readFromDb();
        }
    };

}

private void readFromDb() {
    arrayList.clear();
    DbHelper dbHelper = new DbHelper(this);
    SQLiteDatabase database = dbHelper.getWritableDatabase();
    Cursor cursor = dbHelper.readNumber(database);
    if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            String number;
            int id;
            number = cursor.getString(cursor.getColumnIndex(DbContract.INCOMING_NUMBER));
            id = cursor.getInt(cursor.getColumnIndex("id"));
            arrayList.add(new IncomingNumber(id, number));
        }
        cursor.close();
        dbHelper.close();
        adapter.notifyDataSetChanged();
        recycler_view.setVisibility(View.VISIBLE);
        tv1.setVisibility(View.GONE);
    }

}

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(broadcastReceiver, new IntentFilter(DbContract.UPDATE_UI_FILTER));
    unregisterReceiver(broadcastReceiver);
}

DbHelper.class

private static final String DATABASE_NAME = "numberDb";
private static final int DATABASE_VERSION = 2;
private static final String CREATE = "CREATE TABLE " + DbContract.TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," + DbContract.INCOMING_NUMBER + " TEXT);";
private static final String DROP_TABLE = "drop table if exists " + DbContract.TABLE_NAME;

public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE);

}

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

public void saveNumber(String number, SQLiteDatabase database) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(DbContract.INCOMING_NUMBER, number);
    database.insert(DbContract.TABLE_NAME, null, contentValues);
}

public Cursor readNumber(SQLiteDatabase database) {
    String[] projection = {"id", DbContract.INCOMING_NUMBER};
    return (database.query(DbContract.TABLE_NAME, projection, null, null, null, null, null));
}

在编译代码时,它已编译但未显示任何来电号码。 目前我是Android初学者所以我没有正确理解错误。 应用程序在编译时发出警告,

  

在Android 4.1之前,方法int   android.support.v7.widget.ListViewCompat.lookForSelectablePosition(INT,   boolean)会错误地重写package-private方法   在android.widget.ListView

允许电话许可。

1 个答案:

答案 0 :(得分:0)

无法写评论,因为我的声誉,但尝试在readFromDb()和Toast适配器计数后的Toast数组大小和recyclerview childCount检查它是否真的将数据添加到适配器和recyclerview

并尝试删除recycler_view.setHasFixedSize(true);并让我知道它是否有效