listView显示对象以及access any RESTFull service that uses JSON
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? android:orientation="vertical"
??? android:layout_width="fill_parent"
??? android:layout_height="wrap_content"
??? >
<ListView
??? android:layout_width="fill_parent"
??? android:layout_height="wrap_content"
??? android:id="@+id/lstText"
??? />
</LinearLayout>
listitems.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
?android:layout_width="fill_parent" android:layout_height="fill_parent">
?<LinearLayout
?android:orientation="vertical"
?android:layout_width="0dip" android:layout_weight="1"
?android:layout_height="fill_parent">
??<TextView
??android:layout_width="fill_parent"
??android:layout_height="wrap_content"
??android:id="@+id/txtAlertText" />
??<TextView
??android:layout_width="fill_parent"
??android:layout_height="wrap_content"
??android:id="@+id/txtAlertDate" />
?</LinearLayout>
</LinearLayout>
package josecgomez.com.android.dev.webservice;
import java.util.List;
import josecgomez.com.android.dev.webservice.objects.alerts;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AlertsAdapter extends ArrayAdapter<alerts> {
?int resource;
?String response;
?Context context;
?//Initialize adapter
?public AlertsAdapter(Context context, int resource, List<alerts> items) {
??super(context, resource, items);
??this.resource=resource;
?}
?@Override
?public View getView(int position, View convertView, ViewGroup parent)
?{
??LinearLayout alertView;
??//Get the current alert object
??alerts al = getItem(position);
??//Inflate the view
??if(convertView==null)
??{
???alertView = new LinearLayout(getContext());
???String inflater = Context.LAYOUT_INFLATER_SERVICE;
???LayoutInflater vi;
???vi = (LayoutInflater)getContext().getSystemService(inflater);
???vi.inflate(resource, alertView, true);
??}
??else
??{
???alertView = (LinearLayout) convertView;
??}
??//Get the text boxes from the listitem.xml file
??TextView alertText =(TextView)alertView.findViewById(R.id.txtAlertText);
??TextView alertDate =(TextView)alertView.findViewById(R.id.txtAlertDate);
??//Assign the appropriate data from our alert object above
??alertText.setText(al.alerttext);
??alertDate.setText(al.alertdate);
??return alertView;
?}
}
?Please note that the attached jar file contains a compiled copy of the GSON library available at http://code.google.com/p/google-gson/.
以上是转帖 http://www.josecgomez.com/2010/05/04/android-accessing-restfull-json-services-library/
http://www.josecgomez.com/2010/04/30/android-accessing-restfull-web-services-using-json/