OnItemClickListener on a ListView inside a Fragment not working

后端 未结 7 1500
粉色の甜心
粉色の甜心 2020-12-29 09:11

EDIT: SOLVED. If there\'s anything focusable in the XML of the items, it will break the touch of the list, in other words, android:focusable=false to all the checkbo

7条回答
  •  悲哀的现实
    2020-12-29 09:33

    If you want to pass data from fragment to any activity on Listview click then you can modify your code like...

    class HistoryFragment extends Fragment {  ListView historyListView;
    public HistoryFragment() {
        // Required empty public constructor
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
        View v= inflater.inflate(R.layout.fragment_history, container, false);
        historyListView= (ListView) v.findViewById(R.id.historyDataListView);
    
        sendRequest();  //it is my meathod to load the listview and set the adapter
        return  v;
    }
    
    public void onStart() {
        super.onStart();
        historyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
    
                Intent intent=new Intent(getActivity(), DisplayDetails.class);
    
                intent.putExtra("text", historyListView.getItemAtPosition((int) l).toString());
                startActivity(intent);
              //  Toast.makeText(getActivity(),"Hello.. "+historyListView.getItemAtPosition((int) l).toString(),Toast.LENGTH_LONG).show();
            }
        });
    }}
    

提交回复
热议问题