如何添加取决于 ListView 中 TextView 文本的 ImageView

How to add ImageView's that are depending on the text of the TextView in a ListView

我有一个自定义 ListView,其中 RowItem 包含 TextViewImageViewRowItems 将动态添加。

我只想知道如何将 ImageView 添加到 TextView 中的特定文本。

所以如果在TextView中代表"Title1",ImageView "R.drawable.ic_title1"应该添加到RowItem


这是我的代码

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(position){

                case 1 : image.setImageResource(R.drawable.ic_title1);
                    break;

                case 2 : image.setImageResource(R.drawable.ic_title2);
                    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;
        }


    }

我用 Switch 语句和 position 进行了尝试,但只将 ImageView 设置为 RowItem ,即 RowItem 中所写的位置25=].

您可以根据 TextView 的文本进行 switch-case 声明,如下所示

switch(tv.getText().toString()){

                case "Title1" : image.setImageResource(R.drawable.ic_title1);
                    break;

                case "Title2" : image.setImageResource(R.drawable.ic_title2);
                    break;

                default:
                    //Default image here, if no case found
                break;
}

注意:以上答案需要 JRE 1.7 合规性

这样的事情怎么样。

image.setImageResource(getImageId(this, "ic_" + tv.getText().toString().toLowerCase());

所以基本上你是从文本视图中获取标题,将其操作为你存储图像的格式,并调用下面的帮助程序来获取正确的图像 ID。

public static int getImageId(Context context, String imageName) {
    return context.getResources().getIdentifier("drawable/" + imageName, null, context.getPackageName());
} 

本例中的解决方案是添加获取和设置 ListView 对象的各个字段的方法。例如,由于您的行项目中有一个 TextView 和一个 ImageView,我们假设您的行对象如下所示:

public class ListObject
{

    public ListObject (String textView, String imageView)
    {
        super();
        this.textView                      = textView;
        this.imageView                     = imageView;
    }

    private String     textView;
    private String     imageView;

    public String getTextView ()
    {
        return textView;
    }

    public void setTextView (String pTextView)
    {
        this.textView = pTextView;
    }

    public String getImageView ()
    {
        return imageView;
    }

    public void setImageView (String pImageView)
    {
        this.imageView = pImageView;
    }
}

实施这些后,您可以根据您的 TextView 轻松设置您的 ImageView,如下所示:

// As an example, let us use the first list item
ListObject listObject = (ListObject) parent.getItemAtPosition(0);

if (listObject.getTextView == "Title1")
{
    listObject.setImageView("NameOfResource");
}
// so on and so forth