Mordred Mordred - 3 months ago 89
Android Question

Use Firebase list adapter with Card View

I have my firebase list adapter as follows

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mListview = (ListView) findViewById(R.id.listview);

mAuth = FirebaseAuth.getInstance();
//the post list shit is happening here

DatabaseReference root = FirebaseDatabase.getInstance().getReference();
FirebaseUser user = mAuth.getCurrentUser();
DatabaseReference postRef = root.child("users").child(user.getUid().toString()).child("posts");

ListAdapter adapter = new FirebaseListAdapter<PostList>(this,PostList.class,android.R.layout.simple_list_item_1,postRef) {
@Override
protected void populateView(View view, PostList postList, int i) {
TextView text = (TextView)view.findViewById(android.R.id.text1);
text.setText(postList.getBody());

}

};
mListview.setAdapter(adapter);


I have a normal xml with a list view and another xml with its card view

my problem is that i cannot find a way to relate my cardview xml file to my activity, so my list just keep showing up as a normal list,
While initializing the firebase list adapter, I found an
R.layout_simple_list_item

im not sure exactly how that works and i have googled, nothing explains definitely why i cant just say
R_layout_my_card_view_xml
instead. Also, please let me know if i am going about this all wrong.

Answer

I got the answer to this already, Just in case anyone is going through the same problem, just as Linxy clearly stated, it is better to use FirebaseRecyclerAdapter, it is a modified version of the ListAdapter.

However if you feel like you still want to use a ListAdapter, then:

  1. First note that you cannot directly edit android.R.layout.items, instead create your own layout, say you have row.xml in your layout files, use R.layout.row as your populateView() arguments and not android.R.layout.row, the latter wont find anything because those layout files are stored in your SDK folder.

  2. Please, consider the recycler adapter, its not that hard.