简体   繁体   中英

Why is my onClick method not being reached in this fragment

I am trying to create a list view within a fragment in Android. I have successfully populated the list view with the array of clubs but my method OnClick is never reached when I click on my list. I am hoping to see a toast pop up with row number.

Please see code below:

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

/**
 * club selection java Class
 */



public class ClubsFragment extends Fragment implements View.OnClickListener {

    String[] mClubArray;
    ListView MyList;


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


        //returning our layout file
        //change R.layout.yourlayoutfilename for each of your fragments

        View view = inflater.inflate(R.layout.fragment_clubs, container, false);

        mClubArray = getResources().getStringArray(R.array.club_array);

        ArrayAdapter<String> itemsAdapter =
                new ArrayAdapter<String>(getContext(), R.layout.list_item_style, mClubArray);

        MyList = (ListView) view.findViewById(R.id.ClubList);
        MyList.setAdapter(itemsAdapter);

        return view;

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //you can set the title for your toolbar here for different fragments different titles
        getActivity().setTitle("Clubs");

    }



    @Override
    public void onClick(View view) {

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

                Toast.makeText(getContext(),
                        "Click ListItem Number " + position, Toast.LENGTH_LONG)
                        .show();


            }
        });


    }





}

in your onCreateView():

use OnItemClickListener

After u set the adapter

  MyList.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
            String value = (String)adapter.getItemAtPosition(position); 
             Toast.makeText(this,value,Toast.LENGTH_SHORT).show();
             list.remove(position); 
             adapter.notifyDataSetChanged();
            // do what you intend to do on click of listview row
      }
   });

to remove item:

ArrayList<String> list =new ArrayList<String>(Arrays.asList(mClubArray)); //declare globally so that u can access it 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, list);

 MyList.setAdapter(adapter);
    MyList.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getActivity(), "You selected : " + item, Toast.LENGTH_SHORT).show();
            String item = list.get(position);
            list.remove(position);
            adapter.notifyDataSetChanged();

        }
    });

Implement Onclick method after setting onClick listenser: Like this

ArrayAdapter<Object> ad = new ArrayAdapter<Object>(this,
                android.R.layout.simple_list_item_checked, items);
        setListAdapter(ad);
        ListView list = getListView();
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        //list.setItemChecked(0, true);
        list.setOnItemClickListener(this);

Try this :

MyList.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
            String value = (String)adapter.getItemAtPosition(position); 
             Toast.makeText(this,value,Toast.LENGTH_SHORT).show();

      }
   });

Problem

OnClick is never reached when I click on my list.

OnClickListener and OnItemClickListener are clickevent callback interface of View and AdapterView respectively. callback method is invoked when a specific event occurs. Therefore you don't implements setOnItemClickListener in OnClick(View.OnClickListener interface) .

Solution

remove the source related with View.OnClickListener

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

    //...

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

            Toast.makeText(getContext(),
                    "Click ListItem Number " + position, Toast.LENGTH_LONG)
                    .show();


        }
    });

    //...
}

Reference

Callback functions in Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM