问题描述
- android 调用 webservice 返回字符串想在listview中显示。出现问题……
-
下文是 主查询模块 Query_xiaofeijilu.java,package com.gongneng.chaxun; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.StringTokenizer; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.view.View; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import org.json.JSONException; import org.ksoap2.SoapEnvelope; import org.ksoap2.SoapFault; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import com.constant.Constant; import com.example.kehuduan.R; public class Query_xiaofeijilu extends Activity { private Button okButton; private ListView listview; private Handler handler = null; private SimpleAdapter adapter; private List<Map<String,Object>> muldata; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.query_xiaofeijilu); okButton = (Button) this.findViewById(R.id.xiaofeijiluchaxun); listview = (ListView) this.findViewById(R.id.listxiaofeijilu); handler = new Handler(); okButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { new Thread() { @Override public void run() { try { try { getWebservice(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); } }); } public void getWebservice() throws InterruptedException, IOException, XmlPullParserException, JSONException { // 创建属于主线程的handler HttpTransportSE ht = new HttpTransportSE(Constant.SERVICE_URL); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); SoapObject request = new SoapObject(Constant.SERVICE_NS,Constant.QueryAction_chaxunxiaofeijilu); request.addProperty("aac999", "2200000"); envelope.bodyOut = request;// 往出走的数据Out,也就是服务器的传入值 try { ht.call(null, envelope); Object object = (Object) envelope.getResponse();// 就用Object来取 muldata = new ArrayList<Map<String,Object>>(); if (envelope.getResponse() != null) { String a = object.toString(); String b = a.replace("[","");//变[为空 String c = b.replace("]",""); String d = c.replace("{",""); String e = d.replace("}",""); String f = e; // String[] arr = c.split(",");//将字符串转为数组,以,为分割 Map<String, Object> aa = transStringToMap(f); Iterator<?> iter = aa.entrySet().iterator(); while (iter.hasNext()) { Map<String,Object> map=new HashMap<String, Object>(); Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object val = entry.getValue(); map.put(key.toString(),val.toString()); // map.put("text", "测试文本:"); muldata.add(map); } adapter = buildListAdapter(this, muldata); new Thread() { public void run() { handler.post(runnableUi); } }.start(); return; } else { System.out.println("没有返回值"); return; } } catch (SoapFault e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 构建adapter. public SimpleAdapter buildListAdapter(Context context,List<Map<String, Object>> data) { SimpleAdapter adapter = new SimpleAdapter(context, data, R.layout.chaxunxiaofeijilulist, new String[] { "AKB021","AKE038","KS","JS","ROWNUM" }, new int[] { R.id.akb021, R.id.ake038,R.id.ks,R.id.js ,R.id.rownum}); return adapter; } // 构建Runnable对象,在runnable中更新界面 // 更新页面list 需要单独写个线程 Runnable runnableUi = new Runnable() { @Override public void run() { // 更新界面 listview.setAdapter(adapter); Looper.loop(); } }; //=================================== //字符串转MAP public static Map<String, Object> transStringToMap(String mapString){ Map<String, Object> map = new HashMap<String, Object>(); java.util.StringTokenizer items; for(StringTokenizer entrys = new StringTokenizer(mapString, ","); entrys.hasMoreTokens(); map.put(items.nextToken(), items.hasMoreTokens() ? ((Object) (items.nextToken())).toString() : null)) items = new StringTokenizer(entrys.nextToken(), ":"); return map; } }
下文是list页面
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/child" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <!-- 若想隐藏id加入此属性: android:visibility="gone",这样做有点类似于Html中hidden域 android:background="#E3D25E" --> <TextView android:id="@+id/akb021" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#6633FF" android:text=" akb021 " /> <TextView android:id="@+id/ake038" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" ake038 "/> <TextView android:id="@+id/ks" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" ks "/> <TextView android:id="@+id/js" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" js "/> <TextView android:id="@+id/rownum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" rownum "/> </LinearLayout>
主显示页面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/editTextShenfenzheng" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/listxiaofeijilu" android:layout_alignParentTop="true" android:layout_alignRight="@+id/listxiaofeijilu" android:ems="10" android:hint="@string/shenfenzheng" > </EditText> <Button android:id="@+id/chongzhi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="@string/chongzhi" /> <ListView android:id="@+id/listxiaofeijilu" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/xiaofeijiluchaxun" android:layout_alignRight="@+id/chongzhi" android:layout_below="@+id/editTextShenfenzheng" > </ListView> <Button android:id="@+id/xiaofeijiluchaxun" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/chongzhi" android:layout_alignBottom="@+id/chongzhi" android:layout_toLeftOf="@+id/chongzhi" android:text="@string/xiaofeijilu" /> </RelativeLayout>
但是现在只把行数显示出来了,里边没有值,附图:
解决方案
你往simpleadapter中有4个key"AKB021","AKE038","KS","JS","ROWNUM",但map只放1个
while (iter.hasNext()) {
Map<String,Object> map=new HashMap<String, Object>();
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
map.put(key.toString(),val.toString()); //1个 应该map要放4个
// map.put("text", "测试文本:"); //
muldata.add(map);
}
解决方案二:
参考下我这个:http://blog.csdn.net/chinacsharper/article/details/38386779http://blog.csdn.net/chinacsharper/article/details/38386779
解决方案三:
不要沉啊~ 还有没有哪位朋友有方法啊?~? 我现在是直接把webservice的结果集转成String传出来了~ 现在是一条长长的字符串了~
楼上的朋友的方法应该可行,但是有没有更简便的方法?~而且我想显示出那个想PLSQL查询出来的那种列表,不知道还有没有朋友可以帮忙啊~
在此感谢了!
时间: 2024-11-03 08:41:20