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.xmlListView lstTest;//Array Adapter that will hold our ArrayList and display the items on the ListViewAlertsAdapter arrayAdapter;//List that will host our items and allow us to modify that array adapterArrayList<alerts> alrts=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Initialize ListView lstTest= (ListView)findViewById(R.id.lstText); //Initialize our ArrayList alrts = new ArrayList<alerts>(); //Initialize our array adapter notice how it references the listitems.xml layout arrayAdapter = new AlertsAdapter(main.this, R.layout.listitems,alrts); //Set the above adapter as the adapter of choice for our list lstTest.setAdapter(arrayAdapter); //Instantiate the Web Service Class with he URL of the web service not that you must pass WebService webService = new WebService("http://www.sumasoftware.com/alerts/GetAlerts.php"); //Pass the parameters if needed , if not then pass dummy one as followsMap<String, String> params = new HashMap<String, String>();params.put("var", "");//Get JSON response from server the "" are where the method name would normally go if needed example// webService.webGet("getMoreAllerts", params);String response = webService.webGet("", params);try{//Parse Response into our objectType collectionType = new TypeToken<ArrayList<alerts>>(){}.getType();//JSON expects an list so can't use our ArrayList from the lstartList<alerts> lst= new Gson().fromJson(response, collectionType);//Now that we have that list lets add it to the ArrayList which will hold our items.for(alerts l : lst){alrts.add(l);}//Since we've modified the arrayList we now need to notify the adapter that//its data has changed so that it updates the UIarrayAdapter.notifyDataSetChanged();}catch(Exception e){Log.d("Error: ", e.getMessage());} }}
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/
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer