【问题标题】:Autocomplete textview data Fetch from ksoap webservice, Using Search icon Onclick request with asynchronous task自动完成 textview 数据从 ksoap webservice 获取,使用搜索图标 Onclick 请求和异步任务
【发布时间】:2015-04-02 06:55:55
【问题描述】:

我们如何从 ksoap Web 服务获取数据,显示在 Android Autocomplete textview 搜索建议中,使用 Onclick Search 按钮。

【问题讨论】:

  • 你有代码示例可以提供更好的理解吗?
  • 我在代码中添加了自动完成文本视图,但没有显示数据
  • 在您的问题中发布现有代码总是一个好主意。
  • 我已经发布了我的代码,请检查并回复我。请

标签: android autocompletetextview android-ksoap2


【解决方案1】:

EditText 未显示建议​​。

AutoCompleteTextView 在editText 中提供建议。

首先你需要从服务器解析数据。 然后使用该数据制作适配器并在 AutoCompleteTextView 中设置。

更多信息建议您查看this blog

为了您的参考,还请检查this link 以解析 ksoap Webservice。

【讨论】:

  • 但是这个链接显示地址位置,在我的问题中显示从 ksoap webservcie 获取指定的数据,然后显示在自动完成文本视图中,我没有找到任何这样的代码。
  • 确保您从 web 服务中正确解析并填写您想要在 autocompletetextview 中显示的确切数据。
【解决方案2】:

从 ksoap webservice 获取自动完成文本视图数据的工作代码,使用搜索图标单击请求。

它将显示来自 Ksoap WebService 的建议。

package com.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.LabeledIntent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class FindCity extends ActionBarActivity implementsOnItemClickListener, OnClickListener 
        {

    AutoCompleteTextView SearchAutoComplte;
    Button searchicon;

    // for WebService 
        private static final String SOAP_ACTION2 = "http:*************";
        private static final String NAMESPACE2 = "http:********************";
        private static final String URL2 = "http:****************";
        private static final String METHOD_NAME2 = "**********";
        private String TAG2 = "City";
        public static String Status_Response = "";


           public String Autocomplete_SearchValue;
           public  String  Cityname;

           String[] CITYNAME;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SearchAutoComplte=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
        Autocomplete_SearchValue =SearchAutoComplte.getText().toString();

        searchicon = (Button) findViewById(R.id.Search_iconimage);

        searchicon.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.Search_iconimage:



                AsyncCallWS task = new AsyncCallWS();
                // Call execute
                task.execute();

            break;

        }

    }

        private class AsyncCallWS extends AsyncTask<String, Void, Void> {
            @Override
            protected Void doInBackground(String... params) {
                Log.i(TAG2, "doInBackground");
                try {

                    getdata(Autocomplete_SearchValue);

                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(),"error caught in do in background", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();

                }
                return null;

                // return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                Log.i(TAG2, "onPostExecute");

                try {



                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(FindCity.this,android.R.layout.select_dialog_singlechoice, CITYNAME);

                    //SearchEdittext.setThreshold(1);
                    SearchAutoComplte.setAdapter(adapter);




                } catch (Exception e) {
                    Log.i(TAG2, "Error");

                    e.printStackTrace();
                }

            }

            @Override
            protected void onPreExecute() {
                Log.i(TAG2, "onPreExecute");

            }

            @Override
            protected void onProgressUpdate(Void... values) {
                Log.i(TAG2, "onProgressUpdate");
            }

        }

        public void getdata(String SearchValue) 
        {
            // Create request
            SoapObject request = new SoapObject(NAMESPACE2, METHOD_NAME2);



            PropertyInfo pi4 = new PropertyInfo();
            pi4.setName("City");
            pi4.setValue(SearchValue);// get the string that is to be sent to the webservice
            pi4.setType(String.class);
            request.addProperty(pi4);

            // Create envelope
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            // Set output SOAP object
            envelope.setOutputSoapObject(request);
            // Create HTTP call object
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL2);

            try {
                // Invole web service
                androidHttpTransport.call(SOAP_ACTION2, envelope);
                // Get the response
                SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

                //Converting string to Array list
                  ArrayList<String> Servciecityname_arr= new ArrayList<String>();


                if ((response.toString()).contains("{")) 
                {

                    SoapObject rep = (SoapObject) envelope.bodyIn;
                    JSONArray jr = new JSONArray(rep.getPropertyAsString(0));
                    for (int i = 0; i < jr.length(); i++) {
                        JSONObject jb = (JSONObject) jr.get(i);


                           Cityname = jb.getString("CityName123");


                           Servciecityname_arr.add(Cityname);

                    }

                    CITYNAME = new String[Servciecityname_arr.size()];
                    CITYNAME = Servciecityname_arr.toArray(CITYNAME);


                } 
                else
                {
                    Status_Response = response.toString();
                }

            } catch (Exception e) {
                Log.i(TAG2, "Error in catch");
                e.printStackTrace();
            }

        }



}

【讨论】:

    【解决方案3】:
    public void getSignupdata(String SearchValue, String CityLocation) 
        {
            // Create request
            SoapObject request = new SoapObject(NAMESPACE2, METHOD_NAME2);
    
            PropertyInfo pi3 = new PropertyInfo();
            pi3.setName("search");
            pi3.setValue(SearchValue);// get the string that is to be sent to the webservice
            pi3.setType(String.class);
            request.addProperty(pi3);
    
            PropertyInfo pi4 = new PropertyInfo();
            pi4.setName("City");
            pi4.setValue(CityLocation);// get the string that is to be sent to the webservice
            pi4.setType(String.class);
            request.addProperty(pi4);
    
            // Create envelope
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            // Set output SOAP object
            envelope.setOutputSoapObject(request);
            // Create HTTP call object
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL2);
    
            try {
                // Invole web service
                androidHttpTransport.call(SOAP_ACTION2, envelope);
                // Get the response
                SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
    
                if ((response.toString()).contains("{")) 
                {
                    // JSONArray jr = new JSONArray(response);
                    SoapObject rep = (SoapObject) envelope.bodyIn;
                    JSONArray jr = new JSONArray(rep.getPropertyAsString(0));
                    for (int i = 0; i < jr.length(); i++) {
                        JSONObject jb = (JSONObject) jr.get(i);
    
    
    
                           ServiceCenterName = jb.getString("CenterName");
    
    
    
                            String strArray[] = ServiceCenterName.split(" ");
    
                    System.out.println("String converted to String array");
    
                    //print elements of String array
                    for(int i=0; i < strArray.length; i++)
                    {
                            System.out.println(strArray[i]);
                    }
    
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, strArray);
    
                SearchEdittext.setThreshold(1);
                SearchEdittext.setAdapter(adapter)
    
    
                    }
                } 
                else
                {
                    Status_Response_FindStores_Landing = response.toString();
                }
    
            } catch (Exception e) {
                Log.i(TAG2, "Error in catch");
                e.printStackTrace();
            }
    
        }
    

    【讨论】:

    • 这是我的代码,我没有在自动完成文本视图中获取数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 2023-03-16
    • 1970-01-01
    • 2019-05-01
    相关资源
    最近更新 更多