1

I have a list of students in a ListView for which I want to keep track of attendance (somewhat similar to How to get checked radio button in list view? and Listview with Checkbox,RadioButton,Textview and button not working correctly in android).

When the user hits the submit-button I need to know for each row what the RadioGroup was set to, in order to then save the values back into a database (been using SimpleCursorAdapter so far).

I've looked into SimpleCursorAdapter.getCount() and RadioGroup.getCheckedRadioButtonId() to use a for-loop to traverse the list but still can't figure out how to get a hold of the RadioGroup for each list item.

listView.getAdapter().getItem() ?

Could you please point me in the right direction? I'm not trying to do anything when the user checks an item or when the state of a button is changed.

Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
andr
  • 11
  • 1
  • 3

3 Answers3

3
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
     final int child=listView.getChildCount();
      for(int i=0;i<child;i++) {
        if(listView.childAt(i) instanceof RadioGroup){
         RadioGroup rg=(RadioGroup)listView.childAt(i);
         int a = rg.getChildCount();
         for(int j=0;j<a;++j) {
            if((RadioButton)rg.getChildAt(j).isChecked()==true) {
           System.out.println("RadioButton At "+j+" Is Selected");}
          }
       }
  }
}
});
Tony Stark
  • 31
  • 4
1

use a viewHolder in your listview

Model.java

public class Model {

    private String name;
    private boolean selected;

    public Model(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}

MainActivity.java

public class MainActivity extends Activity implements OnItemClickListener{

    ListView listView;
    ArrayAdapter<Model> adapter;
    List<Model> list = new ArrayList<Model>();

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        listView = (ListView) findViewById(R.id.my_list);
        adapter = new MyAdapter(this,getModel());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
                TextView label = (TextView) v.getTag(R.id.label);
  CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
  Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
    }

    private String isCheckedOrNot(CheckBox checkbox) {
        if(checkbox.isChecked())
        return "is checked";
        else
        return "is not checked";
    }

    private List<Model> getModel() {
        list.add(new Model("Linux"));
        list.add(new Model("Windows7"));
        list.add(new Model("Suse"));
        list.add(new Model("Eclipse"));
        list.add(new Model("Ubuntu"));
        list.add(new Model("Solaris"));
        list.add(new Model("Android"));
        list.add(new Model("iPhone"));
        list.add(new Model("Java"));
        list.add(new Model(".Net"));
        list.add(new Model("PHP"));
        return list;
    }
} 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/my_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

</LinearLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30sp" >
</TextView>

<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4dip"
android:layout_marginRight="10dip"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>

</RelativeLayout> 

MyAdapter.java

public class MyAdapter extends ArrayAdapter<Model> {

    private final List<Model> list;
    private final Activity context;
    boolean checkAll_flag = false;
    boolean checkItem_flag = false;

    public MyAdapter(Activity context, List<Model> list) {
        super(context, R.layout.row, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            convertView = inflator.inflate(R.layout.row, null);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
            viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                    list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                }
            });
            convertView.setTag(viewHolder);
            convertView.setTag(R.id.label, viewHolder.text);
            convertView.setTag(R.id.check, viewHolder.checkbox);
            } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.checkbox.setTag(position); // This line is important.

        viewHolder.text.setText(list.get(position).getName());
        viewHolder.checkbox.setChecked(list.get(position).isSelected());

        return convertView;
    }
} 
Akhil Jayakumar
  • 2,262
  • 14
  • 25
-1

I also had faced same problem now what should you need to do it

  • just use getChildCount() on ListView
  • Use a loop from for(i=0 upto childoflist)
  • Now get child at i th location of listview by using view=listview.getChildAt(i)
  • Now you must do some thing like radiogroup=view.findViewById(R.id.radiogrp)
  • Now you have all things then u must do radioButton = (RadioButton) rgg.findViewById(selectedId);

Here i am attaching my code which is in addition to Akhil Jayakumar code of student list

///ok is your upload button

ok.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         final int child=listView.getChildCount();
          for(int i=0;i<child;i++) {
            View rgg=listView.getChildAt(i);

    radioGroup = (RadioGroup) rgg.findViewById(R.id.radio);

    int selectedId=radioGroup.getCheckedRadioButtonId();

    radioButton = (RadioButton) rgg.findViewById(selectedId);

  }
}
});
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Shubham Sharma
  • 2,763
  • 5
  • 31
  • 46