Reputation: 1
customlist and contain radiobuttons and I want to select one radiobutton and it works. But i want to save that raidobutton which i select. I used sharedpreference but i couldnt do it . i know the sharedpreference is the good way to save value in android . sorry for bad english . help me please .
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;
}
Upvotes: 0
Views: 77
Reputation: 18281
It looks like a spelling mistake, you've written:
int checkedpos=sharedPref.getInt("poistion",-1);
but it should be:
int checkedpos=sharedPref.getInt("position",-1);
For this reason, I usually like to use a constant, so you make an instance variable like:
public static final String POSITION = "position";
And then access the value like this:
int checkedpos = sharedPref.getInt(POSITION, -1);
///...
editor.putInt(POSITION, selectedPosition);
Which will make it easier to spot a spelling mistake.
Upvotes: 1