从listView适配器发送特定文本

时间:2017-04-04 02:16:51

标签: android listview

有人能帮帮我吗?我有listView与适配器包含3个Textviews。我只想将一个选定的文本传递给另一个活动 这是我的代码: 每个listview包含3个Textviews(" noreg"," name"," imo")。我只想通过" noreg"到其他活动onItemClick



    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
        myFragmentView = inflater.inflate(R.layout.fragment_search, container, false);
        ed = (SearchView) getActivity().findViewById(R.id.textSearch);
        sc = (ImageButton) getActivity().findViewById(R.id.scanCamera);
        hom = (ImageButton) getActivity().findViewById(R.id.btnhome);
        lv = (ListView) myFragmentView.findViewById(R.id.resultList);
        if(getArguments() !=null) {
            word = this.getArguments().getString("word");
        }

        new GetResult().execute();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                

                Intent intent = new Intent(getActivity(), MainActivity.class);
                intent.putExtra("word", noreg);
                startActivity(intent);
            }
        });


        return myFragmentView;
    }

    private class GetResult extends AsyncTask<Void, Void, Void>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0){
            RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());
            StringRequest stringRequest = new StringRequest(Request.Method.POST, search_url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONArray jsonArray = new JSONArray(response);
                            final int result = jsonArray.length();
                            resultlist = new ArrayList<>();
                            for (int i = 0; i < result; i++){
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                String noreg = jsonObject.getString("noreg");
                                String nama = jsonObject.getString("nama");
                                String imo = jsonObject.getString("imo");

                                HashMap<String, String> datastat = new HashMap<>();

                                datastat.put("noreg", noreg);
                                datastat.put("nama", nama);
                                datastat.put("imo", imo);

                                resultlist.add(datastat);

                                ListAdapter adapter = new SimpleAdapter(
                                        getActivity(), resultlist,
                                        R.layout.list_item, new String[]{"nama", "noreg",
                                        "imo"}, new int[]{R.id.namakapal, R.id.noreg, R.id.imo});

                                lv.setAdapter(adapter);

                            }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            })
            {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();

                    params.put("noregister", word);
                    return params;
                }
            };
            queue.add(stringRequest);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if (pDialog.isShowing())
                pDialog.dismiss();

        }

    }

}
&#13;
&#13;
&#13;

谢谢....

1 个答案:

答案 0 :(得分:0)

试试这段代码。

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String selected1 = ((TextView) view.findViewById(R.id.name)).getText().toString();

                    Toast toast1 = Toast.makeText(getApplicationContext(), selected1, Toast.LENGTH_SHORT);
                    toast1.show();
                Intent intent = new Intent(getActivity(), MainActivity.class);
                intent.putExtra("word", noreg);
                startActivity(intent);
            }
        });