[英]Android OnItemClickListener on custom ListView adapter
I am working on an Android application and I am trying to make the item click on a listView with custom adapter to work but am not being able to. 我正在开发一个Android应用程序,并且试图使该项目在具有自定义适配器的listView上单击以正常工作,但无法运行。 I have my OnItemClickListener implemented inside the customer adapter.
我在客户适配器内部实现了OnItemClickListener。
Would you know what I can be doing wrong? 你知道我会做错什么吗? ListView loads with content correctly, only point is that it does not clicks.
ListView正确加载了内容,唯一的一点是它没有单击。
This is my listView definition and adapter setting: 这是我的listView定义和适配器设置:
public void updateUserAssetBookingsListView(final ArrayList<AssetBooking> userAssetBookings) {
System.out.println("Bookings updated, new total is: " + userAssetBookings.size());
BookingAdapter bookingAdapter = new BookingAdapter(getContext(), 0, userAssetBookings);
userAssetBookingsListView.setAdapter(bookingAdapter);
userAssetBookingsListView.setOnItemClickListener(bookingAdapter);
}
This is my custom adapter: 这是我的自定义适配器:
package it.bitrack.adapter;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.Toast;
import org.w3c.dom.Text;
import it.bitrack.fabio.bitrack.R;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import it.bitrack.support.Epoch;
import it.bitrack.support.AssetBooking;
/**
* Created by fabio on 25/04/2017.
*/
public class BookingAdapter extends ArrayAdapter<AssetBooking> implements AdapterView.OnItemClickListener {
ArrayList<AssetBooking> userAssetBookings;
Epoch epoch = new Epoch();
public BookingAdapter(@NonNull Context context, @LayoutRes int resource, ArrayList<AssetBooking> userAssetBookings) {
super(context, resource, userAssetBookings);
this.userAssetBookings = userAssetBookings;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Get the data item for this position
AssetBooking ab = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.booking_listview_layout, parent, false);
}
// Lookup view for data population
TextView assetTextView = (TextView) convertView.findViewById(R.id.assetTextView);
TextView fromTextView = (TextView) convertView.findViewById(R.id.fromTextView);
TextView toTextView = (TextView) convertView.findViewById(R.id.toTextView);
TextView durationTextView = (TextView) convertView.findViewById(R.id.durationTextView);
ImageButton cancelImageButton = (ImageButton) convertView.findViewById(R.id.cancelImageButton);
cancelImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "You tried to delete this row " + position, Toast.LENGTH_SHORT).show();
}
});
// Populate the data into the template view using the data object
// AssetBooking ab = userAssetBookings.get(position);
long from = ab.getFromDatetime() / 1000;
long to = ab.getToDatetime() / 1000;
long delta = (to - from);
long deltaDays = delta / 86400;
long deltaMinutes = ((delta % 86400) / 60) % 60;
long deltaHours = (delta % 86400) / 3600;
assetTextView.setText(ab.getNetworkAssetCode() + ": " + ab.getAssetDescription());
fromTextView.setText(epoch.getDatetimeFromTimestampNoSeconds(from));
toTextView.setText(epoch.getDatetimeFromTimestampNoSeconds(to));
durationTextView.setText(deltaDays + " day(s) " + deltaHours + " hour(s) " + deltaMinutes + " min(s)");
// Return the completed view to render on screen
return convertView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
// set title
alertDialogBuilder.setTitle("Bookings details");
// set dialog message
alertDialogBuilder
.setMessage("Booker name: " + userAssetBookings.get(position).getUserName() + " " + userAssetBookings.get(position).getUserLastName() +
"\nBooker e-mail address: " + userAssetBookings.get(position).getUserEmail() +
"\nBooking date: " + epoch.getDatetimeFromTimestampNoSeconds(userAssetBookings.get(position).getCreatedOn() / 1000))
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
Couple of things in your booking_listview_layout.xml
您的
booking_listview_layout.xml
中的booking_listview_layout.xml
Add android:descendantFocusability="blocksDescendants"
to the root layout. 将
android:descendantFocusability="blocksDescendants"
到根布局。
Also if you add android:clickable="true"
in your layout remove it and also add android:focusable="false"
. 另外,如果您在布局中添加
android:clickable="true"
,请将其删除,并同时添加android:focusable="false"
。 ---if the first case not worked. ---如果第一种情况不起作用。
Second approach 第二种方法
Add a clickListener to the view object inside getView()
method..it will call upon entire row. 将clickListener添加到
getView()
方法内的视图对象中。它将调用整行。
Code snippet: 程式码片段:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.booking_listview_layout, parent, false);
}
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(context).setTitle("touched").show();
}
});
return convertView;
}
Inside the 'getView()' method try putting an OnClickListener on the convertview in the following way: 在“ getView()”方法内部,尝试通过以下方式将OnClickListener放在convertview上:
convertview.setOnClickListener(new OnClickListener(){
...............YOUR CODE HERE.............
})
see if this works and dont implement onitemclicklistener 看看这是否有效,不要实现onitemclicklistener
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.