我的适配器似乎没有将我的数组作为参数..
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
List<Character> characters;
public MyAdapter(List<Character> list) {
this.characters = list;
}
@Override
public int getItemCount() {
return characters.size();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int itemType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_cell,viewGroup,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Character character = characters.get(position);
holder.display(character);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView name;
private final TextView secondname;
private final TextView numero;
public MyViewHolder(final View itemView) {
super(itemView);
name = ((TextView) itemView.findViewById(R.id.name));
secondname = ((TextView) itemView.findViewById(R.id.secondname));
numero = ((TextView) itemView.findViewById(R.id.numero));
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
public void display(Character character) {
name.setText(character.getNom());
secondname.setText(character.getPrenom());
numero.setText(character.getNumero());
}
}
}
和我的MainActivity:
public class MainActivity extends AppCompatActivity {
List<Character> characters = new ArrayList<>();
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ajouterVilles();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new MyAdapter());
}
private void ajouterVilles() {
characters.add(new Character("Romain", "Leroy", 0625421535));
}
}
这是日志:
Error:(28, 33) error: constructor MyAdapter in class MyAdapter cannot be applied to given types;
required: List<Character>
found: no arguments
reason: actual and formal argument lists differ in length
我试图在参数中传递List但没有成功。
有人可以帮助我吗?
编辑:我的第一个问题已经解决,但现在,我有一个致命的执行:
致命的例外:主要
流程:ad.training.repertoire,PID:24006 android.content.res.Resources $ NotFoundException:字符串资源ID#0x656235d
在android.content.res.Resources.getText(Resources.java:340)
在android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
在android.widget.TextView.setText(TextView.java:4171)
at ad.training.repertoire.MyAdapter $ MyViewHolder.display(MyAdapter.java:69)
at ad.training.repertoire.MyAdapter.onBindViewHolder(MyAdapter.java:40)
at ad.training.repertoire.MyAdapter.onBindViewHolder(MyAdapter.java:15)
答案 0 :(得分:0)
您需要为Adapter提供一个列表参数,
MyAdapter adapter = new MyAdapter(characters);
recyclerView.setAdapter(adapter);
答案 1 :(得分:0)
更改您的主要活动,如
public class MainActivity extends AppCompatActivity {
List<Character> characters = new ArrayList<>();
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ajouterVilles();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new MyAdapter(characters));
}
private void ajouterVilles() {
characters.add(new Character("Romain", "Leroy", 0625421535));
}
}
因为在适配器中你的适配器构造函数就像
public MyAdapter(List<Character> list) {
this.characters = list;
}