使用onItemClickListner的列表视图无效

时间:2015-11-03 10:14:59

标签: android android-listview

OnItemClickListner 无法帮助我,我希望点击列表项每个项目转到下一个活动

java

 public class Tab1 extends Fragment {

    private ListAdapter mAdapter;

    private ArrayList<String> listCountry;
    private ArrayList<Integer> listFlag;
    private ListView listview;


     @Override

         public View onCreateView(LayoutInflater inflater, ViewGroup container,

                 Bundle savedInstanceState) {

             View rootView = inflater.inflate(R.layout.tab_layout_one, container, false);

             prepareList();        
            mAdapter = new ListAdapter(getActivity(), listCountry, listFlag);
            //Missed code to here
            listview = (ListView)rootView.findViewById(R.id.card_listView);
            listview.setAdapter(mAdapter);


            listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub



                }

            });

             return rootView;

         }

     public void prepareList()
        {
            listCountry = new ArrayList<String>();

            listCountry.add("India");
            listCountry.add("USA");
            listCountry.add("Canada");
            listCountry.add("AUS");



            listFlag = new ArrayList<Integer>();
            listFlag.add(R.drawable.india);
            listFlag.add(R.drawable.usa);
            listFlag.add(R.drawable.canada);
            listFlag.add(R.drawable.aus);

        }

}

2 个答案:

答案 0 :(得分:2)

小错误,请使用onItemClick方法

添加您的代码

在这里显示Toast消息。和新活动发布的演示代码

 listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

              // Add your staff here 
             Toast.makeText(getActivity(),"Listview Click"+position, Toast.LENGTH_SHORT).show();

             Intent intent = new Intent(getActivity(), Your_Desise.class);
             startActivity(intent);

            }

        });

试试这种方式。我相信它会对你有帮助。

  

Listview , open new activity onClick

     

Start Activities from onItemClick of ListView in fragments

答案 1 :(得分:0)

您必须调用AdapterView onItemClickListener

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getActivity(),"Click" +position, Toast.LENGTH_SHORT).show();
        }
    });