自定义ListView适配器上的Android OnItemClickListener

时间:2017-04-25 20:10:48

标签: android onitemclicklistener custom-adapter

我正在开发一个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();

    }

}

2 个答案:

答案 0 :(得分:4)

booking_listview_layout.xml

中的一些事情
  1. android:descendantFocusability="blocksDescendants"添加到根布局。

  2. 此外,如果您在布局中添加android:clickable="true",请将其删除,同时添加android:focusable="false"。 ---如果第一种情况不起作用。

  3. 第二种方法

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

    代码段:

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

答案 1 :(得分:1)

在'getView()'方法中尝试以下列方式在convertview上放置一个OnClickListener:

convertview.setOnClickListener(new OnClickListener(){
...............YOUR CODE HERE.............
})

看看是否有效并且不实施onitemclicklistener