程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> map-android 調用 webservice 返回字符串想在listview中顯示。出現問題……

map-android 調用 webservice 返回字符串想在listview中顯示。出現問題……

編輯:編程綜合問答
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);
                    }
danielinbiti
asd123asdas1
asd123asdas1
asd123asdas1
asd123asdas1
danielinbiti
asd123asdas1
asd123asdas1
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved