![I am using custom adapter for a listview in which i have three items text,buttons and radio buttons.I can select only one row at a time with the help of radio buttons.Now what i want,when i select the row using radio button the particular row of selected radio button should be set as with some color. here is my custom adapter code code in which all items are there.
package com.pramod.customlistviewwithradiobutton;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class CustomAdapter extends ArrayAdapter<Item> {
private final Context context;
private boolean userSelected = false;
private RadioButton mCurrentlyCheckedRB;
private final ArrayList<Item> itemList;
public CustomAdapter(Context context, ArrayList<Item> itemList) {
super(context, R.layout.row_item, itemList);
this.context = context;
this.itemList = itemList;
}
@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// Item rowItem = getItem(position);
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.row_item, parent, false);
// 3. Get the two text view from the rowView
Button btn = (Button) rowView.findViewById(R.id.button1);
TextView tv1 = (TextView) rowView.findViewById(R.id.textView1);
TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
RadioButton radio = (RadioButton) rowView.findViewById(R.id.radioButton1);
// 4. Set the text for textView
tv1.setText(itemList.get(position).getName());
tv2.setText(itemList.get(position).getAddress());
System.out.println(""+getCount());
if (position == getCount() - 1 && userSelected == false) {
// radio.setChecked(true);
mCurrentlyCheckedRB = radio;
} else {
radio.setChecked(false);
}
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "b "+position, Toast.LENGTH_LONG).show();
}
});
radio.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mCurrentlyCheckedRB != null) {
if (mCurrentlyCheckedRB == null)
mCurrentlyCheckedRB = (RadioButton) v;
mCurrentlyCheckedRB.setChecked(true);
Toast.makeText(context, ""+position, Toast.LENGTH_LONG).show();
convertView.setBackgroundColor(Color.BLUE);
}
if (mCurrentlyCheckedRB == v)
return;
mCurrentlyCheckedRB.setChecked(false);
((RadioButton) v).setChecked(true);
mCurrentlyCheckedRB = (RadioButton) v;
}
});
return rowView;
}
}
Here ,Note i don't have to put onclickitem on listview ,because i have a click on button and textview.][1]