0

I have a ListView that I use to display file/folder hierarchy inside my application. I'm using a custom layout with an ImageView and a TextView. I want to change the image of the ImageView according to TextView text is ether folder or file. Can I do this without using a custom ArrayAdapter or if I have to use a custom ArrayAdapter how can I change ImageView icon in the runtime.?

1
  • You will have to write a custom adapter. Commented Sep 11, 2013 at 7:09

4 Answers 4

1

You will have to use a custom adapter any time you need more than one variable item per entry in the list (i.e. in your case you have two, the ImageView and the TextView). Changing data at runtime is most easily achieved by calling ArrayAdapter.notifyDataSetChanged().

Sign up to request clarification or add additional context in comments.

Comments

0

You can check that in getView function of custom adapter and set imageview according to the textview. see this example for custom array adapter.

Comments

0

You can use ImageView.setImageResource(int) method.

1 Comment

can I use it even my imageview is in diffrent layout.?
0

Use this getView() method in your custom adapter class.

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View newRowView = convertView;
    ViewHolder viewHolder;


    if(newRowView == null)
    {
    LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    newRowView = inflator.inflate(R.layout.my_list, parent,false); 
    viewHolder = new ViewHolder();

    viewHolder.name = (TextView) newRowView.findViewById(R.id.textInList);

    viewHolder.image = (ImageView) newRowView.findViewById(R.id.iconInList);

    newRowView.setTag(viewHolder);

    }
    else
    {
        viewHolder = (ViewHolder) newRowView.getTag();

    }


    viewHolder.image.setImageDrawable(textImages[position]);

    viewHolder.name.setText(names[position]);

    return newRowView; 
}



static class ViewHolder
{
    TextView name;
    ImageView image;

}

Here textImages & names are the arrays which I passed to the custom adapter from the activity class.

This is the most memory efficient and fast way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.