Search code examples
androidandroid-listview

Android: Why the listview.add function is always adding elements on position 1 in the list


I am trying to implement two activities that are exchanging information between them using intents. Activity#1 contains an empty listview and a button that starts Activity#2 when pressed. On Activity#2 I have some textbox fields and a "Save" button that sends through intent.putExtra methods information to Activity#1. The issue is that each time I try to create a new View with the information passed by Activity#2, the list is overriding the first element.

You can see below the OnCreate method from 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***********//
    }
}

I am also attaching the custom adapter implemented.

public class MyAdapter extends 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;
}

}

I logged the list size and it is always 1. For some reason, it does not keep the current elements after the Activity#2 is launched.

Any advice will be appreciated.


Solution

  • The problem is that every time you press the "Save" button in Activity#2 you are launching a new instance of Activity#1 hence the single note in the list. You need to use the startActivityForResult() method when launching Activity2 and then override onActivityResult() in order to get the data returned data. Activity#1 can look like this:

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

    And then in 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();
    }
    

    You can also achieve the same function by creating a shared data repository, like a singleton class that will hold your notes list and both the activities will have a reference to the same notes list.