在我的主要活动中有两个编辑文本,一个用于学生姓名,另一个用于学生学院,还有一个添加学生按钮,如下面的代码,添加学生按钮的onClick功能将插入编辑文本的值到列表视图:
public class Students extends AppCompatActivity {
//Initializing ...
EditText NameEditText;
EditText CollegeEditText;
Button AddButton;
ListView StudentListView;
ArrayList<Student> list;
ArrayAdapter<Student> adapter;
Random rand = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_students);
//获取参考资料......
AddButton=(Button) findViewById(R.id.AddBtn);
NameEditText=(EditText) findViewById(R.id.StudentName);
CollegeEditText=(EditText) findViewById(R.id.College);
StudentListView=(ListView) findViewById(R.id.StudentListView);
//define the array list of students .
list=new ArrayList<Student>();
//set the communication between the ArrayList and the adapter.
adapter=new ArrayAdapter<Student>(this,android.R.layout.simple_list_item_1,list);
//set the communication between the Adapter and the ListView.
StudentListView.setAdapter(adapter);
OnClickListener listener=new OnClickListener() {
@Override
public void onClick(View v) {
int ID=rand.nextInt(50) + 1;
Student student=new Student();
student.setID(ID);
student.setName(NameEditText.getText().toString());
student.setCollege(CollegeEditText.getText().toString());
list.add(student);
NameEditText.setText("");
CollegeEditText.setText("");
adapter.notifyDataSetChanged();
}
};
AddButton.setOnClickListener(listener);
}}
如您所见,我已经创建了一个学生班级和学生列表,并使用了数组适配器将值添加到ListView。
我真正想要的是自定义ListView,因此列表视图的每一行都看起来像这样
所以我将Array Adapter类自定义为Student Array Adapter,如下代码所示:
class StudentAdapter extends ArrayAdapter <Student>{
public Context context;
public List<Student>students;
public StudentAdapter(@NonNull Context context, List<Student> list) {
super(context,R.layout.student_row, list);
this.context=context;
this.students=list;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.student_row, parent, false);
TextView name = (TextView) rowView.findViewById(R.id.Name);
TextView college = (TextView) rowView.findViewById(R.id.College);
//下一步做什么???
}
}
我想在学生行中设置每个文本视图的值 我怎样才能完成学生适配器?
答案 0 :(得分:0)
好的,让我看看我是否做对了。
你有一系列的学生 此数组,您将作为List传递给Custom适配器 您有ListItems的自定义XML布局(R.layout.student_row) 在此布局中,您必须使用TextViews(名称和大学)
现在您想要做的是,在列表视图的项目中显示学生列表中的数据。为此,请尝试以下操作:
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.student_row, parent, false);
TextView name = (TextView) rowView.findViewById(R.id.Name);
TextView college = (TextView) rowView.findViewById(R.id.College);
Student student = getItem(position);
name.setText(student.getname());
college.setText(student.getCollege());
return rowView;
}
<强>解释强>
学生= getItem(职位);
//You are passing a List of Students to the Custom Adapter
public StudentAdapter(@NonNull Context context, List<Student> list) {
super(context,R.layout.student_row, list);
this.context=context;
this.students=list;
}
dapter将遍历列表中的所有元素。因此,列表项目与学生列表中的项目一样多。
如果您按CTRL +单击数组适配器(您在自定义适配器中扩展),您将进入ArrayAdapter Java类。在这里,您可以搜索getPosition()函数。
/**
* Returns the position of the specified item in the array.
*
* @param item The item to retrieve the position of.
*
* @return The position of the specified item.
*/
public int getPosition(@Nullable T item) {
return mObjects.indexOf(item);
}
这意味着,您将把学生放在学生列表的当前位置。