RadioButton选择跳转在其他Custom ListView上自动选择(RadioButton selection jumps is auto selected on other Custom

编程入门 行业动态 更新时间:2024-10-27 20:35:25
RadioButton选择跳转在其他Custom ListView上自动选择(RadioButton selection jumps is auto selected on other Custom ListView)

我有一个自定义listView,由Excel Sheet使用jXl库填充。 一切正常但是当我选择一个或多个ListView项目时,自动在8-9个条目之后我的选择也会在其他ListView项目中被选中..例如,如果现在在索引0处选择了radioButton1如果我向下滚动我看到radioButton1被选中了指数8 。 这是我的代码: 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; } } });

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

如果需要更多信息,请告诉我。 谢谢。

更新了代码现在我的问题是如何通过RadioGroup设置RadioButton ?

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?

最满意答案

你在OnCheckChanged做什么? 我有一个类似的问题,在我的情况下,这是因为我使用的是位置

public View getView(int position, View convertView, ViewGroup parent)

更新onCheckChanged上的数据。

最终为我解决的是1.使用ViewHolder模式 。 2.更新onCheckChanged数据时,我用于data.get(NewPosition).set ...(true)的位置不是来自getview的位置,而是来自:

int getPosition = (Integer) group.getTag();

getTag(); 来自使用Viewholder模式,组来自:

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

希望这对你有所帮助。

做这个:

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

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.

更多推荐

本文发布于:2023-08-07 15:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464756.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:跳转   ListView   Custom   RadioButton   auto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!