Pergunta

I set image icon in layout using "android:drawableLeft". This is my layout code:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textView1"
    android:layout_marginLeft="8dp"
    android:drawableLeft="@drawable/ic_launcher"
    android:gravity="left"/> 

What I need to do is, change this image into something else using my class. This is the code I used inside my Adapter class which is extend with BaseExpandableListAdapter.

if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
((CheckedTextView) convertView).setCheckMarkDrawable(R.drawable.ic_folder);

But this is not replace the used image. Instead replacing it is added to the same line. What should I do to replace the current image with the new one?

Foi útil?

Solução 2

This is what I did as the solution for the problem. Hope it will help anyone who faced the same problem. Here parent is ViewGroup parameter,

Drawable imageDrawable=parent.getContext().getResources().getDrawable((R.drawable.ic_folder));
 imageDrawable.setBounds(0,0, 40, 40);
((CheckedTextView) convertView).setCompoundDrawables(imageDrawable, null,null, null);

Outras dicas

In your xml you are using

`android:drawableLeft="@drawable/ic_launcher"`

and in adapter you are doing

((CheckedTextView) convertView).setCheckMarkDrawable(R.drawable.ic_folder);

These are two different features of CheckedTextView . CheckMark drawable and drawable are different drawables. To add or change left,right,top,bottom image of CheckedTextView you should use

CheckedTextView.setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)

or

CheckedTextView.setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)

and to change CheckMark drawable CheckedTextView.setCheckMarkDrawable(R.drawable.ic_folder);

Drawable img = getContext().getResources().getDrawable( R.drawable.ic_folder );

((CheckedTextView) convertView).setCompoundDrawables( img, null, null, null );
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top