从listView Fragment打开Activity

时间:2014-02-18 21:34:25

标签: android listview android-fragments

这是我的第一个应用。我有一个由列表视图填充的片段,它从Web服务获取数据。我希望能够单击列表视图项并打开一个新活动。虽然每个项目的活动都是相同的,但我将传递一个不同的变量。我已经看过很多关于此的帖子了。它们都不适合我。请帮助谢谢。

public class TicketFragment extends Fragment {
    ListView tv;
    ProgressDialog mProgressDialog;

    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");
            mProgressDialog.setMessage("Loading locations.....");
        }

    }     

    public void location() {
        String SOAP_ACTION = "http://example.com/getlocations";
        String METHOD_NAME = "getlocations";
        String NAMESPACE = "http://example.com/";
        String URL = "http://localhost/Example/Service.asmx";   

        try { 
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            //Request.addProperty("get locations", "3");

            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(Request);

            HttpTransportSE transport= new HttpTransportSE(URL);

            transport.call(SOAP_ACTION, soapEnvelope);

            SoapObject response = (SoapObject)soapEnvelope.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();
            }

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

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

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

    }
}

1 个答案:

答案 0 :(得分:0)

您需要在列表视图中添加一个点击监听器:

tv.setOnItemClickListener(new OnItemClickListener() {

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