简体   繁体   中英

listview is not clickable after adding custom adapter android

i want get image from the listview .But problem is that whatever the list view i created that is not clickable. i tried with onItemclick listener also, but its not working because of listview is not clickable . Its seems that listview is not at all clickable ..Below is given adapter for for listview.

// The adapter of the GridView which contains the details of the detected faces.

private class FaceListAdapter extends BaseAdapter {
    //List<FaceRectangle> ffRect;
    // The detected faces.
    List<Face> faces;

    List<UUID> faceIdList;

    List<IdentifyResult> mIdentifyResults;

    // The thumbnails of detected faces.
    List<Bitmap> faceThumbnails;

    // Initialize with detection result.
    FaceListAdapter(Face[] detectionResult) {
        //ffRect=new ArrayList<>();
        faceIdList = new ArrayList<>();
        faces = new ArrayList<>();
        faceThumbnails = new ArrayList<>();
        mIdentifyResults = new ArrayList<>();

        if (detectionResult != null) {
            faces = Arrays.asList(detectionResult);
            for (Face face: faces) {
                try {
  // Crop face thumbnail with five main landmarks drawn from original image.
                    faceThumbnails.add(ImageHelper.generateFaceThumbnail(mBitmap, face.faceRectangle));
                    faceRect=face.faceRectangle;
                    //ffRect.add(faceRect);

                    faceIdList.add(null);
                } catch (IOException e) {
                    // Show the exception when generating face thumbnail fails.
                    //setInfo(e.getMessage());
                }
            }
        }
    }

    public void setIdentificationResult(IdentifyResult[] identifyResults) {
        mIdentifyResults = Arrays.asList(identifyResults);
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    @Override
    public int getCount() {
        return faces.size();
    }

    @Override
    public Object getItem(int position) {
        return faces.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = layoutInflater.inflate(R.layout.item_face_with_description, parent, false);
        }
        convertView.setId(position);

        // Show the face thumbnail.
        ((ImageView)convertView.findViewById(R.id.face_thumbnail)).setImageBitmap(faceThumbnails.get(position));

        if (mIdentifyResults.size() == faces.size()) {
            // Show the face details.
            DecimalFormat formatter = new DecimalFormat("#0.00");
            if (mIdentifyResults.get(position).candidates.size() > 0) {
                String personId = mIdentifyResults.get(position).candidates.get(0).personId.toString();

                String personName = StorageHelper.getPersonName(personId, "person", Face_detection.this);

                String identity = "Person: " + personName + "\n" + "Confidence: " + formatter.format(mIdentifyResults.get(position).candidates.get(0).confidence);

                ((TextView) convertView.findViewById(R.id.text_detected_face)).setText(identity);
                photoAdd.setEnabled(false);
            } else {
                ((TextView) convertView.findViewById(R.id.text_detected_face)).setText(
                        R.string.face_cannot_be_identified);
                photoAdd.setEnabled(true);
               // btmp.add(faceThumbnails.get(position));
                //fRect.add(ffRect.get(position));

            }
        }

        return convertView;
    }
}

> item_face_with_description.xml



       <!-- Copyright (c) Microsoft. All rights reserved. -->

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/face_thumbnail"
            android:layout_width="80dp"
            android:layout_height="80dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:id="@+id/text_detected_face" />

    </LinearLayout>

Xml that contains ListView

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_face_detection"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="myapptest.techm.com.myapptest.Face_detection">


        <include
            android:id="@+id/tool_bar"
            layout="@layout/tacho_toolbar"></include>


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/tool_bar">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">


                <Button
                    android:text="Take Again"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="22dp"
                    android:id="@+id/takePhoto"
                    android:layout_below="@+id/camerapreview"
                    android:layout_centerHorizontal="true"
                    android:layout_width="150dp"
                    android:onClick="clickPhoto (Face_detection)"
                    android:visibility="gone" />

                <TextView
                    android:text="Faces in Image"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView23"
                    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                    android:layout_below="@+id/takePhoto"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="42dp" />

                <ListView
                    android:layout_width="match_parent"
                    android:layout_height="400dp"
                    android:layout_below="@+id/textView23"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="28dp"
                    android:id="@+id/face_detect_list"
                    android:focusable="false" />

                <Button
                    android:text="Add"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="25dp"
                    android:layout_marginStart="25dp"
                    android:layout_marginBottom="103dp"
                    android:id="@+id/photoAdd" />

                <Button
                    android:text="Cancle"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/photoAdd"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginRight="66dp"
                    android:layout_marginEnd="66dp"
                    android:id="@+id/cancle" />

                <SurfaceView
                    android:id="@+id/camerapreview"
                    android:layout_width="350dp"
                    android:layout_height="400dp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp" />

                <Button
                    android:text="Identify"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/cancle"
                    android:layout_alignRight="@+id/takePhoto"
                    android:layout_alignEnd="@+id/takePhoto"
                    android:layout_marginRight="18dp"
                    android:layout_marginEnd="18dp"
                    android:id="@+id/identify"
                    android:visibility="gone" />
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>

The isEnabled method on your adapter is responsible for your ListView ignoring clicks.

@Override
public boolean isEnabled(int position) 
{
    return false;
}

Your adapter overrides isEnabled and returns false for all positions, indicating that every item in the list is disabled. Disabled views do not receive input events.

If its possible to disable items in your list then your custom adapter needs to track these items somehow (eg a list), otherwise you should be returning true for all positions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM