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 Array
after two days of being stuck I was able to answer my own question! Hopefully this will help future beginners stuck trying to display a custom view within an ArrayAdapter. Basically, I created a custom ArrayAdapter
class which could hold three TextViews
inside of it.
I am in no means an expert and will not be able to explain this as well as others, so instead, I will paste a link to the two video tutorials that I followed and had success with.
Android Beginner Tutorial #8 - Custom ListView Adapter For Displaying Multiple Columns https://www.youtube.com/watch?v=E6vE8fqQPTE&t=392s
Android Beginner Tutorial #9 - Custom ListView Adapter [With Loading Animation] https://www.youtube.com/watch?v=SApBLHIpH8A
Here is my updated code below:
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private static ArrayList searchArrayList;
private LayoutInflater mInflater;
private Context mContext;
private int lastPosition;
/**
* Holds elements in a view
*/
static class ViewHolder {
TextView make;
TextView model;
TextView stock;
}
public CustomAdapter(Context context, ArrayList results) {
searchArrayList = results;
mContext = context;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return searchArrayList.size();
}
@Override
public Object getItem(int position) {
return searchArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String make = searchArrayList.get(position).getMake();
String model = searchArrayList.get(position).getModel();
String stock = searchArrayList.get(position).getStock();
final View result;
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.make = (TextView) convertView.findViewById(R.id.tv_list_item_make);
holder.model = (TextView) convertView.findViewById(R.id.tv_list_item_model);
holder.stock = (TextView) convertView.findViewById(R.id.tv_list_item_stock);
result = convertView;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
result = convertView;
}
Animation animation = AnimationUtils.loadAnimation(mContext,
(position > lastPosition) ? R.anim.load_down_anim : R.anim.load_up_anim);
result.startAnimation(animation);
lastPosition = position;
holder.make.setText(make);
holder.model.setText(model);
holder.stock.setText(stock);
return convertView;
}
}
MainActivity.java
(Only the onCreate
method)protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
String make = snapshot.child("make").getValue(String.class);
String model = snapshot.child("model").getValue(String.class);
String stock = snapshot.child("stock").getValue(Long.class).toString();
SearchModel searchModel = new SearchModel(make, model, stock);
searchList.add(searchModel);
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), searchList);
mListView = (ListView) findViewById(R.id.lv_searches);
mListView.setAdapter(customAdapter);
}
@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) {
}
});