Android 如何获取特定按钮并从属于 ListView 的自定义适配器中的一行编辑其文本
How to get specific button and edit its text from a row in custom adapter belonging to ListView in Android
我已经在 Android 中创建了自己的 ListView 适配器,该适配器来自以下教程:https://looksok.wordpress.com/2012/11/03/android-custom-listview-tutorial/
在这个适配器中,每个学生都有一个特定的行,我在其中显示他的统计数据。每行都有一些按钮,描述有关学生的一些信息。
按钮显示有关学生的正确信息,但是当我想通过单击 "copy grade" 按钮将值从 "predicted grade" 按钮复制到 "final grade" 按钮时,适配器会在最后复制成绩(或第一)行显示在屏幕上。这取决于当前滚动列表视图的方向(向下滚动导致在屏幕上显示最后一行的复制等级,向上滚动导致在屏幕上显示第一行的复制等级)。
我找不到关于更改未参数化适配器的组件值的合适解决方案。
这是我的代码:
public class SummaryDetStudentsAdapter extends ArrayAdapter<Student> {
private List<Student> items;
private int layoutResourceId;
private Context context;
private AtomSummaryGenStudensHolder holder = null;
private LessonsDetailedManagementWindow parent;
private MySQLiteHelper db;
private View row;
private boolean isLaboratory;
public SummaryDetStudentsAdapter(Context context, int layoutResourceId, List<Student> items) {
super(context, layoutResourceId, items);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.items = items;
parent = (LessonsDetailedManagementWindow) context;
db = parent.getDatabase();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
row = convertView;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new AtomSummaryGenStudensHolder();
holder.setStudent(items.get(position));
holder.setPresenceCount((Button) row.findViewById(R.id.btn_presence));
holder.setAbsentCount((Button) row.findViewById(R.id.btn_absence));
holder.setTotalPts((Button) row.findViewById(R.id.btn_total_pts));
holder.setStudentHeader((TextView) row.findViewById(R.id.tv_student_header));
holder.setTotalActPts((Button) row.findViewById(R.id.btn_total_activity_pts));
holder.setPerc((Button) row.findViewById(R.id.btn_percent));
holder.setPredictedGrade((Button) row.findViewById(R.id.btn_predicted_grade));
holder.setFinalGrade((Button) row.findViewById(R.id.btn_final_grade));
holder.setCopyGrade((ImageButton) row.findViewById(R.id.btn_copy_grade));
holder.getPresenceCount().setTag(holder.getStudent());
holder.getAbsentCount().setTag(holder.getStudent());
holder.getTotalPts().setTag(holder.getStudent());
holder.getStudentHeader().setTag(holder.getStudent());
holder.getTotalActPts().setTag(holder.getStudent());
holder.getPerc().setTag(holder.getStudent());
holder.getPredictedGrade().setTag(holder.getStudent());
holder.getFinalGrade().setTag(holder.getStudent());
holder.getCopyGrade().setTag(holder.getStudent());
handleCourseForm();
row.setTag(holder);
setupItem(items.get(position));
prepareButtons();
return row;
}
protected void handleCourseForm()
{
if(!this.parent.getGroup().getCourse().getCourseForm().equals(CourseForm.laboratory))
{
isLaboratory = false;
holder.getTotalPts().setVisibility(View.INVISIBLE);
holder.getPerc().setVisibility(View.INVISIBLE);
holder.getPredictedGrade().setVisibility(View.INVISIBLE);
}
else
{
isLaboratory = true;
}
}
protected void setupItem(Student student)
{
StudentStatistics ss = parent.getStudentStatistics(student);
holder.getStudentHeader().setText(TextRepresentationGetter.getStudentRepresentation(student));
holder.getPresenceCount().setText(ss.getPresenceCount()+"");
holder.getAbsentCount().setText(ss.getAbsenceCount()+"");
holder.getTotalActPts().setText(String.format("%.1f",ss.getTotalActivityPoints()));
if(isLaboratory) {
holder.getTotalPts().setText(String.format("%.1f", ss.getTotalPoints()));
int totalPts = parent.getTotalPoints();
double perc = totalPts == 0 ? 0 : new BigDecimal(ss.getTotalPoints()).divide(new BigDecimal(totalPts),4, RoundingMode.HALF_UP).doubleValue()*100;
holder.getPerc().setText(String.format("%.2f",perc));
double grade = parent.getGradeFromPercent(perc);
holder.getPredictedGrade().setText(totalPts == 0 || (int) grade == 0 ? getContext().getText(R.string.not_available) : String.valueOf(grade));
}
}
private void prepareButtons()
{
final ImageButton copyGrade = holder.getCopyGrade();
copyGrade.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Student s = (Student) v.getTag();
copyPredictedGradeToFinalGrade(s);
}
});
}
private void copyPredictedGradeToFinalGrade(Student s)
{
Button predictedGrade = holder.getPredictedGrade(); // it takes first or last button displayed on screen
Button finalGrade = holder.getFinalGrade(); // it should take the button from a row, where the copyGrade button was clicked
double grade = Double.parseDouble(String.valueOf(predictedGrade.getText()));
finalGrade.setText(String.valueOf(grade));
}
}
public class AtomSummaryGenStudensHolder {
private Student student;
private TextView student_header;
private Button presenceCount;
private Button absentCount;
private Button totalActPts;
private Button totalPts;
private Button perc;
private Button predictedGrade;
private Button finalGrade;
private ImageButton copyGrade;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Button getPresenceCount() {
return presenceCount;
}
public void setPresenceCount(Button presenceCount) {
this.presenceCount = presenceCount;
}
public Button getAbsentCount() {
return absentCount;
}
public void setAbsentCount(Button absentCount) {
this.absentCount = absentCount;
}
public Button getTotalActPts() {
return totalActPts;
}
public void setTotalActPts(Button totalActPts) {
this.totalActPts = totalActPts;
}
public Button getTotalPts() {
return totalPts;
}
public void setTotalPts(Button totalPts) {
this.totalPts = totalPts;
}
public TextView getStudentHeader() {
return student_header;
}
public void setStudentHeader(TextView student_header) {
this.student_header = student_header;
}
public Button getPerc() {
return perc;
}
public void setPerc(Button perc) {
this.perc = perc;
}
public Button getPredictedGrade() {
return predictedGrade;
}
public void setPredictedGrade(Button predictedGrade) {
this.predictedGrade = predictedGrade;
}
public Button getFinalGrade() {
return finalGrade;
}
public void setFinalGrade(Button finalGrade) {
this.finalGrade = finalGrade;
}
public ImageButton getCopyGrade() {
return copyGrade;
}
public void setCopyGrade(ImageButton copyGrade) {
this.copyGrade = copyGrade;
}
}
行布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_student_header"
android:layout_alignParentLeft="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_centerVertical="true"
android:textSize="@dimen/text_size_medium"
/>
<Button
android:id="@+id/btn_presence"
android:layout_width="@dimen/bulk_data_det_button_size_smaller"
android:layout_height="@dimen/bulk_data_det_button_size"
android:background="#80009688"
android:textColor="#ffffff"
android:text="@string/attendance_absent_acronym"
android:layout_toLeftOf="@+id/btn_absence"
android:minWidth="@dimen/bulk_data_det_button_size"
/>
<Button
android:id="@+id/btn_absence"
android:layout_width="@dimen/bulk_data_det_button_size_smaller"
android:layout_height="@dimen/bulk_data_det_button_size"
android:background="#80ff6666"
android:textColor="#ffffff"
android:text="@string/attendance_absent_acronym"
android:layout_toLeftOf="@+id/btn_total_activity_pts"
android:minWidth="@dimen/bulk_data_det_button_size"
/>
<Button
android:id="@+id/btn_total_activity_pts"
android:layout_width="@dimen/bulk_data_det_button_size_smaller"
android:layout_height="@dimen/bulk_data_det_button_size"
android:textColor="#000000"
android:layout_toLeftOf="@+id/btn_total_pts"
android:text="0"
android:background="#8000bdbd"
/>
<Button
android:id="@+id/btn_total_pts"
android:layout_width="@dimen/bulk_data_button_size"
android:layout_height="@dimen/bulk_data_det_button_size"
android:textColor="#000000"
android:layout_toLeftOf="@+id/btn_percent"
android:text="0"
android:background="#80bdbd00"
/>
<Button
android:id="@+id/btn_percent"
android:layout_width="@dimen/bulk_data_button_size"
android:layout_height="@dimen/bulk_data_det_button_size"
android:background="#80009688"
android:textColor="#ffffff"
android:layout_toLeftOf="@+id/btn_predicted_grade"
android:minWidth="@dimen/bulk_data_det_button_size"
/>
<Button
android:id="@+id/btn_predicted_grade"
android:layout_width="@dimen/bulk_data_det_button_size_smaller"
android:layout_height="@dimen/bulk_data_det_button_size"
android:background="#8000bdbd"
android:textColor="#ffffff"
android:layout_toLeftOf="@+id/btn_final_grade"
android:minWidth="@dimen/bulk_data_det_button_size"
/>
<Button
android:id="@+id/btn_final_grade"
android:layout_width="@dimen/bulk_data_det_button_size"
android:layout_height="@dimen/bulk_data_det_button_size"
android:background="#80bdbd00"
android:textColor="#ffffff"
android:layout_toLeftOf="@+id/btn_copy_grade"
android:minWidth="@dimen/bulk_data_det_button_size"
/>
<ImageButton
android:id="@+id/btn_copy_grade"
android:layout_width="@dimen/bulk_data_det_button_size_smaller"
android:layout_height="@dimen/bulk_data_det_button_size"
android:textColor="#ffffff"
android:background="#eeeeee"
android:scaleType="fitXY"
android:src="@drawable/check"
android:layout_alignParentRight="true"
android:minWidth="@dimen/bulk_data_det_button_size"
/>
</RelativeLayout>
我想更改方法 prepareButtons(copyButton 的侦听器在哪里)和方法 copyPredictedGradeToFinalGrade() 中按钮的值。它对我不起作用。我试图通过 'getTag()' 方法获取它们,然后将其转换为 Button class,但编译器每次都说这不是 Student class,但不是视图。
通过点击支票(屏幕上的数字 1),我想将 POC 列中的按钮内容复制到 KOC 列,但是 POC 列中的按钮内容已复制到数字 2 中的 KOC 列。我有不知道如何实现(我只能在 class 学生上正常操作)。
我该如何解决这个问题?我怎样才能让我的代码复制一行中的值,点击检查的图像按钮?如果能提供解决我问题的任何线索,我将不胜感激。
anwsers 的建议对我不起作用,因为在滚动 up/down 后,新元素在屏幕上显示了这些元素的顶部的听众。
提前致谢。
问题在这里,删除这一行:
private AtomSummaryGenStudensHolder holder = null;
您的函数 prepareButtons 和 copyPredictedGradeToFinalGrade 在调用时使用当前持有者值。在所有 getView 调用之后,这是最后显示的视图持有者值。
首先要正确使用 ViewHolder 模式,您应该这样做:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AtomSummaryGenStudensHolder holder;
row = convertView;
if (row == null || row .getTag() == null) {
holder = new AtomSummaryGenStudensHolder();
row = inflater.inflate(layoutResourceId, parent, false);
...
row .setTag(holder);
} else {
holder = (AtomSummaryGenStudensHolder) row.getTag();
}
那么你应该将 holder 传递给你的函数
handleCourseForm(holder );
...
prepareButtons(holder );
并且你应该更改所有使用 holder 的函数
private void prepareButtons(AtomSummaryGenStudensHolder holder)
{
final ImageButton copyGrade = holder.getCopyGrade();
copyGrade.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Student s = (Student) v.getTag();
copyPredictedGradeToFinalGrade(s, holder);
}
});
}
private void copyPredictedGradeToFinalGrade(Student s, AtomSummaryGenStudensHolder holder)
{
Button predictedGrade = holder.getPredictedGrade(); // it takes first or last button displayed on screen
Button finalGrade = holder.getFinalGrade(); // it should take the button from a row, where the copyGrade button was clicked
double grade = Double.parseDouble(String.valueOf(predictedGrade.getText()));
finalGrade.setText(String.valueOf(grade));
}
How can I make my code to copy the values in a row, where the image
button of check is clicked?
使用 View of onClick
方法从同一布局访问其他视图。将 copyPredictedGradeToFinalGrade
更改为:
private void copyPredictedGradeToFinalGrade(Student s, RelativeLayout view)
{
Button predictedGrade = (Button)view.findViewById(R.id.btn_predicted_grade);
Button finalGrade = = (Button)view.findViewById(R.id.btn_final_grade);
double grade = Double.parseDouble(String.valueOf(predictedGrade.getText()));
finalGrade.setText(String.valueOf(grade));
}
并通过传递 View as 来调用 copyPredictedGradeToFinalGrade
:
@Override
public void onClick(View v) {
Student s = (Student) v.getTag();
RelativeLayout relativeLayout=(RelativeLayout)view.getParent();
copyPredictedGradeToFinalGrade(s,relativeLayout);
}
我已经在 Android 中创建了自己的 ListView 适配器,该适配器来自以下教程:https://looksok.wordpress.com/2012/11/03/android-custom-listview-tutorial/
在这个适配器中,每个学生都有一个特定的行,我在其中显示他的统计数据。每行都有一些按钮,描述有关学生的一些信息。
按钮显示有关学生的正确信息,但是当我想通过单击 "copy grade" 按钮将值从 "predicted grade" 按钮复制到 "final grade" 按钮时,适配器会在最后复制成绩(或第一)行显示在屏幕上。这取决于当前滚动列表视图的方向(向下滚动导致在屏幕上显示最后一行的复制等级,向上滚动导致在屏幕上显示第一行的复制等级)。
我找不到关于更改未参数化适配器的组件值的合适解决方案。
这是我的代码:
public class SummaryDetStudentsAdapter extends ArrayAdapter<Student> {
private List<Student> items;
private int layoutResourceId;
private Context context;
private AtomSummaryGenStudensHolder holder = null;
private LessonsDetailedManagementWindow parent;
private MySQLiteHelper db;
private View row;
private boolean isLaboratory;
public SummaryDetStudentsAdapter(Context context, int layoutResourceId, List<Student> items) {
super(context, layoutResourceId, items);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.items = items;
parent = (LessonsDetailedManagementWindow) context;
db = parent.getDatabase();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
row = convertView;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new AtomSummaryGenStudensHolder();
holder.setStudent(items.get(position));
holder.setPresenceCount((Button) row.findViewById(R.id.btn_presence));
holder.setAbsentCount((Button) row.findViewById(R.id.btn_absence));
holder.setTotalPts((Button) row.findViewById(R.id.btn_total_pts));
holder.setStudentHeader((TextView) row.findViewById(R.id.tv_student_header));
holder.setTotalActPts((Button) row.findViewById(R.id.btn_total_activity_pts));
holder.setPerc((Button) row.findViewById(R.id.btn_percent));
holder.setPredictedGrade((Button) row.findViewById(R.id.btn_predicted_grade));
holder.setFinalGrade((Button) row.findViewById(R.id.btn_final_grade));
holder.setCopyGrade((ImageButton) row.findViewById(R.id.btn_copy_grade));
holder.getPresenceCount().setTag(holder.getStudent());
holder.getAbsentCount().setTag(holder.getStudent());
holder.getTotalPts().setTag(holder.getStudent());
holder.getStudentHeader().setTag(holder.getStudent());
holder.getTotalActPts().setTag(holder.getStudent());
holder.getPerc().setTag(holder.getStudent());
holder.getPredictedGrade().setTag(holder.getStudent());
holder.getFinalGrade().setTag(holder.getStudent());
holder.getCopyGrade().setTag(holder.getStudent());
handleCourseForm();
row.setTag(holder);
setupItem(items.get(position));
prepareButtons();
return row;
}
protected void handleCourseForm()
{
if(!this.parent.getGroup().getCourse().getCourseForm().equals(CourseForm.laboratory))
{
isLaboratory = false;
holder.getTotalPts().setVisibility(View.INVISIBLE);
holder.getPerc().setVisibility(View.INVISIBLE);
holder.getPredictedGrade().setVisibility(View.INVISIBLE);
}
else
{
isLaboratory = true;
}
}
protected void setupItem(Student student)
{
StudentStatistics ss = parent.getStudentStatistics(student);
holder.getStudentHeader().setText(TextRepresentationGetter.getStudentRepresentation(student));
holder.getPresenceCount().setText(ss.getPresenceCount()+"");
holder.getAbsentCount().setText(ss.getAbsenceCount()+"");
holder.getTotalActPts().setText(String.format("%.1f",ss.getTotalActivityPoints()));
if(isLaboratory) {
holder.getTotalPts().setText(String.format("%.1f", ss.getTotalPoints()));
int totalPts = parent.getTotalPoints();
double perc = totalPts == 0 ? 0 : new BigDecimal(ss.getTotalPoints()).divide(new BigDecimal(totalPts),4, RoundingMode.HALF_UP).doubleValue()*100;
holder.getPerc().setText(String.format("%.2f",perc));
double grade = parent.getGradeFromPercent(perc);
holder.getPredictedGrade().setText(totalPts == 0 || (int) grade == 0 ? getContext().getText(R.string.not_available) : String.valueOf(grade));
}
}
private void prepareButtons()
{
final ImageButton copyGrade = holder.getCopyGrade();
copyGrade.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Student s = (Student) v.getTag();
copyPredictedGradeToFinalGrade(s);
}
});
}
private void copyPredictedGradeToFinalGrade(Student s)
{
Button predictedGrade = holder.getPredictedGrade(); // it takes first or last button displayed on screen
Button finalGrade = holder.getFinalGrade(); // it should take the button from a row, where the copyGrade button was clicked
double grade = Double.parseDouble(String.valueOf(predictedGrade.getText()));
finalGrade.setText(String.valueOf(grade));
}
}
public class AtomSummaryGenStudensHolder {
private Student student;
private TextView student_header;
private Button presenceCount;
private Button absentCount;
private Button totalActPts;
private Button totalPts;
private Button perc;
private Button predictedGrade;
private Button finalGrade;
private ImageButton copyGrade;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Button getPresenceCount() {
return presenceCount;
}
public void setPresenceCount(Button presenceCount) {
this.presenceCount = presenceCount;
}
public Button getAbsentCount() {
return absentCount;
}
public void setAbsentCount(Button absentCount) {
this.absentCount = absentCount;
}
public Button getTotalActPts() {
return totalActPts;
}
public void setTotalActPts(Button totalActPts) {
this.totalActPts = totalActPts;
}
public Button getTotalPts() {
return totalPts;
}
public void setTotalPts(Button totalPts) {
this.totalPts = totalPts;
}
public TextView getStudentHeader() {
return student_header;
}
public void setStudentHeader(TextView student_header) {
this.student_header = student_header;
}
public Button getPerc() {
return perc;
}
public void setPerc(Button perc) {
this.perc = perc;
}
public Button getPredictedGrade() {
return predictedGrade;
}
public void setPredictedGrade(Button predictedGrade) {
this.predictedGrade = predictedGrade;
}
public Button getFinalGrade() {
return finalGrade;
}
public void setFinalGrade(Button finalGrade) {
this.finalGrade = finalGrade;
}
public ImageButton getCopyGrade() {
return copyGrade;
}
public void setCopyGrade(ImageButton copyGrade) {
this.copyGrade = copyGrade;
}
}
行布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_student_header"
android:layout_alignParentLeft="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_centerVertical="true"
android:textSize="@dimen/text_size_medium"
/>
<Button
android:id="@+id/btn_presence"
android:layout_width="@dimen/bulk_data_det_button_size_smaller"
android:layout_height="@dimen/bulk_data_det_button_size"
android:background="#80009688"
android:textColor="#ffffff"
android:text="@string/attendance_absent_acronym"
android:layout_toLeftOf="@+id/btn_absence"
android:minWidth="@dimen/bulk_data_det_button_size"
/>
<Button
android:id="@+id/btn_absence"
android:layout_width="@dimen/bulk_data_det_button_size_smaller"
android:layout_height="@dimen/bulk_data_det_button_size"
android:background="#80ff6666"
android:textColor="#ffffff"
android:text="@string/attendance_absent_acronym"
android:layout_toLeftOf="@+id/btn_total_activity_pts"
android:minWidth="@dimen/bulk_data_det_button_size"
/>
<Button
android:id="@+id/btn_total_activity_pts"
android:layout_width="@dimen/bulk_data_det_button_size_smaller"
android:layout_height="@dimen/bulk_data_det_button_size"
android:textColor="#000000"
android:layout_toLeftOf="@+id/btn_total_pts"
android:text="0"
android:background="#8000bdbd"
/>
<Button
android:id="@+id/btn_total_pts"
android:layout_width="@dimen/bulk_data_button_size"
android:layout_height="@dimen/bulk_data_det_button_size"
android:textColor="#000000"
android:layout_toLeftOf="@+id/btn_percent"
android:text="0"
android:background="#80bdbd00"
/>
<Button
android:id="@+id/btn_percent"
android:layout_width="@dimen/bulk_data_button_size"
android:layout_height="@dimen/bulk_data_det_button_size"
android:background="#80009688"
android:textColor="#ffffff"
android:layout_toLeftOf="@+id/btn_predicted_grade"
android:minWidth="@dimen/bulk_data_det_button_size"
/>
<Button
android:id="@+id/btn_predicted_grade"
android:layout_width="@dimen/bulk_data_det_button_size_smaller"
android:layout_height="@dimen/bulk_data_det_button_size"
android:background="#8000bdbd"
android:textColor="#ffffff"
android:layout_toLeftOf="@+id/btn_final_grade"
android:minWidth="@dimen/bulk_data_det_button_size"
/>
<Button
android:id="@+id/btn_final_grade"
android:layout_width="@dimen/bulk_data_det_button_size"
android:layout_height="@dimen/bulk_data_det_button_size"
android:background="#80bdbd00"
android:textColor="#ffffff"
android:layout_toLeftOf="@+id/btn_copy_grade"
android:minWidth="@dimen/bulk_data_det_button_size"
/>
<ImageButton
android:id="@+id/btn_copy_grade"
android:layout_width="@dimen/bulk_data_det_button_size_smaller"
android:layout_height="@dimen/bulk_data_det_button_size"
android:textColor="#ffffff"
android:background="#eeeeee"
android:scaleType="fitXY"
android:src="@drawable/check"
android:layout_alignParentRight="true"
android:minWidth="@dimen/bulk_data_det_button_size"
/>
</RelativeLayout>
我想更改方法 prepareButtons(copyButton 的侦听器在哪里)和方法 copyPredictedGradeToFinalGrade() 中按钮的值。它对我不起作用。我试图通过 'getTag()' 方法获取它们,然后将其转换为 Button class,但编译器每次都说这不是 Student class,但不是视图。
通过点击支票(屏幕上的数字 1),我想将 POC 列中的按钮内容复制到 KOC 列,但是 POC 列中的按钮内容已复制到数字 2 中的 KOC 列。我有不知道如何实现(我只能在 class 学生上正常操作)。
我该如何解决这个问题?我怎样才能让我的代码复制一行中的值,点击检查的图像按钮?如果能提供解决我问题的任何线索,我将不胜感激。
anwsers 的建议对我不起作用,因为在滚动 up/down 后,新元素在屏幕上显示了这些元素的顶部的听众。
提前致谢。
问题在这里,删除这一行:
private AtomSummaryGenStudensHolder holder = null;
您的函数 prepareButtons 和 copyPredictedGradeToFinalGrade 在调用时使用当前持有者值。在所有 getView 调用之后,这是最后显示的视图持有者值。 首先要正确使用 ViewHolder 模式,您应该这样做:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AtomSummaryGenStudensHolder holder;
row = convertView;
if (row == null || row .getTag() == null) {
holder = new AtomSummaryGenStudensHolder();
row = inflater.inflate(layoutResourceId, parent, false);
...
row .setTag(holder);
} else {
holder = (AtomSummaryGenStudensHolder) row.getTag();
}
那么你应该将 holder 传递给你的函数
handleCourseForm(holder );
...
prepareButtons(holder );
并且你应该更改所有使用 holder 的函数
private void prepareButtons(AtomSummaryGenStudensHolder holder)
{
final ImageButton copyGrade = holder.getCopyGrade();
copyGrade.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Student s = (Student) v.getTag();
copyPredictedGradeToFinalGrade(s, holder);
}
});
}
private void copyPredictedGradeToFinalGrade(Student s, AtomSummaryGenStudensHolder holder)
{
Button predictedGrade = holder.getPredictedGrade(); // it takes first or last button displayed on screen
Button finalGrade = holder.getFinalGrade(); // it should take the button from a row, where the copyGrade button was clicked
double grade = Double.parseDouble(String.valueOf(predictedGrade.getText()));
finalGrade.setText(String.valueOf(grade));
}
How can I make my code to copy the values in a row, where the image button of check is clicked?
使用 View of onClick
方法从同一布局访问其他视图。将 copyPredictedGradeToFinalGrade
更改为:
private void copyPredictedGradeToFinalGrade(Student s, RelativeLayout view)
{
Button predictedGrade = (Button)view.findViewById(R.id.btn_predicted_grade);
Button finalGrade = = (Button)view.findViewById(R.id.btn_final_grade);
double grade = Double.parseDouble(String.valueOf(predictedGrade.getText()));
finalGrade.setText(String.valueOf(grade));
}
并通过传递 View as 来调用 copyPredictedGradeToFinalGrade
:
@Override
public void onClick(View v) {
Student s = (Student) v.getTag();
RelativeLayout relativeLayout=(RelativeLayout)view.getParent();
copyPredictedGradeToFinalGrade(s,relativeLayout);
}