I have spent the better half of the day trying to figure out how to read data from my firebase database and display multiple fields in a ListView
via an ArrayAdapter
. After finally getting my ListView to display one piece of data in one textview, I am stuck trying to display two other fields.
In my MainActivity
class I used the android.R.layout.simple_list_item_1
in the Adapter constructor because I was told that the textViewResourceID argument must only contain one TextView. So to reiterate my problem: I can not figure out how to display multiple fields with an array adapter. This app currently only displays the make field. Thanks so much in advance, I only started android development about a week ago! Here is my code below:
SearchModel.java
public class SearchModel {
public String make;
private String model;
private String stock;
public SearchModel() {}
public String getMake() {
return this.make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getStock() {
return stock;
}
public void setStock(String stock) {
this.stock = stock;
}
}
MainActivity.java
(I omitted the irrelevant bottom section of the class after onCreate()
)
public class MainActivity extends AppCompatActivity {
private DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("users").child("user1");
private ListView mListView;
private TextView tv_make;
private TextView tv_model;
private TextView tv_stock;
private ArrayList<String> list = new ArrayList<>();
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tv_make = (TextView) findViewById(R.id.tv_list_item_make);
tv_model = (TextView) findViewById(R.id.tv_list_item_model);
tv_stock = (TextView) findViewById(R.id.tv_list_item_stock);
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
String value = snapshot.child("make").getValue(String.class);
list.add(value);
mListView = (ListView) findViewById(R.id.lv_searches);
adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
mListView.setAdapter(adapter);
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
Here is the ListView
in content.main.xml
:
<ListView
android:id="@+id/lv_searches"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
And here is the list_item.xml
which is supposed to sit inside my ListView. This is where I would like to display the three String values (make, model, stock) in an instance of SearchModel.java
.
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="10dp">
<TextView
android:id="@+id/tv_list_item_make"
android:layout_width="wrap_content"
android:layout_height="50dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_list_item_model"
android:layout_width="wrap_content"
android:layout_height="40dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<TextView
android:id="@+id/tv_list_item_stock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>