Search code examples
androidlistviewalphabetic

Alphabetic list with letter - clickable


I am creating an alphabetic ordered list, where there is also a letter showing up above the items - see the image: screenshot

This is already done and working fine, but I need to have each item clickable, except the letters.

This is a part of my code from MainActivity.java (fragment):

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

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

        List<LetterSectionListItem> books = createBooks();

        ListView bookListView = (ListView) rootView.findViewById(R.id.booklistview);

                LetterSectionListAdapter bookListAdapter = new LetterSectionListAdapter(getContext(), R.layout.booklist_row, books) {
            @NonNull
            @Override
            public View getView(int position, View v, ViewGroup parent) {
                //Must call this before to wrap the header around the view
                v = super.getView(position, v, parent);

                TextView bookTitle = (TextView) v.findViewById(R.id.book_title);
                TextView authorName = (TextView) v.findViewById(R.id.author_name);

                Book book = (Book) this.getItem(position);

                assert book != null;
                bookTitle.setText(book.getBookName());
                authorName.setText(book.getAuthorLastName() + ", " + book.getAuthorFirstName());

                return v;
            }
        };

        bookListView.setAdapter(bookListAdapter);

        return rootView;
    }

Now what I tried:

I put there this code, just above return rootView; :

v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent=new Intent(v.getContext(),SingleitemView.class);
                        intent.putExtra("place2", "test");
                        getContext().startActivity(intent);

                    }
                });

However, this is working fine, so opens another activity after clicking the item, but also when I click the standalone letters, and I don't want to have clickable letters A, B, C.. etc., just the items below.

Can someone please advice here, what am I doing wrong?


Solution

  • I wonder that nobody could help with such easy thing! I solved it for myself, in my adapter I just used this:

       TextView headerLetterTextView = (TextView) headerLayout.findViewById(R.id.item_row_letter);
    headerLetterTextView.setOnClickListener(null);
    

    Works like a charm!