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.