Reputation: 2288
I have a Custom ListView created from XML Layout. I want to bind that to my Custom Data Class. How do i Convert that class to a ListAdapter and bind the layout to the ListView. The Code of the class is as Follows:
public class Sessions {
private int status;
private List<Session> sessions;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public List<Session> getSessions() {
return sessions;
}
public void setSessions(List<Session> sessions) {
this.sessions = sessions;
}
public static class Session {
public Boolean active;
public String contributor_covu_id;
public String created_at;
public String key;
public String status;
public String name;
public String type;
};
}
Upvotes: 1
Views: 4570
Reputation: 23514
Create an Activity based on ListActivity and use something like what's below in your onCreate. It'll make a list with a simple two line layout for each row and set the text to the session name and session status.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final List<Sessions.Session> sessions = new Sessions().getSessions();
setListAdapter(new BaseAdapter() {
public int getCount() {
return sessions.size();
}
public Object getItem(int pos) {
return sessions.get(pos);
}
public long getItemId(int pos) {
return pos;
}
public View getView(int pos, View view, ViewGroup viewGroup) {
if (view == null) {
view = View.inflate(ViewTest.this, android.R.layout.two_line_list_item, null);
}
Sessions.Session session = (Sessions.Session) getItem(pos);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText(session.name);
text = (TextView) view.findViewById(android.R.id.text2);
text.setText(session.status);
return view;
}
});
}
Upvotes: 4
Reputation: 6794
You need to extend the ArrayAdapter class. Here is a good tutorial that will show you how to override the correct methods (especially the getView() method).
Upvotes: 0
Reputation: 16110
Check this out.
You can search for tutorials on how to make a simple List Adapter. This shows one simple approach of binding your list to an adapter. You can find many others even more advanced examples.
Upvotes: 0