I am not able to set customized color to a Radio Button in a listview at selected position. It is getting selected at different position and at the same time if i scroll the listview the items i selected are changing the position as well as the Radio button.
List of items were populated from Database and able to inflate the items.
activity_student_stats.xml code is here:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@android:color/transparent"
android:orientation="horizontal"
android:weightSum="3" >
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.6"
android:maxLength="25"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
>
</TextView>
<RadioGroup
android:id="@+id/radiogroup1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_weight="1.4"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="1.4" >
<RadioButton
android:id="@+id/present_radio"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.6"
android:button="@drawable/radio_background"
/>
<RadioButton
android:id="@+id/absent_radio"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.6"
android:button="@drawable/radio_background"
/>
</RadioGroup>
</LinearLayout>
Here is my code for Custom Adapter:
public class ArrayAdapterActivity extends ArrayAdapter<String>{
private Context context;
private List<String> StudentInfo;
int pos=0;
private boolean userSelected=false;
private RadioButton mcheckedRB;
ViewHolder holder;
public ArrayAdapterActivity(Context context, String[] values){
super(context, R.layout.activity_student_stats, values);
this.context=context;
this.StudentInfo=Arrays.asList(values);
}
public ArrayAdapterActivity(Context context){
super(context, R.layout.activity_student_stats);
this.context=context;
}
@Override
public boolean hasStableIds(){
return true;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
View rowView=convertView;
if(convertView==null){
LayoutInflater infalter= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView=infalter.inflate(R.layout.activity_student_stats, parent,false);
holder=new ViewHolder();//Holder initiation
holder.position=position;
holder.label=(TextView) rowView.findViewById(R.id.name);
holder.presentAbsentRadio=(RadioGroup) rowView.findViewById(R.id.radiogroup1);
holder.present_radio=(RadioButton) rowView.findViewById(R.id.present_radio);
holder.absent_radio=(RadioButton) rowView.findViewById(R.id.absent_radio);
rowView.setTag(holder);
holder.presentAbsentRadio.setTag(position);
holder.present_radio.setTag(position);
holder.absent_radio.setTag(position);
}
else{
holder=(ViewHolder) rowView.getTag();
}
holder.label.setText(StudentInfo.get(position).toString());
if(position==getCount()-1 && userSelected == false){
mcheckedRB=holder.present_radio;
}
holder.presentAbsentRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radiogrp, int CheckedId) {
// TODO Auto-generated method stub
boolean isSelected=(CheckedId==R.id.present_radio);
int getposition=(Integer) radiogrp.getTag();
if(CheckedId==R.id.present_radio){
holder.present_radio.setButtonDrawable(R.drawable.radio_button_green);
holder.present_radio.setSelected(true);
//To Uncheck the absent button incase it was checked
holder.absent_radio.setButtonDrawable(R.drawable.radio_background);
holder.absent_radio.setChecked(false);
}
else{
holder.absent_radio.setButtonDrawable(R.drawable.radio_button_red);
holder.absent_radio.setSelected(true);
//To Uncheck the present button incase it was checked
holder.present_radio.setButtonDrawable(R.drawable.radio_background);
holder.present_radio.setChecked(false);
}
}});
return rowView;
}
static class ViewHolder{
int position;
TextView label;
RadioGroup presentAbsentRadio=null;
RadioButton present_radio=null;
RadioButton absent_radio=null;
}
}
I am not able to implement setonItemClickListner() and setOnCheckedChangeListner() at a time.
Here is my Activity class:
public class AttendanceActivity extends ActionBarActivity implements OnClickListener, OnItemClickListener {
LinearLayout listview;
Expandableview EV = new Expandableview();
public static Button chooseclass_btn, choosesection_btn, choosesubject_btn,submit;
static ListView class_listview, section_listview, subject_listview, lv,datalstview;
protected String text = "", classitemclicked,sectionitemclicked,subjectitemclicked,_class,_section;
String StudentAttendanceInfo;
ArrayList<String> studentName = null;// To store the Student name from listview
ArrayList<String> RadioGrp=null;
String[] class_output,section_output,subj_output, stud_data;
ArrayAdapterActivity mlistadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance);
chooseclass_btn = (Button) findViewById(R.id.chooseclass_btn);
choosesection_btn = (Button) findViewById(R.id.choosesection_btn);
choosesubject_btn = (Button) findViewById(R.id.choosesubject_btn);
listview = (LinearLayout) findViewById(R.id.listview_layout);
class_listview = (ListView) findViewById(R.id.class_listview);
section_listview = (ListView) findViewById(R.id.section_listview);
subject_listview = (ListView) findViewById(R.id.subject_listview);
submit=(Button)findViewById(R.id.submit);
datalstview=(ListView) findViewById(android.R.id.list);
//listview.setVisibility(View.GONE);
//************************** onclick Event *****************************
//*******************of Choose Class button*****************************//
chooseclass_btn.setOnClickListener(this);
submit.setOnClickListener(this);
class_listview.setOnItemClickListener(this);
section_listview.setOnItemClickListener(this);
subject_listview.setOnItemClickListener(this);
datalstview.setOnItemClickListener(this);
datalstview.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
int first = view.getFirstVisiblePosition();
int count = view.getChildCount();
Toast.makeText(getApplicationContext(), "Scroll State:: " + scrollState, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Cout of listview items:: " + view.getCount(), Toast.LENGTH_SHORT).show();
if (scrollState == SCROLL_STATE_IDLE || (first + count > view.getCount()) ) {
datalstview.invalidateViews();
}
}
@Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onItemClick(AdapterView<?> listadapter, View v,
final int pos, long id) {
// TODO Auto-generated method stub
if(listadapter.getId()==R.id.class_listview){
classitemclicked=(String)listadapter.getItemAtPosition(pos);
chooseclass_btn.setText(classitemclicked);
class_listview.setVisibility(View.GONE);
new GetDetails(getApplicationContext(), R.id.section_listview).execute();
}
else if(listadapter.getId()==R.id.section_listview){
sectionitemclicked=(String)listadapter.getItemAtPosition(pos);
choosesection_btn.setText(sectionitemclicked);
section_listview.setVisibility(View.GONE);
new GetDetails(getApplicationContext(), R.id.subject_listview).execute();
}
else if(listadapter.getId()==R.id.subject_listview){
subjectitemclicked=(String)listadapter.getItemAtPosition(pos);
choosesubject_btn.setText(subjectitemclicked);
subject_listview.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Subject id" + android.R.id.list, Toast.LENGTH_LONG).show();
new GetDetails(android.R.id.list,classitemclicked,sectionitemclicked).execute();
}
else{
//Here we have to do task to handle the listview items which are clicked
}
}
}