如何使用自定义列表视图将特定文本视图文本发送到另一个活动?

时间:2016-05-06 06:05:12

标签: android listview textview

As shown in image I want to get the text view text

我唯一需要的是将TextView文本发送到所选Activity项的另一个ListView。我使用自定义ListView Adapter来使用JSON解析从Web服务获取数据。使用我当前的代码,无论我点击哪个项目,我每次都会从第一个ListView项目中获取文本。

        @Override
        protected void onPostExecute(Void args) {

            ArrayList<String> nameList = new ArrayList<>();
            for (Details bean : details) {
                nameList.add(bean.getResType());
                aero(details);


            }
        }
    }

    private void aero(final List<Details> mList) {

        for (Details bean : details) {

            final String urlChar = bean.getUrl();// may be the problem is here
            idno = String.valueOf(bean.getId()); // may be the problem is here

            if (bean.getResType().equals("Notes")) {
                final ListView listView;
                View view = getView();
                if (view != null) {
                    listView = (ListView) view.findViewById(R.id.list2);


                    CustomListViewAdapterNotes adapter = new CustomListViewAdapterNotes(getContext(),
                            R.layout.notes, mList);
                    listView.setAdapter(adapter);
                    // Assign adapter to ListView
                    listView.setAdapter(adapter);
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {


                            Intent intent = new Intent(getContext(), Main_Activity_for_Notes.class);
                            //add data to the Intent object
                            intent.putExtra("idnotes", idno);


                            intent.putExtra("urlchar", urlChar);


                            Toast.makeText(getActivity(),
                                    "Click ListItem Number " + idno, Toast.LENGTH_LONG)
                                    .show();

                            startActivity(intent);


                        }


                    });
                }
            }
        }


}

2 个答案:

答案 0 :(得分:1)

  

。随着我的代码,我每次都会得到第一个项目文本,无论如何   我点击的地方。

首先不需要初始化ListView,为ListView设置Adapter,并将setOnItemClickListener添加到for-loop内的ListView。所以只需从代码中删除for循环。

如果在position方法内部使用onItemClick方法,则设置点击行的点击项使用onItemClick参数:

Details bean= mList.get(position);
String urlChar = bean.getUrl(); 
String idno = String.valueOf(bean.getId());

使用Intent将urlCharidno传递给其他活动。

答案 1 :(得分:1)

更改这些行

                    Intent intent = new Intent(getContext(), Main_Activity_for_Notes.class);
                    //add data to the Intent object
                    intent.putExtra("idnotes", details.get(position).getId());


                    intent.putExtra("urlchar", details.get(position).getUrl());


                    Toast.makeText(getActivity(),
                            "Click ListItem Number " + details.get(position).getId(), Toast.LENGTH_LONG)
                            .show();

                    startActivity(intent);