Я пытаюсь реализовать два действия, которые обмениваются информацией между ними, используя намерения. Упражнение № 1 содержит пустой список и кнопку, которая запускает занятие № 2 при нажатии. На занятии № 2 у меня есть несколько полей для ввода текста и кнопка «Сохранить», которая отправляет через методы intent.putExtra информацию в занятие № 1. Проблема заключается в том, что каждый раз, когда я пытаюсь создать новый View с информацией, переданной Activity # 2, список переопределяет первый элемент.
Вы можете увидеть ниже метод OnCreate из Activity # 1:
@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***********//
}
}
Я также присоединяю реализованный пользовательский адаптер.
publi c Класс 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. По какой-то причине он не сохраняет текущие элементы после запуска действия № 2.
Любой совет будет оценен.