listview недоступен для просмотра после добавления пользовательского адаптера Android

#android #xml #android-layout #listview

#Android #xml #android-layout #listview

Вопрос:

я хочу получить изображение из listview.Но проблема в том, что независимо от того, какой вид списка я создал, он недоступен для просмотра. я также пробовал использовать onItemClick listener, но он не работает, потому что listview недоступен для просмотра. Кажется, что listview вообще не доступен для просмотра..Ниже приведен адаптер для для listview.

// Адаптер GridView, который содержит подробную информацию об обнаруженных гранях.

 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, содержащий 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>
  

Ответ №1:

isEnabled Метод вашего адаптера отвечает за то, что ваш ListView игнорирует щелчки.

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

Ваш адаптер переопределяет isEnabled и возвращает false для всех позиций, указывая, что каждый элемент в списке отключен. Отключенные представления не получают события ввода.

Если возможно отключить элементы в вашем списке, то вашему пользовательскому адаптеру необходимо каким-то образом отслеживать эти элементы (например, список), в противном случае вы должны возвращаться true ко всем позициям.