我正在尝试执行两个活动,这些活动使用意图在它们之间交换信息。Activity#1包含一个空的列表视图,以及一个按下该按钮即可启动Activity#2的按钮。在Activity#2上,我有一些文本框字段和一个“保存”按钮,该按钮通过intent.putExtra方法将信息发送到Activity#1。问题是,每次我尝试使用Activity#2传递的信息创建一个新的View时,该列表都会覆盖第一个元素。
您可以在活动#1的OnCreate方法下面看到:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_explorer);
notesList = findViewById(R.id.listviewNotes);
FloatingActionButton myFab = this.findViewById(R.id.fabAddNote);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intentNoteEditor = new Intent(getApplicationContext(), NoteEditor.class);
startActivity(intentNoteEditor);
//Log.i("Lista",notesList.getCount()+"");
}
});
Intent intent =getIntent();
Bundle extras =intent.getExtras();
if(extras!=null){
if(extras.containsKey("isnewNote")){
isnewElement=extras.getBoolean("isnewNote",false);
}
}
if(isnewElement==true){
//***************Fetch data from intent***************//
notetext = intent.getStringExtra("noteText");
notecolor = intent.getStringExtra("noteColor");
notelocation = intent.getStringExtra("noteLocation");
notereminder = intent.getStringExtra("noteReminder");
Note receivednote = new Note(notetext, notecolor, notereminder, notelocation);
MyAdapter adapter = new MyAdapter(this, R.layout.list_item, notesArray);
notesArray.add(receivednote);
notesList.setAdapter(adapter);
//***************End Fetch data from intent***********//
}
}
我还将附加已实现的自定义适配器。
公共类MyAdapter扩展ArrayAdapter {
private Context mContext;
private int mResource;
private ArrayList<Note> mNotes = new ArrayList<>();
private String TAG = "Adapter Class";
public MyAdapter(@NonNull Context context, int resource, ArrayList<Note> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
mNotes = objects;
}
@Override
public int getCount() {
return mNotes.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItem =convertView;
if(listItem==null){
listItem=LayoutInflater.from(mContext).inflate(mResource,parent,false);
Note currentNote = mNotes.get(position);
String text = mNotes.get(position).getText();
String color = mNotes.get(position).getColor();
String location = mNotes.get(position).getLocation();
String reminder = mNotes.get(position).getReminder();
TextView nttxt = listItem.findViewById(R.id.noteText);
TextView ntcolor = listItem.findViewById(R.id.textcolor);
TextView ntrem = listItem.findViewById(R.id.reminder);
TextView ntlocat = listItem.findViewById(R.id.location);
nttxt.setText(text);
ntcolor.setText(color);
ntrem.setText(reminder);
ntlocat.setText(location);
}
return listItem;
}
}
我记录了列表大小,并且始终为1。由于某些原因,启动Activity#2后它不会保留当前元素。
任何建议将不胜感激。
问题是,每当您在Activity#2中按下“保存”按钮时,您都在启动Activity#1的新实例,因此将启动列表中的单个注释。启动Activity2时,需要使用startActivityForResult()方法,然后重写onActivityResult()才能获取返回的数据。活动1看起来可能像这样:
public static final int NEW_NOTE_REQUEST = 23;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_explorer);
notesList = findViewById(R.id.listviewNotes);
FloatingActionButton myFab = this.findViewById(R.id.fabAddNote);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intentNoteEditor = new Intent(getApplicationContext(), NoteEditor.class);
startActivityForResult(intentNoteEditor, NEW_NOTE_REQUEST);
//Log.i("Lista",notesList.getCount()+"");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check the returned result and parse the data
if(resultCode == RESULT_OK && requestCode == NEW_NOTE_REQUEST){
notetext = intent.getStringExtra("noteText");
notecolor = intent.getStringExtra("noteColor");
notelocation = intent.getStringExtra("noteLocation");
notereminder = intent.getStringExtra("noteReminder");
Note receivednote = new Note(notetext, notecolor, notereminder, notelocation);
MyAdapter adapter = new MyAdapter(this, R.layout.list_item, notesArray);
notesArray.add(receivednote);
notesList.setAdapter(adapter);
}
}
然后在Activity#2:
public void onSaveButtonClick(View view) {
Intent intent = new Intent();
// Add note data to intent
// return the result to Activity#1
setResult(RESULT_OK, intent);
finish();
}