我正在使用recyclerview来显示我的列表。我接受的输入来自edittext。当我点击保存按钮时,它只显示一行。如何在列表中添加更多项目..这是我的代码
MycardViewAdapter.class
public class MyCardViewAdapter extends RecyclerView.Adapter<MyCardViewAdapter.myViewHolder> {
private Context context;
private List<Student> student;
public MyCardViewAdapter(Context context, List<Student> student){
this.context = context;
this.student = student;
}
@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.student,parent,false);
return new myViewHolder(itemView);
}
@Override
public void onBindViewHolder(myViewHolder holder, int position) {
Student s = student.get(position);
holder.name.setText(s.getName());
holder.surname.setText(s.getSurname());
holder.age.setText(s.getAge());
holder.marks.setText(s.getMarks());
}
public void updateList(List<Student> student)
{
student = student;
notifyDataSetChanged();
}
public void addItem(int position, Student stud)
{
student.add(position, stud);
notifyItemInserted(position);
}
@Override
public int getItemCount() {
return student.size();
}
public class myViewHolder extends RecyclerView.ViewHolder{
TextView name,surname,age,marks;
public myViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.cardname);
surname = (TextView) itemView.findViewById(R.id.cardsurname);
age = (TextView) itemView.findViewById(R.id.cardage);
marks = (TextView) itemView.findViewById(R.id.cardmarks);
}
}
}
MainActivity.class
public class MainActivity extends AppCompatActivity {
EditText name,surname,age,marks;
Button save;
MyCardViewAdapter adapter;
RecyclerView recyclerView;
List<Student> student;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.name);
surname = (EditText) findViewById(R.id.surname);
age = (EditText) findViewById(R.id.age);
marks = (EditText) findViewById(R.id.marks);
save = (Button) findViewById(R.id.savebutton);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
student = new ArrayList<Student>();
adapter = new MyCardViewAdapter(MainActivity.this,student);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RecyclerView.LayoutManager lmmanager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(lmmanager);
recyclerView.setAdapter(adapter);
student.add(new Student(name.getText().toString(),surname.getText().toString(),age.getText().toString(),marks.getText().toString()));
adapter.notifyDataSetChanged();
}
});
}
答案 0 :(得分:1)
In your save button, add this code.
RecyclerView.LayoutManager lmmanager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(lmmanager);
student.add(new Student(name.getText().toString(), surname.getText().toString(),age.getText().toString(), marks.getText().toString()));
adapter = new MyCardViewAdapter(MainActivity.this, student);
recyclerView.setAdapter(adapter);