我有自定義列表,每行包含圖像,文本視圖和單選按鈕而不是單選組。 我只選擇一個單選按鈕。我想要獲取與所選單選按鈕相同行中的圖像。然後我用這個圖像設置背景 請幫我。我如何在選定的單選按鈕的同一行中獲取圖像?
//rowadapter
public class rowadapter extends ArrayAdapter<String> {
private final Activity context;
int layoutResourceId;
private final String[] web;
private final Integer[] imageId;
int selectedPosition = -1;
SharedPreferences sharedPref;
public rowadapter(Activity context,String[] web, Integer[] imageId) {
super(context,R.layout.item_listview, web);
this.context = context;
this.web = web;
this.imageId = imageId;
sharedPref = context.getSharedPreferences("position",Context.MODE_PRIVATE);
}
public View getView(final int position, View row, ViewGroup parent)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
backgroundholder holder = null;
View rowView=row;
rowView= inflater.inflate(R.layout.item_listview, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
final ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
holder = new backgroundholder();
holder.radiobutton = (RadioButton)rowView.findViewById(R.id.radiobutton);
holder.radiobutton.setChecked(position == selectedPosition);
holder.radiobutton.setTag(position);
int checkedpos=sharedPref.getInt("position",-1);
if(checkedpos==position)
{
holder.radiobutton.setChecked(true);
}
holder.radiobutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
selectedPosition = (Integer)view.getTag();
RadioButton radio = (RadioButton)view;
if(radio.isChecked())
{
Editor editor=sharedPref.edit();
editor.putInt("position", selectedPosition);
editor.commit();
}
Intent i=new Intent(getContext(), Preferences.class);
context.startActivity(i);
notifyDataSetInvalidated();
}
});
return rowView;
}
static class backgroundholder
{
RadioButton radiobutton;
}
}
//object class
import android.widget.RadioButton;
public class Item {
String txt;
Integer drawable;
RadioButton radiobutton;
boolean checked;
public Item(String txt,Integer drawable,RadioButton radiobutton)
{
this.txt=txt;
this.drawable=drawable;
this.radiobutton=radiobutton;
}
public String gettxt()
{
return txt;
}
public Integer getInteger()
{
return drawable;
}
public boolean ischecked()
{
return checked;
}
public void setchecked(boolean checked)
{
this.checked=checked;
}
}
走線佈局'horizontal'並添加於一身行 – Siva
我不明白答案.. – user3631823
你在XML文件中使用哪種佈局?這裏發佈的XML文件 – Siva