单选按钮选择跳转在其他自定义列表视图上自动选择

时间:2014-08-15 20:27:42

标签: android listview custom-lists

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

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

更新了代码 现在我的问题是如何设置RadioButtonRadioGroup

2 个答案:

答案 0 :(得分:0)

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

中的位置
public View getView(int position, View convertView, ViewGroup parent)

更新onCheckChanged上的数据。

最终为我解决的是,是 1.使用ViewHolder pattern。 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;
                                        }

                });

答案 1 :(得分:0)

好!最后我得到了答案! 基本上我只需要添加3个static final变量并使用它们来保存单击的radioButton的状态。 请关注此answer,这是互联网上最简单的答案。 感谢@hrishitiwari指出答案。