gpt4 book ai didi

自定义 ListView 适配器上的 Android OnItemClickListener

转载 作者:行者123 更新时间:2023-11-29 14:44:43 26 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,我试图让该项目在带有自定义适配器的 listView 上单击以工作,但我无法做到。我在客户适配器中实现了 OnItemClickListener。

你知道我可能做错了什么吗? ListView 正确加载内容,唯一的问题是它没有点击。

这是我的 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);


}

这是我的自定义适配器:

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();

}

}

最佳答案

booking_listview_layout.xml 中的一些内容

  1. android:descendantFocusability="blocksDescendants" 添加到根布局。

  2. 此外,如果您在布局中添加 android:clickable="true",请将其移除并添加 android:focusable="false"。 ---如果第一种情况无效。

第二种方法

将 clickListener 添加到 getView() 方法中的 View 对象。它将调用整行。

代码 fragment :

 @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;
}

关于自定义 ListView 适配器上的 Android OnItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43620092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com