Sunday, September 29, 2013

Not able toget a result on clicking the items of a list that include radio buttons

Not able toget a result on clicking the items of a list that include radio
buttons

I have a list that has a text view and 2 radio buttons in each row.I am
using a custom array adapter to populate the list view.I have implemented
the on item click listener on the list but it is not returning anything.In
fact it not executing the on click method at all.Here is the code:
Adapter class:
public class MyAdapter extends ArrayAdapter<String> {
Context context;
public static ArrayList<String> values=new ArrayList<String>();
public static ArrayList<String> attendance=new ArrayList<String>();
RadioGroup radioGroup;
RadioButton present,absent;
public MyAdapter(Context context,ArrayList values) {
super(context,org.example.attendance.R.layout.list_radio,values);
// TODO Auto-generated constructor stub
this.context=context;
this.values=values;
}
public View getView(int pos,View convertview,ViewGroup parent)
{
LayoutInflater
inflator=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View
rowView=inflator.inflate(org.example.attendance.R.layout.list_radio,parent,false);
radioGroup=(RadioGroup)rowView.findViewById(R.id.radioGroup1);
present=(RadioButton)rowView.findViewById(org.example.attendance.R.id.radio0);
absent=(RadioButton)rowView.findViewById(org.example.attendance.R.id.radio1);
TextView
textview=(TextView)rowView.findViewById(org.example.attendance.R.id.textView1);
// textview.setText(values[pos]);
//String s=values[pos];
textview.setText(values.get(pos));
//int selectedId = radioGroup.getCheckedRadioButtonId();
//if(selectedId==R.id.radio0)
return rowView;
}
public boolean areAllItemsEnabled()
{
return true;
}
@Override
public boolean isEnabled(int arg0)
{
return true;
}
}
main class:
public class Students_List extends Activity {
Intent i1;
String tablename;
RadioGroup radioGroup;
RadioButton present,absent;
ArrayList<String> student_name = new ArrayList<String>() ;
public static ArrayList<String> attendance=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_students__list);
final ListView listview = (ListView) findViewById(R.id.listview);
i1=getIntent();
radioGroup=(RadioGroup)findViewById(R.id.radioGroup1);
present=(RadioButton)findViewById(org.example.attendance.R.id.radio0);
absent=(RadioButton)findViewById(org.example.attendance.R.id.radio1);
tablename=i1.getStringExtra("class")+"_student";
//Log.d("student","table"+tablename);
String temp=getServerData(KEY_12);
listview.setAdapter(new MyAdapter(this,student_name));
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position,
long arg3) {
// TODO Auto-generated method stub
Log.d("view","clicked");
if(present.isChecked())
{
attendance.add("present");
}
if(absent.isChecked())
{
attendance.add("absent");
}
}
});
}
list_radio.xml:file that contains the views to populate each row
<?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="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="24dp" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView1"
android:layout_marginRight="35dp"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="45dp"
android:checked="true" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
</RelativeLayout>
and finally the list view xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:textSize="20sp"
android:textStyle="bold"
android:text="@string/present"
android:paddingRight="25dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:text="@string/absent" />
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:cacheColorHint="#f8f8ff"
android:divider="#000000"
android:fadeScrollbars="true"
android:choiceMode="singleChoice"
android:fastScrollEnabled="true"
android:footerDividersEnabled="true"
android:textFilterEnabled="true"
android:textStyle="normal" />
<Button
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/submit"
android:onClick="submit"/>
</LinearLayout>
What should i do so that i can get the state of the radio buttons??

No comments:

Post a Comment