Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Lukas Baumgartner
Lukas Baumgartner
14,817 Points

[SOLVED] - OnLongClickListener not working

Hy! I'm working on an App, when you click an item of a recyclerview long enough it should display an option to delete the selected item.

But it's not working and i don't know why...

here is the code of the project

Adapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter {
    private final MainFragment.OnListItemSelectedInterface mListener;
    private final MainFragment.OnListItemLongSelectedInterface mLongListener;

    public RecyclerViewAdapter(MainFragment.OnListItemSelectedInterface listener, MainFragment.OnListItemLongSelectedInterface longListener) {
        mListener = listener;
        mLongListener = longListener;
    }


    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.llist_item_vertical, parent, false);
        return new ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ListViewHolder)holder).bindView(position);
    }

    @Override
    public int getItemCount() {
        //change to how many items are in the list or should be displayed in the list
        return Exercises.mainList.length;
    }

    private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
        private TextView mTextView1;
        private ImageView mImageView;
        private int mIndex;

        public ListViewHolder(View itemView) {
            super(itemView);
            mTextView1 = (TextView) itemView.findViewById(R.id.textView);

            itemView.setOnClickListener(this);
        }

        public void bindView(int position){
            mTextView1.setText(Exercises.mainList[position]);
            mIndex = position;
        }

        @Override
        public void onClick(View v) {
            mListener.onItemSelected(mIndex);
        }

        @Override
        public boolean onLongClick(View v) {
            mLongListener.onItemLongSelected(mIndex);
            return true;
        }
    }
}

Fragment:

public interface OnListItemLongSelectedInterface{
        void onItemLongSelected(int index);
    }

    public interface OnListItemSelectedInterface {
        void onItemSelected(int index);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        OnListItemSelectedInterface listener = (OnListItemSelectedInterface)getActivity();
        OnListItemLongSelectedInterface longlistener = (OnListItemLongSelectedInterface)getActivity();


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

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.listRecyclerView);
        RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(listener, longlistener);
        recyclerView.addItemDecoration(new Divider(getActivity()));
        recyclerView.setAdapter(recyclerViewAdapter);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

And here's the main activity:

 @Override
    public void onItemLongSelected(int index) {
        Toast.makeText(this, "Delete selected item with prompt message " + index, Toast.LENGTH_SHORT).show();
    }

1 Answer

Lukas Baumgartner
Lukas Baumgartner
14,817 Points

I've found it :)

I forgott to set the onLonClickListener to the item.

private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        private TextView mTextView1;
        private int mIndex;

        public ListViewHolder(View itemView) {
            super(itemView);
            mTextView1 = (TextView) itemView.findViewById(R.id.textView);

            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        public void bindView(int position){
            mTextView1.setText(Exercises.mainList[position]);
            mIndex = position;
        }

        @Override
        public void onClick(View v) {
            mListener.onItemSelected(mIndex);
        }

        @Override
        public boolean onLongClick(View v) {
            mLongListener.onItemLongSelected(mIndex);
            return true;
        }
    }

Now it works :)

Jason Anders
Jason Anders
Treehouse Moderator 145,623 Points

Very happy to hear you solved it! :)

I've edited your post to mark it as solved.

:dizzy: