How to use List<Status in android?
I am retrieving the information I need here.
getStatus
public java.util.List<Status> getStatus(int limit)
throws EasyFacebookError
Throws:
EasyFacebookError
how do I set the List<Status>
I retrieve to a list in android?
EDIT Here is what i am using to reteive the the status...
List<开发者_开发问答;Status> status;
try {
status = rest.getStatus(10);
} catch (EasyFacebookError e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
How do i set this to a arrayAdapter?
What do you mean a "list in android"? It's giving you a list that you can use already.
To understand what the <Status>
part means, you need to understand generics.
Generics allow you to have methods that take and return the type you need, rather than dealing with possible exceptions or accidentally putting the wrong type into a list.
Without generics, List's methods (it's an interface that things such as ArrayList or LinkedList happen to implement) would look like this (only some methods shown):
boolean add(Object object);
boolean add(int location, Object object);
Object get(int location);
boolean contains(Object object);
Everything is an Object and you could for example, put in anything or get out anything.
If you use a generic List, everything is of type Status instead:
boolean add(Status object);
boolean add(int location, Status object);
Status get(int location);
boolean contains(Status object);
You can then just use your retrieved list and do the following to check if size > 0 and get the first status, as an example (I have no experience with the Facebook API itself so you may have to do something entirely different than this for what you actually need):
if (list.size() > 0) {
Status myStatus = list.get(0);
}
You don't have to worry in this case that list.get(index) doesn't return a Status.
You can definitely pass this list to anything that says it accepts a List, for example an ArrayAdapter.
Check out generics, interfaces, and Android's java.util.List interface for more info on each of those things!
UPDATE
To set this list to an ArrayAdapter, ArrayAdapter has a constructor just for this:
List<Status> status;
try {
status = rest.getStatus(10);
ArrayAdapter<Status> arrayAdapter = new ArrayAdapter<Status>(context, R.layout.list_layout_name, R.id.list_item_textbox, status);
listView.setAdapter(arrayAdapter);
} catch (EasyFacebookError e1) {
// TODO Handle possible errors, of course!
e1.printStackTrace();
}
Notice ArrayAdapter is also generic. You are going to need to make an XML layout file with a TextView element that has the ID you set in the second resource argument. If you just want what the default layout, you use the two parameters android.R.layout.simple_list_item_1
, android.R.id.text1
. context
is most likely this
if you are inside an extended Activity
class, or if not, you would pass a reference of the instance of your activity instead.
See you just pass the list right into the ArrayAdapter!
Note you might want a more complex adapter for more complex views, in which case you would have to subclass an Adapter and override the getView
to do whatever with the Status elements. An ArrayAdapter will just put Status.toString()
into the specified item text view, which may or may not be what you want.
精彩评论