Fabio Lanza
Fabio Lanza

Reputation: 175

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. I have my OnItemClickListener implemented inside the customer adapter.

Would you know what I can be doing wrong? ListView loads with content correctly, only point is that it does not clicks.

This is my listView definition and adapter setting:

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

    }

}

Upvotes: 1

Views: 7287

Answers (2)

rafsanahmad007
rafsanahmad007

Reputation: 23881

Couple of things in your booking_listview_layout.xml

  1. Add android:descendantFocusability="blocksDescendants" to the root layout.

  2. Also if you add android:clickable="true" in your layout remove it and also add 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.

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

Upvotes: 6

Mohaimanul Chowdhury
Mohaimanul Chowdhury

Reputation: 121

Inside the 'getView()' method try putting an OnClickListener on the convertview in the following way:

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

see if this works and dont implement onitemclicklistener

Upvotes: 1

Related Questions