How to add Different OnClickListners on views of ListView item to perform different actions
Clash Royale CLAN TAG#URR8PPP
How to add Different OnClickListners on views of ListView item to perform different actions
Are there any possibilities to check what view inside ListView
Item was clicked?
In other words, when you click on different Views inside ListView
item app should perform a different action.
In details, I have a simple Book.java
class that contains some book description.
Then I create ListView<Book>
using BooksAdapter.class
:
ListView
ListView
Book.java
ListView<Book>
BooksAdapter.class
public class BooksAdapter extends ArrayAdapter<Book>
public BooksAdapter (Context context, List<Book> books)
super(context,0,books);
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
// ConstraintLayout constraintLayout = new ConstraintLayout();
// constraintLayout.setVisibility();
View listItemView = convertView;
if (listItemView == null)
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.books_list_item, parent, false);
Book currentBook = getItem(position);
ImageView coverView = (ImageView) listItemView.findViewById(R.id.preview_image_view);
if (currentBook.getImage() == "No cover")
coverView.setImageResource(R.drawable.no_book_cover);
else
Picasso.get().load(currentBook.getImage()).into(coverView);
TextView authorTextView = (TextView)listItemView.findViewById(R.id.autor_text);
authorTextView.setText(formatAuthor(currentBook.getAuthor(),currentBook.getDate()));
TextView titleTextView = (TextView)listItemView.findViewById(R.id.title_text);
titleTextView.setText(currentBook.getTitle());
//TextView descrTextView = (TextView)listItemView.findViewById(R.id.description_text);
//descrTextView.setText(currentBook.getDescription());
return listItemView;
private String formatAuthor (String name,String date )
name = name.substring(2,name.length()-2);
date = date.substring(0,4);
String fullString = name + ", " + date;
return(fullString);
books_list_item.xml
:
books_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingStart="16dp"
android:paddingBottom="16dp"
android:paddingTop="8dp">
<ImageView
android:id="@+id/preview_image_view"
android:layout_width="80dp"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/autor_text"
android:layout_width="match_parent"
android:layout_height="40dp"
android:maxLines="1"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:textColor="@color/textColorPrimary"
android:textSize="16sp" />
<TextView
android:id="@+id/title_text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center_vertical"
android:maxLines="2"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="0dp"
android:textColor="@color/textColorLight"
android:textSize="16sp" />
</LinearLayout>
<ImageView
android:layout_width="40dp"
android:layout_height="match_parent"
android:src="@drawable/ic_info_black_24dp"
android:scaleType="center"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@android:color/darker_gray"/>
</LinearLayout>
In MainActivity.java
create mAdapter and override setOnItemClickListener
:
MainActivity.java
setOnItemClickListener
ListView booksListView = (ListView) findViewById(R.id.list);
mAdapter = new BooksAdapter(this, new ArrayList<Book>());
booksListView.setAdapter(mAdapter);
booksListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l)
Book book = mAdapter.getItem(position);
ad = new AlertDialog.Builder(BooksListActivity.this);
ad.setTitle("Book description");
ad.setMessage(book.getDescription());
AlertDialog alert = ad.create();
alert.show();
//ad.create();
);
And here are some general question can we set onClickListeners
on different Views
inside Item (@+id/autor_text
and @+id/title_text
for example) and perform different actions in these cases?
I'm reading about this several places but doesn't find any helpful things.Thanks for any help.
onClickListeners
Views
@+id/autor_text
@+id/title_text
2 Answers
2
Inside your getView()
method set onClickListener
to all your different Views
and perform respective action.
getView()
onClickListener
Views
By implementing this code:-
authorTextView.setOnClickListener(this);
titleTextView.setOnClickListener(this);
public void onClick(View v)
switch(v.getId())
case R.id.authorTextView:
//code to be written to handle the click event
break;
case R.id.titleTextView:
//code to be written to handle the click event
break;
};
Inside your getView
getView
authorTextView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
//Do your action
);
titleTextView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
//Do your action
);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.