-1

For example, I have a simple ListView App. Each item has 3 TextView

This is my JSON file

{
  "list-view": [
    {
      "date": "23 October 2020",
      "description": "Lorem ipsum dolor sit amet.",
      "name": "Carl Johnson"
    },
    {
      "date": "27 October 2020",
      "description": "Lorem ipsum dolor sit amet.",
      "name": "Sean Johnson"
    }
  ]
}

And here's my Java Code

public class ListViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        ListView listView = findViewById(R.id.list_example);
        final ArrayList<ListViewHelperClass> arrayList = new ArrayList<>();
        final ListViewAdapter listViewAdapter = new ListViewAdapter(getApplicationContext(), arrayList);

        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
        Query query = FirebaseDatabase.getInstance().getReference().child("list-view");
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (int i = 0; i < 2; i++) {

                    ListViewHelperClass listViewHelperClass = new ListViewHelperClass();

                    String name = dataSnapshot.child(String.valueOf(i)).child("name").getValue(String.class);
                    String date = dataSnapshot.child(String.valueOf(i)).child("date").getValue(String.class);
                    String description = dataSnapshot.child(String.valueOf(i)).child("description").getValue(String.class);

                    listViewHelperClass.setName(name);
                    listViewHelperClass.setDate(date);
                    listViewHelperClass.setDescription(description);
                    arrayList.add(listViewHelperClass);

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        listView.setAdapter(listViewAdapter);

    }
}

I've searched a lot of post and use this code. But, is it the best way? Or someone have a more efficient way to do? Especially for an app that support offline mode.

Rezky
  • 41
  • 8
  • 1
    @AlexMamo: I see no misuse of asynchronous API calls here, and keeping the call to `setAdapter` outside of `onDataChange` is actually more idiomatic as it consumes fewer resources. Can you explain why you think this is a duplicate of the question you linked? – Frank van Puffelen Feb 21 '21 at 16:06
  • 1
    You're missing a call to `listViewAdapter.notifyDataSetChanged()` in your `onDataChange` method. Without such a call, Android doesn't know that it needs to repaint the list view. – Frank van Puffelen Feb 21 '21 at 16:07
  • did you make that change, and did it help? – Frank van Puffelen Feb 22 '21 at 02:08
  • 1
    @FrankVanPuffelen I did it, it works – Rezky Feb 22 '21 at 02:46
  • @FrankvanPuffelen The duplicate it's not about the issue of an asynchronous API call but about the way of displaying the data in the ListView. And yes, `listViewAdapter.notifyDataSetChanged()` makes more sense. I just updated the duplicate answer accordingly. – Alex Mamo Feb 22 '21 at 08:30
  • @RezkyAF Good to hear that you made it work. One thing to notice, is that a ListView nowadays is considered a legacy. The [ListView is replaced by RecyclerView](https://stackoverflow.com/questions/50079026/why-are-some-views-inside-the-legacy-tab-in-android-studio-3-1-and-what-replaces). Here you can find a [working example](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849). – Alex Mamo Feb 22 '21 at 08:32

1 Answers1

1

You're missing a call to listViewAdapter.notifyDataSetChanged() in your onDataChange method. Without such a call, Android doesn't know that it needs to repaint the list view.

Every time you modify the data set that the list view shows, you need to notify the adapter of this. In your case that should be as simple as:

arrayList.add(listViewHelperClass);
listViewAdapter.notifyDataSetChanged();
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807