我有一个可由用户更新的列表,但在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;
}
});
}
CustomAdapter
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;
}
}
有什么主意吗?
答案 0 :(得分:0)
只需要更改一行。只需如下更改arrayAdapter.notifyDataSetChanged()即可。
class Ship{
public:
int max_HP;
int HP;
int DMG;
};
class Fleet{
public:
int amount_of_ships;
Ship * ship = new Ship[amount_of_ships];
Fleet(int fleet_size)
{
amount_of_ships=fleet_size;
}
};
class Player{
public:
string name;
Fleet fleet{1}; //this line
};
答案 1 :(得分:0)
显示适配器对象不可访问的错误。
您实际上是在尝试更新已被破坏的活动。从MainActivity本身通知适配器。
答案 2 :(得分:0)
在我看来,发生错误是因为您将CustomAdapter定义为 private 类,因此您不能在其外部访问其方法。
CustomAdapter应该定义为 public (如果所有内容都位于同一软件包中,则可以访问软件包)。
话虽如此,我也同意其他评论,指出这不是调用 notifyDataSetChanged 的正确方法(如果您对此仍有疑问,我建议为此提出一个不同的问题)。 / p>