0
自定義列表和包含單選按鈕,我想選擇一個單選按鈕,它的工作原理。但我想保存我選擇的那個raidbutton。 我使用sharedpreference,但我無法做到這一點。我知道sharedpreference是在android中節省價值的好方法。 對不起英文不好。 請幫我。選擇一個單選按鈕,當我回到這個活動時,選擇仍然存在?
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);
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("poistion",-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();
}
notifyDataSetInvalidated();
}
});
return rowView;
static class backgroundholder
{
RadioButton radiobutton;
}
哦是的......:S:S:S:S:S:S:S非常感謝 – user3631823
@ user3631823沒有probs,你會如此友善接受這個答案嗎?謝謝 – user184994
我怎麼接受?我不能投票,直到nw – user3631823