我一直试图在我的主页上设置适配器,但无法通过 .setAdapter 让它工作,它总是出现 Cannot resolve symbol 'setAdapter'
我试图改变进口自:
implementation 'androidx.recyclerview:recyclerview:1.0.0'
到:
implementation 'com.android.support:recyclerview-v7:28.0.0'
但似乎都没有用
@Override
protected void onCreate(Bundle savedInstanceState) {
rv = findViewById(R.id.rvtop);
RecyclerView.LayoutManager mRv;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feiticos);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
DatabaseHandler databaseHandler = new DatabaseHandler(this);
mRv = new GridLayoutManager(this.getBaseContext(),1);
Context context;
DatabaseHandler banco = new DatabaseHandler(this);
SQLiteDatabase db = banco.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from feiticos", new String[]{});
cursor.moveToFirst();
do {
spell_nome.add(cursor.getString(1));
spell_nivel.add(cursor.getString(2));
spell_cast_time.add(cursor.getString(3));
spell_range.add(cursor.getString(4));
spell_duration.add(cursor.getString(5));
spell_classe.add(cursor.getString(6));
}
while (cursor.moveToNext());
}
FeiticosRecycler adapter = new FeiticosRecycler(this,spell_nome,spell_nivel,spell_cast_time,spell_range,spell_duration,spell_classe);
rv.setAdapter(adapter);
我希望让 setAdapter 开始工作,因为我已经在另一个项目中做过一次,我希望它再次工作,同样在另一个项目中,我使用的是 android.support:recyclerview-v7:28.0。 0
而不是 androidx.recyclerview:recyclerview:1.0.0
最佳答案
在设置内容 View 之前,您正在使用 findViewById 查找 recyclerview 的 View 。这就是为什么它找不到所需的 recyclerview View 。
将这些行更改为此顺序
super.onCreate(savedInstanceState); setContentView(R.layout.activity_feiticos) ;
rv = findViewById(R.id.rvtop);
RecyclerView.LayoutManager mRv;
关于java - 无法在 recyclerview 上设置适配器,错误 - 无法解析 'setAdapter',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57762175/