gpt4 book ai didi

android - 如何在android中将更多项目从edittext添加到recyclerview

转载 作者:太空狗 更新时间:2023-10-29 13:53:07 27 4
gpt4 key购买 nike

我正在使用 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();


}
});
}

最佳答案

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);

关于android - 如何在android中将更多项目从edittext添加到recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43291502/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com