'ListView is not updating once I add elements to the array
package com.example.pillappreal;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public ArrayList<Drug> list = new ArrayList<>();
public ArrayList<String> nameList = new ArrayList<>();
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
nameList.add("Test Drug");
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, nameList);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.listView1);
listView.setAdapter(adapter);
nameList.add("Test Drug 3");
}
public void addNewDrug(View view) {
Intent intent = new Intent(this, AddNewDrugActivity.class);
startActivity(intent);
}
public void testMethod(View view) {
Intent intent = new Intent(this, MainActivity.class);
}
public void addNewDrug(Drug drug) {
list.add(drug);
nameList.add(drug.name);
nameList.add("Test Drug 2");
adapter.notifyDataSetChanged();
}
}
I want my ListView to update as I add things to the array. It doesn't work. I added a notifyDataSetChanged() to the updater code, but it does nothing. I've used Log.d tags to make sure the Arrays actually have their values, at pretty much every point of the code and they do. The issue seems to be that the ListView doesn't update since its in onCreate which runs once. I can't put it anywhere else though.
Solution 1:[1]
I think an other solution is that you call the notifyDatasetChange() in the onResume() function (You should override the onResume() function)
Solution 2:[2]
You wrote this: "addNewDrug(Drug drug) is called in a separate activity when a button is pushed. A new drug object is created and added to the existing array"
The notifyDatasetChanged() function is good when if you add the new item where your list is:
Add an EditText and a Button to your main activity. And when you add a new item call the notifyDatasetChanged() function.
If you want to use a new activity for this, you have to call the activity for result. The ActivityForResult function is deprecated. I offer this technic if you want a result (in Java)
// You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { if (result.getResultCode() == Activity.RESULT_OK) { // There are no request codes Intent data = result.getData(); doSomeOperations(); } } });public void openSomeActivityForResult() { Intent intent = new Intent(this, SomeActivity.class); someActivityResultLauncher.launch(intent); }
I made a github project for you: https://github.com/Asuki/SimpleListView
P.S.: I believe this question is the continuation of the next question: Why won't my programmatically created textview work? If I am write, please accept my answer if it helped for you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Seng Phrakonkham |
| Solution 2 |
