如何获取特定按钮并在Android中属于ListView的自定义适配器中的行中编辑其文本

时间:2015-11-27 10:47:39

标签: android listview android-arrayadapter

我已根据以下教程创建了自己的Android ListView适配器:https://looksok.wordpress.com/2012/11/03/android-custom-listview-tutorial/

在这个适配器中,每个学生都有一个特定的行,我会在其中显示他的统计信息。每行都有一些按钮,用于描述学生的一些信息。

按钮显示有关学生的正确信息,但是当我想复制来自"预测成绩"按钮到"最终成绩"单击"复制成绩"按钮,适配器在屏幕上显示的最后(或第一)行中复制等级。它取决于当前滚动列表视图的方向(向下滚动导致屏幕上显示的最后一行中的复制等级,向上滚动导致屏幕上显示的第一行中的复制等级。)

我无法找到关于更改不参数化适配器的组件值的正确解决方案。

这是我的代码:

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()中的按钮值。它对我不起作用。我试图通过&#39; getTag()&#39;方法并稍后将其转换为Button类,但编译器说每次都不是Student类,而不是视图。

Screen of list view with custom adapter

点击支票(屏幕上的数字1)我想将POC栏中按钮的内容复制到KOC栏,但是POC栏中的按钮内容被复制到2号中看到的KOC栏。我不知道我怎么能做到这一点(我只能在班级学生上正常运作)。

如何解决此问题?如何使我的代码复制行中的值,单击检查的图像按钮?我会很感激解决问题的任何线索。

对于我不起作用的提议,因为在向上/向下滚动之后,新元素会从屏幕上显示的这些元素中听到这些元素。

提前致谢。

2 个答案:

答案 0 :(得分:1)

问题在于此处,请删除此行:

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

然后你应该将持有人传递给你的职能

handleCourseForm(holder );
...
prepareButtons(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));
}

答案 1 :(得分:1)

  

如何让我的代码复制图像中的行   单击检查按钮?

使用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视为:

来调用copyPredictedGradeToFinalGrade
@Override
public void onClick(View v) {
 Student s = (Student) v.getTag();
 RelativeLayout relativeLayout=(RelativeLayout)view.getParent();
 copyPredictedGradeToFinalGrade(s,relativeLayout);
}