使用来自服务器的数据处理ListView中的单选按钮

时间:2015-09-29 12:10:05

标签: android listview

大家好,你好吗? 我一直在尝试使用ListView和Radio Buttons。但是,我在单选按钮处理方面遇到了一些问题。 无论如何,我的应用程序应该处理来自Mysql服务器的数据。从服务器检索的数据如下:

1

b 0

c 1

d 1

e 0

所以,你可以看到我只有两列,一列是名字而第二列是一个数字(这个数字只能是1或0) 所以,我创建了我的形状来处理两列作为TEXT。我没有问题。但是,当我尝试使用Radio Buttons时。我已经开始发行了。 贝娄是我的布局和我的课程。

形状布局:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<RadioGroup
    android:layout_width="0dp"
    android:layout_weight="4"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:gravity="center"
    android:id="@+id/CHOOSING_ABSENCE">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/present"/>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/absence"/>

          </RadioGroup>

<TextView
    android:paddingRight="10dp"
    android:layout_width="0dp"
    android:layout_weight="5"
    android:gravity="center_vertical"
    android:text="TEST"
    android:textColor="@android:color/black"
    android:id="@+id/Student_name"
    android:layout_height="50dp" />
     </LinearLayout>

我认为这里没有问题。

第二节课是我的自定义课程:

 public class ListViewStudentEditAbsence {
private  String StudentName ;
private String StudentID ;
private int Absence  ;

public ListViewStudentEditAbsence(String StudentName, String  StudentID, String Absence )
{
    this.StudentName = StudentName ;
    this.StudentID = StudentID;
    if(Absence == "0")
        this.Absence = 0  ;
    else
        this.Absence = 1  ;

}


public int getAbsence() {
    return Absence;
}

public void setAbsence(int absence) {
    Absence = absence;
}

public String getStudentID() {
    return StudentID;
}

public void setStudentID(String studentID) {
    StudentID = studentID;
}

public String getStudentName() {
    return StudentName;
}


public void setStudentName(String studentName)
{
    StudentName = studentName;
}
}

我的适配器如下:

 public class ListViewStudentEditAbsenceAdapter extends ArrayAdapter<ListViewStudentEditAbsence>
  {

private Context mContext;
private ArrayList<ListViewStudentEditAbsence> mData;


public ListViewStudentEditAbsenceAdapter (Context mContext, ArrayList<ListViewStudentEditAbsence> mData) {
    super(mContext, R.layout.student_name_editing, mData);
    this.mContext = mContext;
    this.mData = new ArrayList<ListViewStudentEditAbsence>();
    this.mData.addAll(mData);
}

@Override
public int getCount() {
    return mData.size();
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.student_name_editing, null);
    }
    TextView Name  = (TextView) convertView.findViewById(R.id.Student_name);
    Name.setText(mData.get(position).getStudentName());
/*
    RadioGroup Absence  = (RadioGroup) convertView.findViewById(R.id.CHOOSING_ABSENCE);
    Absence.setText(mData.get(position).getAbsence());
  */
    return convertView;
}
 }

正如您所见,我已经提交处理单选按钮,因为我无法解决此问题。

我希望你能帮忙

1 个答案:

答案 0 :(得分:1)

更改

 if(Absence == "0")
    this.Absence = 0  ;
else
    this.Absence = 1  ;

 if(Absence.equals("0"))
    this.Absence = 0  ;
else
    this.Absence = 1  ;

 RadioGroup Absence  = (RadioGroup) convertView.findViewById(R.id.CHOOSING_ABSENCE);
Absence.setText(mData.get(position).getAbsence());

 RadioGroup Absence  = (RadioGroup) convertView.findViewById(R.id.CHOOSING_ABSENCE);
if((mData.get(position).getAbsence() == 1))
Absence.check(R.id.present);
else
Absence.check(R.id.absence);