简体   繁体   中英

How can I set a ImageView in a custom ListView that depends on the text of the TextView?

I have a custom ListView with a rowItem that contains a TextView and a ImageView . The rowItems can be added dynamically from the user.

So how is it possible to set a specific ImageView to a specific TextView . For instance, the text "bird" get the ImageView named bird and so on.

I tried it with SwitchCase but it didn't work.

My ListView adapter:

class SessionItemAdapter extends ArrayAdapter<Map> {
    final ArrayList<Map> values;
    Context context;

    SessionItemAdapter(Context context, ArrayList<Map> values) {
        super(context, R.layout.session_list_layout_row, values);
        this.values = values;
        this.context = context;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.session_list_layout_row, parent, false);

        TextView tv = (TextView) rowView.findViewById(R.id.stepTime);
        final int rowId = (Integer) values.get(position).get("id");
        int beepStringId = getResources().getIdentifier("exercise_typ_" + values.get(position).get("type"), "string", getPackageName());

        rowView.setTag(rowId);

        tv.setText(getResources().getString(beepStringId));
        ImageView image =(ImageView) rowView.findViewById(R.id.imageView2);

        switch (rowId) {
            case 1 : image.setImageResource(R.drawable.ic_launcher);
                break;

            case 2 : image.setImageResource(R.drawable.ic_down);
                break;
        }

        ImageButton deleteSession = (ImageButton) rowView.findViewById(R.id.session_item_del);
        deleteSession.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*if (!startStopToggle.isChecked()) {*/
                sessionAdapter.remove(values.get(position));
                sessionAdapter.notifyDataSetChanged();
            }
        });

        return rowView;
    }

    //in your adapter getAllPlayers() would be something like this
    public List<String> getAllPlayers() {
        List<String> list = new ArrayList<String>();
        for (Map item : values) {
            int beepStringId = context.getResources().getIdentifier("exercise_typ_"
                + item.get("type"), "string", context.getPackageName());
            String str = context.getResources().getString(beepStringId);
            list.add(str);
        }
        return list;
    }
}

When the TextView is filled with the word "bird" you can use it to fill the ImageView with the proper image.

txtView.setText(textVar);

if (txtView.getText().equals("bird")) {
    imageView.setImageResource(R.drawable.bird_drawable);
}

One way to do this is if you have control over naming the drawable names you can name you drawable resources same as the text/title to be matched. So you would have drawable names like birds.png, animals.png etc. Then you can use this answer to get the drawable dynamically and attach to image view. So your code should look something like this

int drawableResourceId = context.getResources().getIdentifier(getResources().getString(beepStringId), "drawable", context.getPackageName());
imageView.setImageResource(drawableResourceId);

The drawback for this approach is that if you have spaces or special characters in your title then you have to handle it separately. But its sufficient for fairly simple set of titles.

Alternatively you can use switch case like you wanted starting from API 19 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM