Android ListView Bind to Custom Class
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<Sessi开发者_如何学运维on> 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;
};
}
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;
}
});
}
Check this out.
Simple List Adapter
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.
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).
精彩评论