【问题标题】:How to set simple_list_item_1 and Simple_list_item_2 with a query from and webservice如何使用来自 web 服务的查询设置 simple_list_item_1 和 Simple_list_item_2
【发布时间】:2014-02-19 16:26:53
【问题描述】:

我正在尝试使用来自 Web 服务的查询来填充简单的列表项 1 和 2。我正在尝试在简单列表项 1 中显示位置名称,在简单列表项 2 中显示地址。我无法在简单列表项 2 中显示地址。这是我的代码。谢谢。

 public class TicketFragment extends Fragment {
    ListView tv;
    public TicketFragment(){}
       private String TAG ="Vik";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_ticket, container, false);
        tv =(ListView)rootView.findViewById(R.id.listView1);

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

        return rootView;
    }


    private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {

            location();
            return null;
        }

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

        }

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

        }

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

    }     




     public void location() 
        {
            String SOAP_ACTION = "http://example.com/getlocations";
            String SOAP_ACTION2 = "http://example.com/getaddress";
            String METHOD_NAME = "getlocations";
            String METHOD_NAME2 = "getaddress";
            String NAMESPACE = "http://example.com/";
            String URL = "http://100.100.00.00/example/Service.asmx";   

            try { 
                SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
                SoapObject Request2 = new SoapObject(NAMESPACE, METHOD_NAME2);
                SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                SoapSerializationEnvelope soapEnvelope2 = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                soapEnvelope.dotNet = true;
                soapEnvelope.setOutputSoapObject(Request);
                soapEnvelope.setOutputSoapObject(Request2);

                HttpTransportSE transport= new HttpTransportSE(URL);

                transport.call(SOAP_ACTION, soapEnvelope);
                transport.call(SOAP_ACTION2, soapEnvelope2);

                SoapObject response = (SoapObject)soapEnvelope.getResponse();
                SoapObject response2 = (SoapObject)soapEnvelope2.getResponse(); 

                System.out.println(response);
                int intPropertyCount = response.getPropertyCount();
                String[] locations= new String[intPropertyCount];

                for (int i = 0; i < intPropertyCount; i++)
                {               
                locations[i] = response.getPropertyAsString(i).toString();

                }
                System.out.println(response2);
                int intPropertyCount2 = response2.getPropertyCount();
                String[] address= new String[intPropertyCount2];

                for (int i = 0; i < intPropertyCount; i++)
                {               
                address[i] = response2.getPropertyAsString(i).toString();

                }

                final ArrayAdapter<String> adapter =
                        new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, locations);

                final ArrayAdapter<String> adapter2 =
                        new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_2, address); 

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // This code will always run on the UI thread, therefore is safe to modify UI elements.

                        tv.setAdapter(adapter);
                        tv.setAdapter(adapter2);
                        tv.setOnItemClickListener(new OnItemClickListener() {

                            public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
                                Intent intent = new Intent(getActivity(), CreateTicket.class);
                                // add data to the intent...
                                startActivity(intent);
                    }

                });

            }
                });
            }


            catch(Exception ex) {
                Log.e(TAG, "Error: " + ex.getMessage());
            }

        }


}

【问题讨论】:

    标签: android web-services listview android-asynctask


    【解决方案1】:
    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapObject Request2 = new SoapObject(NAMESPACE, METHOD_NAME2);
    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    SoapSerializationEnvelope soapEnvelope2 = new SoapSerializationEnvelope(
                                SoapEnvelope.VER11);
    soapEnvelope.dotNet = true;
    soapEnvelope.setOutputSoapObject(Request);
    soapEnvelope.setOutputSoapObject(Request2); // should be soapEnvelope2.setOutputSoapObject(Request2);
    

    编辑:

    对于布局,请尝试创建自定义布局,例如custom_layout.xml。这个布局就是 simple_list_item 布局的样子。

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center_vertical"
        android:paddingLeft="6dip"
        android:minHeight="?android:attr/listPreferredItemHeight"
    />
    

    然后,在 setAdapted() 中,传递 custom_layout 以及 TextView id

    lv.setAdapter(new ArrayAdapter<String>(this,R.layout.custom_layout,R.id.text1,location));
    

    现在应该可以了。与此同时,我正在调查这个问题。实际上,当您为 ListView 创建自定义布局时需要 TextView,但不是在简单的列表视图中。

    【讨论】:

    • 谢谢,但我仍然遇到错误。您必须为文本视图提供资源 ID。这是我遇到的错误。
    • 你的意思是ListView吗?
    • 你的权利我没有,但这是我得到的错误。我正在尝试填充列表视图而不是文本视图,我不明白为什么会出现该错误。
    • 这是你的全部代码还是你遗漏了一些?
    • 也粘贴布局xml。
    猜你喜欢
    • 1970-01-01
    • 2012-06-30
    • 2011-08-28
    • 1970-01-01
    • 2012-03-29
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多