如何在android studio中使用recyclerview显示sqllite插入数据

时间:2019-12-09 18:04:10

标签: android sqlite android-recyclerview

我是android新手。 当我插入数据时,它可以正常工作。但是,当我单击用于使用recyclerview显示插入的数据的按钮时,会出现以下错误消息:Sqlite插入和视图已停止。

enter image description here

enter image description here

错误消息如下:

  1. 在 com.example.sqliteinsertandview.MainActivity.ShowData(MainActivity.java:56)         在java.lang.reflect.Method.invoke(本机方法)         在androidx.appcompat.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)         在android.view.View.performClick(View.java:6256)         在android.view.View $ PerformClick.run(View.java:24701)         在android.os.Handler.handleCallback(Handler.java:789)         在android.os.Handler.dispatchMessage(Handler.java:98)         在android.os.Looper.loop(Looper.java:164)         在android.app.ActivityThread.main(ActivityThread.java:6541)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

MainActivity代码:

public class MainActivity extends AppCompatActivity {

    private EditText nameEt, ageEt;
    private Button insertBtn;
    String name, age;
    DatabaseHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        insertData();


    }

    private void insertData() {
        insertBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                name = nameEt.getText().toString();
                age = ageEt.getText().toString();

                long id = helper.insertData(name, age);
                Toast.makeText(MainActivity.this, "Your id" + id, Toast.LENGTH_SHORT).show();

            }
        });

    }

    private void init() {

        nameEt = findViewById(R.id.nameEt);
        ageEt = findViewById(R.id.ageEt);
        insertBtn = findViewById(R.id.insertBtn);
        helper = new DatabaseHelper(this);

    }

    public void ShowData(View view) {
        startActivity(new Intent(this, ShowActivity.class));

    }
}

ShowActivity代码:

public class ShowActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    UserAdapter adapter;
    List<User> users;
    DatabaseHelper helper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);

        init();

        getData();
    }

    private void getData(){

        Cursor cursor = helper.showData();
        while (cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex(helper.COL_ID));
            String name = cursor.getString(cursor.getColumnIndex(helper.COL_NAME));
            String age = cursor.getString(cursor.getColumnIndex(helper.COL_AGE));

            users.add(new User(id,name,age));
            adapter.notifyDataSetChanged();
        }

    }

    private void init(){

        recyclerView = findViewById(R.id.userRecyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        users = new ArrayList<>();
        helper = new DatabaseHelper(this);
        adapter = new UserAdapter(users);
        recyclerView.setAdapter(adapter);
    }

DatabaseHelper代码:

public class DatabaseHelper extends SQLiteOpenHelper {

    private static String DATABASE_NAME = "User.db";
    private static String TABLE_NAME = "User";
    public static String COL_ID = "Id";
    public static String COL_NAME = "Name";
    public static String COL_AGE = "Age";
    private static  int VERSION = 1;
    private static String createTable = "create table "+TABLE_NAME+"(Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Age TEXT)";

    public DatabaseHelper(@Nullable Context context) {
        super(context, DATABASE_NAME, null, VERSION);
    }

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

    }

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

    }

    long insertData(String name, String age){
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_NAME,name);
        contentValues.put(COL_AGE,age);
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        long id = sqLiteDatabase.insert(TABLE_NAME,null,contentValues);
        sqLiteDatabase.close();

        return id;

    }

    public Cursor showData(){
        String show_all = " select* From "+TABLE_NAME;
        SQLiteDatabase sqLiteDatabase = getReadableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery(show_all,null);
        return cursor;
    }
}

1 个答案:

答案 0 :(得分:0)

您没有在ShowActivity文件中添加AndroidManifest.xml类。将此添加到application标记内

<activity android:name=".ShowActivity"/>