问题
I have a custom listView that is populated by Excel Sheet using jXl
library.
Everything works fine but when I select one or more ListView Item, automatically after 8-9 entries my selection is also selected on other ListView Item.. for example if radioButton1
is selected at index 0
now If I scroll down I see radioButton1
is selected at index 8
.
Here is my code:
CustomListView
public class CustomListView extends ArrayAdapter<ListModel> {
Context context;
private List<ListModel> studentsList;
ListModel listModel;
TextView name, date;
RadioGroup radioGroup;
RadioButton presentRDOButton, absentRDOButton, leaveRDOButton;
LayoutInflater inflater;
public static List<ListModel> attenList = new ArrayList<ListModel>();
final JExcelClass jXl = new JExcelClass();
boolean flagPres = true;
boolean flagAbsen = true;
boolean flagLeave = true;
public AttendanceCustomListView(Context context, int resource,
List<ListModel> studentsList) {
super(context, resource, studentsList);
this.context = context;
this.studentsList = studentsList;
}
private class ViewHolder {
TextView name, date;
RadioGroup radioGroup;
RadioButton presentRDOButton, absentRDOButton, leaveRDOButton;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final int pos = position + 1;
// inflater = (LayoutInflater) context
// .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.layout_list_content, null);
holder = new ViewHolder();
// initialization here
holder.name = (TextView) convertView.findViewById(R.id.nameListTV);
holder.date = (TextView) convertView.findViewById(R.id.dateTV);
holder.radioGroup = (RadioGroup) convertView
.findViewById(R.id.attendanceRDG);
holder.presentRDOButton = (RadioButton) convertView
.findViewById(R.id.presentRDO);
holder.absentRDOButton = (RadioButton) convertView
.findViewById(R.id.absentRDO);
holder.leaveRDOButton = (RadioButton) convertView
.findViewById(R.id.leaveRDO);
holder.radioGroup
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group,
int checkedId) {
int getPosition = (Integer) group.getTag();
studentsList.get(getPosition). //problem here
switch (checkedId) {
case R.id.presentRDO:
//code goes here
break;
}
}
});
and CustomClass
public class CustomClass extends Activity {
ListView listView;
List<ListModel> listModel;
CustomListView attendanceCustomListView;
JExcelClass jXl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_listview);
listView = (ListView) findViewById(R.id.list);
listModel = new ArrayList<ListModel>();
jXl = new JExcelClass();
List<String> abc = jXl.readExcelSheet("test.xls");
String date = this.getDate();
for (String temp : abc) {
listModel.add(new ListModel(temp, date));
}
attendanceCustomListView = new AttendanceCustomListView(this,
R.layout.layout_list_content, listModel);
listView.setAdapter(attendanceCustomListView);
}
}
Please let me know If some more information is needed. Thanks.
Updated the Code
Now my problem is how to set the RadioButton
through RadioGroup
?
回答1:
What do you do in OnCheckChanged? I had a similar issue and in my case, it was because I was using the position from
public View getView(int position, View convertView, ViewGroup parent)
to update the data onCheckChanged.
What eventually solved it for me, is 1. Using the ViewHolder pattern. 2. When updating the data onCheckChanged, the position I used for data.get(NewPosition).set...(true) was not the position from the getview, rather from :
int getPosition = (Integer) group.getTag();
The getTag(); comes from using the Viewholder pattern and group comes from:
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int getPosition = (Integer) group.getTag();//do this
switch (checkedId) {
case R.id.presentRDO:
//some code here....
studentsList.get(getPosition).setPresent(true);//do this
break;
}
Hope this helps you.
Do this:
if (convertView == null) {
inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.layout_list_content, null);
holder = new ViewHolder();
// initialization here
holder.name = (TextView) convertView.findViewById(R.id.nameListTV);
holder.date = (TextView) convertView.findViewById(R.id.dateTV);
holder.radioGroup = (RadioGroup) convertView
.findViewById(R.id.attendanceRDG);
holder.presentRDOButton = (RadioButton) convertView
.findViewById(R.id.presentRDO);
holder.absentRDOButton = (RadioButton) convertView
.findViewById(R.id.absentRDO);
holder.leaveRDOButton = (RadioButton) convertView
.findViewById(R.id.leaveRDO);
convertView.setTag(R.id.attendanceRDG, holder.radioGroup);//do for all views
}
} else {
viewHolder = (ViewHolderItem) convertView.getTag();
}
holder.radioGroup.setTag(position);//Similarly for all elements
holder.radioGroup
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group,
int checkedId) {
int getPosition = (Integer) group.getTag();
studentsList.get(getPosition). //problem here
switch (checkedId) {
case R.id.presentRDO:
//code goes here
break;
}
});
回答2:
Okay! at last i got my answer!
Basically all i needed was to add 3 static final
variables and use them for saving the state of the radioButton clicked.
Please follow this answer, this is the most simple answer all over the internet.
Thanks @hrishitiwari for pointing to the answer.
来源:https://stackoverflow.com/questions/25333728/radiobutton-selection-jumps-is-auto-selected-on-other-custom-listview