Pergunta

I want to click on my listview (Rainbow Dash) and the image drawable/rainbowdash to be in the imageview instead of drawable/default. I want to use the onclick method. If there is any suggestion to improve what i already have It'd be awesome. Thanks

This is my main activity.

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

        }

    });

}

}

This is my 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>

This is my layout

<?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>

And what the log says is: java.lang.RuntimeException: Unable to start activity ComponentInfo{test.imagenes/test.imagenes.SecondActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.ListView

Foi útil?

Solução

Issue should be with the Drawables your using. Try to change the images. For testing purpose check with some low resolution images .

Also, check this http://developer.android.com/training/displaying-bitmaps/index.html

Also , try to recycle the ImageView before setting a new drawable when item selected in ListView

  ImageView image=(ImageView)findViewById(R.id.imageView1);
  ((BitmapDrawable) image.getDrawable()).getBitmap().recycle();
  image.setImageResource(imgs.getResourceId(position,-1));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top