![](/img/trans.png)
[英]Change value recycleview.adapter and save it to textview in mainactivity?
[英]Android - ArrayList and RecycleView.Adapter
我的ChordsListActivity
看起来像这样:
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(this, getChords());
这是错误:
错误:(126,38)错误:类ChordsListAdapter中的构造函数ChordsListAdapter无法应用到给定类型; 必需:找到ArrayList:ChordsListActivity,ArrayList原因:实际参数和形式参数列表的长度不同
这是我的ChordsListAdapter
:
public class ChordsListAdapter extends RecyclerView.Adapter<ChordsListAdapter.MyViewHolder> {
private ArrayList<Accordo> chordsList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView nome, note;
public ImageView immagine;
public MyViewHolder(View view) {
super(view);
nome = (TextView) view.findViewById(R.id.nome);
note = (TextView) view.findViewById(R.id.note);
immagine = (ImageView) view.findViewById(R.id.immagine_accordo);
}
}
public ChordsListAdapter(ArrayList<Accordo> chordsList) {
this.chordsList = chordsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position){
Accordo chord = chordsList.get(position);
holder.nome.setText(chord.getNome());
holder.note.setText(chord.getNote());
holder.immagine.setImageResource(chord.getImage());
}
@Override
public int getItemCount() {
return chordsList.size();
}
}
您能帮我弄清楚问题是什么以及如何解决?
您只有一个构造函数
public ChordsListAdapter(ArrayList<Accordo> chordsList) {
this.chordsList = chordsList;
}
它没有活动作为参数,因此您必须像下面这样更改创建适配器的方式:
ChordsListAdapter adapter = new ChordsListAdapter(getChords());
这条线
ChordsListAdapter adapter = new ChordsListAdapter(this, getChords());
是你的构造函数的调用ChordsListAdapter
,它看起来像这样在你ChordsListAdapter.class
:
public ChordsListAdapter(ArrayList<Accordo> chordsList) {
this.chordsList = chordsList;
}
您需要更改构造函数以接受Context
或Activity
。
public ChordsListAdapter(Context context, ArrayList<Accordo> chordsList) {
this.chordsList = chordsList;
}
或者您可以删除调用中的第一个参数:
ChordsListAdapter adapter = new ChordsListAdapter(getChords());
您只有一个构造函数
ChordsListAdapter adapter = new ChordsListAdapter(this, getChords());
public ChordsListAdapter(ArrayList<Accordo> chordsList) {
this.chordsList = chordsList;
}
它没有活动作为参数,因此您必须像下面那样更改适配器:
ChordsListAdapter adapter = new ChordsListAdapter(getChords());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.