片段列表数据未正确传递给活动

时间:2013-06-11 13:17:20

标签: android

当我点击我的列表项时,只有第一项的ID将转到下一个活动。由于我在此列表片段中获得患者姓名和身份证并将患者ID传递给另一个活动,因此即使我点击另一个项目,也只有第一个列表项的ID正在通过...希望您明白我的观点......

这是我的ListFragment

public class AllPatient extends ListFragment {


    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> productsList = new ArrayList<HashMap<String,String>>();

    // url to get all products list
    private static String url_all_patients = "http://192.168.44.208/get_all_patients.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "products";
    private static final String TAG_PATIENT_ID = "patient_id"; 
    private static final String TAG_PATIENT_NAME = "patient_name";

    JSONArray products = null;

    Context ctx;
    String pid;
    EditText inputSearch = null;
    ListAdapter adapter;
    ListView lv;


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

        View view = inflater.inflate(R.layout.patient_list, container, false);
        //ListView lv;

        lv = (ListView)view.findViewById(android.R.id.list);

        //ListView v = getListView();

        new LoadAllPatients().execute();



        return view;        
    }    





    @Override
    public void onListItemClick(ListView lv, View v, int position, long id) {
        // TODO Auto-generated method stub

        onAttach(getActivity());
        //lv.setAdapter(adapter);


        String id1 = ((TextView) getView().findViewById(R.id.pid)).getText().toString();
        System.out.println("all  patient"+id1);

        Bundle bundle = new Bundle();
        bundle.putString("TAG_PATIENT_ID",id1 );
        System.out.println("bundle"+id1);
        Intent i = new Intent(getActivity(),DocPresc.class);
        i.putExtras(bundle);
        startActivity(i);




        //passData(date);


        Toast.makeText(
                getActivity(), 
                getListView().getItemAtPosition(position).toString(), 
                Toast.LENGTH_LONG).show();
    }






    class LoadAllPatients extends AsyncTask<String, String, String> {


        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_patients, "GET", params);

            // Check your log cat for JSON reponse
        Log.d("All Patients: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    products = json.getJSONArray(TAG_PRODUCTS);

                    // looping through All Products
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        // Storing each json item in variable
                        String id = c.getString(TAG_PATIENT_ID).toUpperCase();
                        String name = c.getString(TAG_PATIENT_NAME).toUpperCase();

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_PATIENT_ID, id);
                        map.put(TAG_PATIENT_NAME, name);

                        // adding HashList to ArrayList
                        productsList.add(map);  

                    }
                } else {

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }


        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            //  pDialog.dismiss();
            // updating UI from Background Thread

            getActivity().runOnUiThread(new Runnable() {
                public void run() {

                    ListAdapter adapter = new SimpleAdapter(
                            getActivity(), productsList,
                            R.layout.list_item1, new String[] { TAG_PATIENT_ID,
                                TAG_PATIENT_NAME},
                                new int[] { R.id.pid, R.id.name });
                    // updating listview
                    //setListAdapter(adapter);
                    setListAdapter(adapter);

                }

             });

        }

    }
}

1 个答案:

答案 0 :(得分:0)

试试这个。

更改此行:

String id1 = ((TextView) getView().findViewById(R.id.pid)).getText().toString();

String id1 = ((TextView) v.findViewById(R.id.pid)).getText().toString();