我尝试在外部数据库的recyclerview中吃午餐。所以我准备了mydatabase.db.zip并将其放入资产/数据库文件夹中。在编写代码后,我发现了一个错误NullPointerException.i我不知道问题出在哪里。请帮助
数据模型:
package com.example.myapplication.DataModel;
public class Name {
public int id;
public String name;
public String meaning;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMeaning() {
return meaning;
}
public void setMeaning(String meaning) {
this.meaning = meaning;
}
}
MyDatabase.java
package com.example.myapplication;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class MyDatabase extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "niniaad.db";
private static final int DATABASE_VERSION = 1;
private static final String TBL_NAME="names";
private static final String COL_ID="id";
private static final String COL_NAME="name";
private static final String COL_MEAN="mean";
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getData(){
SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
Cursor cursor=sqLiteDatabase.rawQuery("SELECT * FROM "+TBL_NAME,null);
return cursor;
}
}
SecondRecycler.java
package com.example.myapplication;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.RelativeLayout;
import com.example.myapplication.Adapter.NameAdapter;
import com.example.myapplication.Adapter.StudentAdapter;
import com.example.myapplication.DataModel.Name;
import com.example.myapplication.DataModel.Students;
import java.util.ArrayList;
import java.util.List;
public class SecondRecycler extends AppCompatActivity {
RecyclerView recyclerView;
List<Name> nameList;
MyDatabase myDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_recycler);
setupviews();
getDataFromSqlite();
}
private void getDataFromSqlite() {
Cursor cursor= myDatabase.getData();
for (cursor.moveToFirst(); !cursor.isAfterLast() ; cursor.moveToNext()) {
Name name=new Name();
name.setName(cursor.getString(0));
name.setMeaning(cursor.getString(1));
nameList.add(name);
}
recyclerView.setAdapter(new NameAdapter(SecondRecycler.this,nameList));
}
private void setupviews() {
MyDatabase myDatabase=new MyDatabase(this);
recyclerView = (RecyclerView) findViewById(R.id.rv_secondrecycler);
recyclerView.setLayoutManager(new LinearLayoutManager(SecondRecycler.this));
}
}
适配器
package com.example.myapplication.Adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.myapplication.DataModel.Name;
import com.example.myapplication.R;
import java.util.List;
public class NameAdapter extends RecyclerView.Adapter<NameAdapter.NameViewHolder> {
public Context context;
public List<Name> nameList;
public NameAdapter(Context context, List<Name> nameList) {
this.context = context;
this.nameList = nameList;
}
@NonNull
@Override
public NameViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.secondrecycler_row, viewGroup, false);
return new NameViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull NameViewHolder nameViewHolder, int position) {
Name name = nameList.get(position);
nameViewHolder.txtName.setText(name.getName());
nameViewHolder.txtMean.setText(name.getMeaning());
}
@Override
public int getItemCount() {
return nameList.size();
}
public class NameViewHolder extends RecyclerView.ViewHolder {
TextView txtName, txtMean;
public NameViewHolder(@NonNull View itemView) {
super(itemView);
txtName = (TextView) itemView.findViewById(R.id.txt_name_Secondrecycler);
txtMean = (TextView) itemView.findViewById(R.id.txt_mean_seconrecycler);
}
}
}
activity_second_recycler.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"
tools:context=".SecondRecycler">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_secondrecycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
recycler_row.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="wrap_content"
android:layout_margin="10dp"
tools:context=".SecondRecycler">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txt_name_Secondrecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#000"
tools:text="name"
android:textAlignment="center"
/>
<TextView
android:layout_below="@id/txt_name_Secondrecycler"
android:id="@+id/txt_mean_seconrecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#FF0000"
tools:text="mean"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>