我正在使用Android Studio,并且在我的项目的DB SQLite上有一个表,其中包含3条记录。
public class BaseDeDatos extends SQLiteOpenHelper {
public BaseDeDatos(Context context, String s, Context applicationContext, int i) {
super(context, "verbos.db", null, 10);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table verbos(id integer, verbos text, referencia text, eu text, voce text, nos text)");
db.execSQL("insert into verbos values(1, 'agito', 'abalo', 'abala', 'abalamos', 'abalam')," +
"(1, 'agito', 'abalava', 'abalava', 'abalávamos', 'abalavam')," +
"(1, 'agito', 'abalei', 'abalou', 'abalamos', 'abalaram')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS verbos");
db.execSQL("create table verbos(id integer, verbos text, referencia text, eu text, voce text, nos text)");
db.execSQL("insert into verbos values(1, 'agito', 'abalo', 'abala', 'abalamos', 'abalam')," +
"(1, 'agito', 'abalava', 'abalava', 'abalávamos', 'abalavam')," +
"(1, 'agito', 'abalei', 'abalou', 'abalamos', 'abalaram')");
}
}
这是我正在工作的“ Verbos2”活动:
public class Verbos2 extends AppCompatActivity {
ArrayList<Item> listaItem;
RecyclerView recyclerViewItem;
EditText etVerbos;
Button mostrar;
BaseDeDatos conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verbos2);
etVerbos = findViewById(R.id.etIngresar);
mostrar = findViewById(R.id.bnMostrar);
conn = new BaseDeDatos(getApplicationContext(),"verbos.db",null,10);
listaItem = new ArrayList<>();
recyclerViewItem=findViewById(R.id.rvListItems);
recyclerViewItem.setLayoutManager(new LinearLayoutManager(this));
mostrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
consultarListaItems();
RecyclerViewAdapter adapter = new RecyclerViewAdapter(listaItem);
recyclerViewItem.setAdapter(adapter);
}
private void consultarListaItems(){
SQLiteDatabase db = conn.getReadableDatabase();
Item item = null;
String[] parametros = {etVerbos.getText().toString()};
Cursor cursor=db.rawQuery("select * from verbos WHERE verbos =?", parametros);
while (cursor.moveToNext()){
item = new Item();
item.setVerbo(cursor.getString(0));
item.setReferencia(cursor.getString(1));
item.setEu(cursor.getString(2));
listaItem.add(item);
}
}
});
}
}
当我在EditText中设置单词“ agito”时,我试图用SQLite数据库中的数据加载Recycler View((它应该带给我数据库中3条记录的信息,因为字段“ verbo”在所有记录中),但是当我编写并单击该按钮时,什么也没有发生,并且控制台将显示以下消息:
“ E / RecyclerView:未连接适配器;正在跳过布局”
这是recyclerview布局的图片,可以更好地理解我:
这是我的“回收者视图”活动:
public class RecyclerViewAdapter extends RecyclerView.Adapter {
private ArrayList<Item> listItem ;
public RecyclerViewAdapter(ArrayList<Item> listItem) {
this.listItem = listItem;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View contentView = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista, null);
System.out.println("CREATE VIEW HOLDER: " + viewType);
return new Holder(contentView);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Item item = listItem.get(position);
Holder Holder = (Holder) holder;
Holder.tvVerbo.setText(item.getVerbo());
Holder.tvReferencia.setText(item.getReferencia());
Holder.tvEu.setText(item.getEu());
System.out.println("BIND VIEW HOLDER: " + position);
}
@Override
public int getItemCount() {
return listItem.size();
}
public class Holder extends RecyclerView.ViewHolder{
TextView tvVerbo;
TextView tvReferencia;
TextView tvEu;
public Holder(View itemView) {
super(itemView);
tvVerbo = itemView.findViewById(R.id.tvLista1);
tvReferencia = itemView.findViewById(R.id.tvLista2);
tvEu = itemView.findViewById(R.id.tvLista3);
}
}
}
除了错误,我不确定在“ Verbos2”中我是否正在做正确的事情,以便从SQLite数据库中获取数据并将其加载到我的Recycler View中。
如果您能帮助我,我会非常感激。
您正在代码中的某处使用context.getSystemService。您正在使用的上下文为null。
尝试将attachToRoot
中的onCreateViewHolder
设置为false
将是:
View contentView = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista, false);