自定义数组适配器以将 edittext 值添加到 ListView
customization array adapter to add edittext values to ListView
在我的主程序中 activity 有两个编辑文本,一个用于学生姓名,一个用于学生学院,还有一个添加学生按钮,如下代码添加学生按钮的 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);
}}
如您所见,我创建了一个学生 class 和学生列表,并使用数组适配器将值添加到 ListView。
我真正想要的是自定义我的 ListView,使列表视图的每一行看起来像这样
ListViewRow.xml
所以我将阵列适配器 class 自定义为学生阵列适配器,如下代码:
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);
//下一步做什么???
}
}
我想设置学生行中每个文本视图的值
我怎样才能完成我的学生适配器?
好的,让我看看我是否做对了。
你有一组学生
这个数组,您将作为列表传递给自定义适配器
您有一个自定义的 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;
}
适配器将遍历列表中的所有元素。因此,列表项的数量将与学生列表中的项目一样多。
如果您按住 CTRL 并单击阵列适配器(您在自定义适配器中扩展),您将进入阵列适配器 Java Class。在这里你可以搜索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);
}
这意味着,您将获得学生列表当前位置的学生。
在我的主程序中 activity 有两个编辑文本,一个用于学生姓名,一个用于学生学院,还有一个添加学生按钮,如下代码添加学生按钮的 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);
}}
如您所见,我创建了一个学生 class 和学生列表,并使用数组适配器将值添加到 ListView。
我真正想要的是自定义我的 ListView,使列表视图的每一行看起来像这样
ListViewRow.xml
所以我将阵列适配器 class 自定义为学生阵列适配器,如下代码:
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);
//下一步做什么???
}
}
我想设置学生行中每个文本视图的值 我怎样才能完成我的学生适配器?
好的,让我看看我是否做对了。
你有一组学生 这个数组,您将作为列表传递给自定义适配器 您有一个自定义的 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;
}
适配器将遍历列表中的所有元素。因此,列表项的数量将与学生列表中的项目一样多。
如果您按住 CTRL 并单击阵列适配器(您在自定义适配器中扩展),您将进入阵列适配器 Java Class。在这里你可以搜索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);
}
这意味着,您将获得学生列表当前位置的学生。