Listview setOnItemClickListener不工作?

时间:2015-10-15 16:26:14

标签: android listadapter onitemclicklistener

每当我点击listview时,都会弹出任何内容。

这是on onCreate:

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


    String[] Data = {"Sunday 20/44", "dsd  65 /00", "defef","dddf","sdsdsd","dsdsdsd","3e3we"};


    List lstData = new ArrayList<>(Arrays.asList(Data));
    forecastAdp = new ArrayAdapter<String>(
            getActivity(),
            R.layout.list_item_forecast,
            R.id.list_item_forecast_textview,
            lstData

    );

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

    final ListView listView = (ListView) rootView.findViewById(R.id.listViewForecast);
    listView.setAdapter(forecastAdp);



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

2 个答案:

答案 0 :(得分:1)

替换此行

String main = listView.getSelectedItem().toString();

String main = forecastAdp.get(position).(your adapter textView name where you are storing value).

答案 1 :(得分:0)

为什么要使它变得复杂,尝试直接在Toast体中传递位置:

Toast.makeText(getActivity(),Data[position], Toast.LENGTH_SHORT).show();