Java Android-ArrayList和RecycleView.Adapter
我的和弦反射像这样:Java Android-ArrayList和RecycleView.Adapter,java,android,android-recyclerview,adapter,Java,Android,Android Recyclerview,Adapter,我的和弦反射像这样: public class ChordsListActivity extends Activity { private RecyclerView chordsList; private RecyclerView.LayoutManager listLayoutManager; private ArrayList<Accordo> getChords() { ArrayList<Accordo> chords = new ArrayList&l
public class ChordsListActivity extends Activity {
private RecyclerView chordsList;
private RecyclerView.LayoutManager listLayoutManager;
private ArrayList<Accordo> getChords() {
ArrayList<Accordo> chords = new ArrayList<>();
Accordo a=new Accordo();
a.setName("Do maggiore");
a.setNote("Do, Mi, Sol");
a.setImage(R.drawable.do_maggiore);
chords.add(a);
a=new Accordo();
a.setName("do 5");
a.setNote("na, na, na");
a.setImage(R.drawable.do5);
chords.add(a);
return chords;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chords_list);
chordsList = (RecyclerView) findViewById(R.id.chords_recycler);
//use a linear layout manager
listLayoutManager = new LinearLayoutManager(this);
chordsList.setLayoutManager(listLayoutManager);
ChordsListAdapter adapter = new ChordsListAdapter(this, getChords());
/** start SearchActivity when ImageButton is pressed */
ImageButton cerca = (ImageButton) findViewById(R.id.search);
cerca.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
ArrayList<Accordo> chords = new ArrayList<Accordo>();
intent.putExtra("chords", chords);
startActivity(intent);
}
});
}
}
ChordsListAdapter adapter = new ChordsListAdapter(getChords());
这就是错误:
错误:126,38错误:类ChordListAdapter中的构造函数ChordListAdapter不能应用于给定类型;
必需:ArrayList
发现:弦线抑制,ArrayList
原因:实际参数列表和正式参数列表长度不同
这是我的ChordsListAdapter:
你能帮我找出问题所在以及解决方法吗?你只有一个构造函数
public ChordsListAdapter(ArrayList<Accordo> chordsList) {
this.chordsList = chordsList;
}
ChordsListAdapter adapter = new ChordsListAdapter(this, getChords());
public ChordsListAdapter(ArrayList<Accordo> chordsList) {
this.chordsList = chordsList;
}
这条线
ChordsListAdapter adapter = new ChordsListAdapter(this, getChords());
调用ChordListAdapter的构造函数,在chordListAdapter.class中如下所示:
或者您可以删除调用中的第一个参数:
ChordsListAdapter adapter = new ChordsListAdapter(getChords());
您只有一个构造函数
public ChordsListAdapter(ArrayList<Accordo> chordsList) {
this.chordsList = chordsList;
}
ChordsListAdapter adapter = new ChordsListAdapter(this, getChords());
public ChordsListAdapter(ArrayList<Accordo> chordsList) {
this.chordsList = chordsList;
}
你能发布GetChord的代码吗method@DeepanshuGandhi已经有了,它是在chordListActivity中返回ArrayList的方法。你正在传递这个,getChords两个参数,你有一个参数的构造函数谢谢!回答得好!这修复了它,即使我尝试运行它时,我的recycler视图仍然看起来是空的。你知道我如何解决这个问题吗?这似乎是另一个问题的问题,因为到目前为止我看不出你的代码有问题;可能原因:您的xml活动布局、行布局编码不正确。奇怪。。。这个错误肯定存在,因为我的类只有这两个,而且在我更改ArrayListof课程之前,我的xml布局是正常工作的。问题是我没有设置适配器。现在我把它修好了,完全看不到。很高兴您这么快就发现了错误:
ChordsListAdapter adapter = new ChordsListAdapter(getChords());
ChordsListAdapter adapter = new ChordsListAdapter(this, getChords());
public ChordsListAdapter(ArrayList<Accordo> chordsList) {
this.chordsList = chordsList;
}
ChordsListAdapter adapter = new ChordsListAdapter(getChords());