【发布时间】:2019-08-18 14:34:39
【问题描述】:
我有一个可由用户更新的列表,但 notifyDataSetChanged() 出现错误我正在做一个待办事项列表,当我在数组适配器中添加 notifyDataSetChanged() 时,它显示错误提示
错误:ArrayAdapter 中的 notifyDataSetChanged() 定义在不可访问的类或接口中
无法访问“android.widget.ArrayAdapter”中的“notifyDataSetChanged()”
note_editor 发生错误。 以下是我的代码:
note_editor(另一个活动)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_editor);
EditText editText = (EditText) findViewById(R.id.editText);
Intent intent = getIntent();
noteId = intent.getIntExtra("noteId", -1);
// note id variable transferred from main activity
if (noteId != -1) {
editText.setText(MainActivity.notes.get(noteId));
} else {
MainActivity.notes.add("");
noteId = MainActivity.notes.size() - 1;
MainActivity.arrayAdapter.notifyDataSetChanged(); //error happened here
}
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
ListView listView = (ListView) findViewById(R.id.listView);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
SharedPreferences sharedPreferences =
getApplicationContext().getSharedPreferences
("com.example.application1", Context.MODE_PRIVATE);
HashSet<String> set = (HashSet<String>)
sharedPreferences.getStringSet("notes", null);
if (set == null) {
notes.add("Example note");
} else {
notes = new ArrayList(set);
}
arrayAdapter = new CustomAdapter
(this, R.layout.simplerow, notes);
listView.setAdapter(arrayAdapter);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new
Intent(getApplicationContext(),note_editor.class);
startActivity(intent);
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick
(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(),
note_editor.class);
intent.putExtra("noteId", i);
startActivity(intent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick
(AdapterView<?> adapterView, View view, int i, long l) {
final int itemToDelete = i;
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure?")
.setMessage("Do you want to delete this note?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
notes.remove(itemToDelete);
arrayAdapter.notifyDataSetChanged();
SharedPreferences sharedPreferences =
getApplicationContext().getSharedPreferences("com.example.application1",Context.MODE_PRIVATE);
HashSet<String> set = new HashSet(MainActivity.notes);
sharedPreferences.edit().putStringSet("notes", set).apply();
}
}
)
.setNegativeButton("No", null)
.show();
return true;
}
});
}
自定义适配器
private class CustomAdapter extends ArrayAdapter<String> {
Context context;
ArrayList<String> notes = new ArrayList<>();
int layoutResourceId;
public CustomAdapter(Context context, int layoutResourceId,
ArrayList<String> objects) {
super(context, layoutResourceId, objects);
this.layoutResourceId = layoutResourceId;
this.notes=objects;
this.context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckBox chBox = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.simplerow,
parent, false);
chBox = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(chBox);
chBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = findViewById(R.id.rowTextView);
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
arrayAdapter.notifyDataSetChanged();
}
});
}
return convertView;
}
}
有什么想法吗?
【问题讨论】:
-
为什么要更新另一个Activity的适配器??
-
因为我打算做一些事情,比如当我点击浮动操作按钮时,它会将我重定向到一个允许我编写文本的新页面。这是不正确的做法吗?
-
只更新数据。当使用带有 listView 的 Activity 时,只需读取数据并设置适配器。
标签: android listview android-arrayadapter notifydatasetchanged