package com.example.dc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import com.lianjie.connect.Connect;
import com.zdm.util.Util;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class XZ extends Activity {
ListView listViewzh;
String[] zhtv;
String[] zhuangtai;
String zhuohaovlue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xuanzhuo);
if(android.os.Build.VERSION.SDK_INT > 13) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
zhuohaoServlet();
listViewzh=(ListView)findViewById(R.id.listViewzh);
/**********************************************************/
ArrayList<Map<String, Object>> mlist = new ArrayList<Map<String, Object>>();
for (int i = 0; i < zhtv.length; i++) { //提示这句有问题
Map<String, Object> map = new HashMap<String, Object>();
map.put("zhuoming", zhtv[i]);
map.put("zzzt", zhuangtai[i]);
mlist.add(map);
}
SimpleAdapter adapter1 = new SimpleAdapter(XZ.this,
mlist,
R.layout.xuanzhuo1,
new String[] { "zhuoming","zzzt"},
new int[] { R.id.zhtv,R.id.zhuangtai});
listViewzh.setAdapter(adapter1);
}
private void zhuohaoServlet()
{
String url=Util.httpURL+"zhangdanServlet";
String paraname[]={"zhuohao"};
String paravalue[]={zhuohaovlue};
JSONArray jsonainfo =Connect.dopost(url, paraname, paravalue, 1,"info1");
if(jsonainfo==null)
{
Toast.makeText(XZ.this, "暂时无法读取数据!", Toast.LENGTH_LONG)
.show();
}
else
{
JSONObject jsonobject;
try {
zhtv=new String[jsonainfo.length()];
zhuangtai=new String[jsonainfo.length()];
for(int i=0;i<jsonainfo.length();i++)
{
jsonobject = jsonainfo.getJSONObject(i);
zhtv[i]=jsonobject.getString("zhuoming");
zhuangtai[i]=jsonobject.getString("zzzt");
}
} catch (Exception e) {
Toast.makeText(XZ.this, "读取数据失败!", Toast.LENGTH_LONG)
.show();
}
}
}
}
布局文件中用到的控件
<TextView
android:id="@+id/zhtv"
android:text=""
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/zhuangtai"
android:text=""
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"/>