使用"设置<String>和共享首选项"更新列表适配器不起作用



我正在使用带有文本和一个按钮的自定义适配器。该文本以共享流程中的设置形式存储。(使用editor.putstringset()方法)

每当我更新这组字符串时,我都会称呼NotifyDataSetchanged(),但它没有更新列表。

请参阅下面的我的活动: -

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);
}

}

这是适配器: -

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;
}

}

请建议为什么按下活动或删除按钮(在适配器中)按下按钮时我无法更新列表。

在适配器中修改活动或数组中的集合不会修改数组中的数据存储。

您应该使用 add remove arrayadapter 的方法:http://developer.android.com/reference/android/widget/arrayadapter.html#add(T)http://developer.android.com/reference/android/widget/arrayadapter.html#remove(t)

commit 编辑器的方法可以阻止UI apply 方法。

最新更新