【问题标题】:Cant get ImageView onItemClick from custom ListView Android [EDIT]无法从自定义 ListView Android [编辑] 中获取 ImageView onItemClick
【发布时间】:2013-07-24 07:59:51
【问题描述】:

我已经实现了一个自定义的 ListView,这是每一行的结构:

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

<ImageView
android:id="@+id/imagenEvento"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"
android:adjustViewBounds="false"
android:paddingBottom="2dp"     
android:contentDescription="@string/contenidoImagen"       
/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:padding="10dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/titulo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/sitio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/fecha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

我已设置为我的 ListView 和适配器,它在访问数据时工作正常,但是当我尝试将 ImageView 转换为 byteArray 以便将其发送到另一个 Activity 时,我无法获取 ImageView

lv = (ListView) findViewById(R.id.lista);       
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            //creo el nuevo objeto para poder llamarlo
            Intent intent = new Intent(AllProductsActivity.this, PostActivity.class);

            //Creo la informacion para pasar entre actividades
            Bundle informacion= new Bundle();
            informacion.putInt("id", eventos.get(position).getID());

            Bitmap bmp = BitmapFactory.decodeResource(a.getResources(), R.id.imagenEvento);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            informacion.putByteArray("imagen", byteArray);                              
        }

    });

我的 bmp 为 NULL:

Bitmap bmp = BitmapFactory.decodeResource(a.getResources(), R.id.imagenEvento);

我想我应该指出项目的位置,因为它在 ListView 中,但我不知道该怎么做

[编辑] 问题解决了,我在下一个 Activity 中获取图片,但是,我总是从这个 Activity 的 ListView 中获取顶部图片,如何获取点击的图片?

【问题讨论】:

    标签: android android-listview null android-imageview


    【解决方案1】:

    代替:

    Bitmap bmp = BitmapFactory.decodeResource(a.getResources(), R.id.imagenEvento);
    

    获取您的位图图像。试试这个:

    ImageView image = (ImageView)v.findViewByID(R.id.iamgeenEvento);
    Bitmap bmp = ((BitmapDrawable)image.getDrawable()).getBitmap();
    

    您的原始代码试图将图像视图本身解码为位图,因为它不是位图,所以会返回空值。

    【讨论】:

    • 07-24 10:11:22.851:E/AndroidRuntime(651):致命异常:主要 07-24 10:11:22.851:E/AndroidRuntime(651):java.lang.ClassCastException: eventos.MyClassAdapter$DownloadedDrawable
    • 如果不是BitmapDrawable,你放什么样的drawable,你期望如何从中得到Bitmap?
    【解决方案2】:

    您使用的ImageView 只是一个引用,因此无需将其转换为数组,只需将引用直接传递为static 或创建一个包含此类引用的static class,以便您以后可以使用它,但是您也可以将imageview 的图像保存为位图并编码将该图像保存为字节数组并在需要时对其进行解码,然后将其 imageView 用作imageView.setImageBitmap(bitmap);

    imageView.buildDrawingCache();
    Bitmap bmap = imageView.getDrawingCache();
    
    
    public String BitmapToString(Bitmap bitmap){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos);
        byte[] b = baos.toByteArray();
        String image = Base64.encodeToString(b, Base64.DEFAULT);
        return image;
    }
    
    
    public Bitmap StringToBitmap(String image){
        byte[] encodeByte =  Base64.decode(image, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0 , encodeByte.length);
        return bitmap;
    }
    

    【讨论】:

    • 将 bmp 编码为字符串并对其进行解码,我得到了我的图像的 URL
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多