大家好!我一直在尝试使用单选按钮ListView。但是,我有一些问题与单选按钮处理。无论如何,我的应用程序应该处理来自Mysql服务器的数据。从服务器检索到的数据如下:
1
b 0
c 1
d 1
e0
所以,你可以看到我只有两列第一列是一个名字第二列是一个数字(这个数字只能是1或0)所以,我已经创建了我的形状来处理这两列作为文本。我没有问题。但是,当我尝试使用单选按钮时。我已经开始发行了。下面是我的布局和我的类。
形状布局:
<?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>
我觉得我在这里没有问题。
第二个类是我的Custom类:
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;
}
}
My Adapter如下:
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;
}
}
你可以看到,我承诺处理单选按钮,因为我不能解决这个问题。
希望你能帮上忙
变化
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);