单击listview更改imageview中的图像

时间:2014-05-06 23:26:41

标签: android listview imageview

我想点击我的listview(Rainbow Dash),图片drawable / rainbowdash在imageview中而不是drawable / default。 我想使用onclick方法。如果有任何改善我已经拥有的建议那就太棒了。感谢

这是我的主要活动。

package test.imagenes;
import java.util.ArrayList;

import android.net.ParseException;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.app.Activity;
import android.content.res.TypedArray;

public class SecondActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maindos);

    String[] ponies = getResources().getStringArray(R.array.ponies);
    final ListView listview = (ListView) findViewById(R.id.lista);
    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < ponies.length; ++i) {
          list.add(ponies[i]);
        }


    ArrayAdapter<String> arrayAdapter =      
            new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list);
    listview.setAdapter(arrayAdapter);
    listview.setOnItemClickListener(new OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,
                long arg3) {
            String selectedPony=list.get(position);
            Toast.makeText(getApplicationContext(), "Pony Selected : "+selectedPony,   Toast.LENGTH_LONG).show();
            TypedArray imgs= getResources().obtainTypedArray(R.array.poniesimg);
            ImageView image=(ImageView)findViewById(R.id.imageView1);
            image.setImageResource(imgs.getResourceId(position,-1));
            imgs.recycle();

        }

    });

}

}

这是我的list_data.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="ponies">
            <item>Rainbow Dash</item>
            <item>Apple Jack</item>
            <item>Rarity</item>
            <item>Pinkie Pie</item>
            <item>Twilight Sparkle</item>
            <item>Fluttershy</item>
        </string-array>
        <string-array name="poniesimg">
            <item>@drawable/rainbowdash</item>
            <item>@drawable/applejack</item>
            <item>@drawable/rarity</item>
            <item>@drawable/pinkiepie</item>
            <item>@drawable/twilightsparkle</item>
            <item>@drawable/fluttershy</item>
        </string-array>
    </resources>

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >

<ListView
    android:id="@+id/lista"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/imageView1" >

</ListView>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="200dip"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>

日志说的是: java.lang.RuntimeException:无法启动活动ComponentInfo {test.imagenes / test.imagenes.SecondActivity}:java.lang.ClassCastException:android.widget.ImageView无法强制转换为android.widget.ListView

1 个答案:

答案 0 :(得分:0)

问题应该与您使用的Drawables一致。尝试更改images。出于测试目的,请检查一些low resolution images

另外,请检查此http://developer.android.com/training/displaying-bitmaps/index.html

此外,在ImageView

中选择的项目设置新的drawable之前,请尝试回收ListView
  ImageView image=(ImageView)findViewById(R.id.imageView1);
  ((BitmapDrawable) image.getDrawable()).getBitmap().recycle();
  image.setImageResource(imgs.getResourceId(position,-1));