日期:2014-05-16  浏览次数:20400 次

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;
?}

}

package josecgomez.com.android.dev.webservice;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import josecgomez.com.android.dev.webservice.objects.alerts;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class main extends Activity {
    /** Called when the activity is first created. */
	//ListView that will hold our items references back to main.xml
	ListView lstTest;
	//Array Adapter that will hold our ArrayList and display the items on the ListView
	AlertsAdapter arrayAdapter;

	//List that will  host our items and allow us to modify that array adapter
	ArrayList<alerts> alrts=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Initialize ListView
        lstTest= (Li