RohitAneja
RohitAneja

Reputation: 1011

Updating list adapter with Set<String> and sharedPreferences does not work

I am using custom adapter with text and a button. The text is stored in form of Set in sharedPreferences. (using editor.putStringSet() method)

Whenever i update this set of strings, i call notifyDataSetChanged() but it is not updating the list.

Refer my activity below :-

public class FunctionaScreen extends ActionBarActivity {
private Button button;
private EditText edit;
SharedPreferences prefs;
Set<String> nums;
private ArrayAdapter<String> listAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_functiona_screen);

    prefs = getApplicationContext()
            .getSharedPreferences("CALLBACKPREFS", 0);

    button = (Button) findViewById(R.id.button1);
    edit = ((EditText) findViewById(R.id.editText1));
    nums = prefs.getStringSet("nums", new HashSet<String>(Arrays.asList("")));
    listAdapter = new NumbersAdapter(this, nums.toArray((new String[nums.size()])));

    final ListView mainListView = (ListView) findViewById(R.id.listView1);
    mainListView.setAdapter(listAdapter);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            SharedPreferences.Editor editor = prefs.edit();
            nums.add(edit.getText().toString());
            editor.putStringSet("nums", nums);
            editor.commit();
            mainListView.invalidateViews();
            listAdapter.notifyDataSetChanged();
            Toast.makeText(getApplicationContext(), "Number updated !!", 0)
                    .show();

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.functiona_screen, menu);
    return true;
}

@Override
public void onBackPressed() {
    super.onBackPressed();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

And here is the adapter :-

public class NumbersAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] nums;

public NumbersAdapter(Context context, String[] nums) {
    super(context, R.layout.number_row, nums);
    this.context = context;
    this.nums = nums;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.number_row, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.textViewRow);
    Button btnDelNum = (Button) rowView.findViewById(R.id.btnDelNum);
    btnDelNum.setTag(position);
    textView.setText(nums[position]);

    btnDelNum.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            SharedPreferences prefs = context.getApplicationContext()
                    .getSharedPreferences("CALLBACKPREFS", 0);
            Set<String> n = prefs.getStringSet("nums", null);
            n.remove(nums[(Integer) v.getTag()]);
            SharedPreferences.Editor ed = prefs.edit();
            ed.putStringSet("nums", n);
            ed.commit();
            notifyDataSetChanged();
        }
    });

    return rowView;
}

}

Please suggest why i am not able to update list whenever button is pressed from activity or remove button (in adapter) is pressed.

Upvotes: 0

Views: 420

Answers (1)

sagix
sagix

Reputation: 660

Modifing your Set in Activity or array in Adapter does not modify data stores inside the ArrayAdapter.

You should use the add and remove method of ArrayAdapter: http://developer.android.com/reference/android/widget/ArrayAdapter.html#add(T) http://developer.android.com/reference/android/widget/ArrayAdapter.html#remove(T)

Also commit method of your Editor can block the UI take a look of apply method.

Upvotes: 1

Related Questions