如何使用来自和webservice的查询设置simple_list_item_1和Simple_list_item_2

时间:2014-02-19 16:26:54

标签: android web-services listview android-asynctask

我正在尝试使用来自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());
            }

        }


}

1 个答案:

答案 0 :(得分:0)

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));

这应该适用于现在。与此同时,我正在研究这个问题。实际上,为TextView创建自定义布局时需要ListView,而不是在简单的列表视图中。