【问题标题】:Issue trying to change ImageView in List Activity尝试在列表活动中更改 ImageView 的问题
【发布时间】:2012-10-11 16:16:48
【问题描述】:

我在尝试更改列表视图中的图像时遇到问题。

下面是列表中行的布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="horizontal" 
android:gravity="left">


<ImageView
    android:id="@+id/flagIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="0.1"
    android:src="@drawable/orange_flag"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/location_row_item_main_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/location_row_item_secondary_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" 
        android:textAppearance="?android:attr/textAppearanceSmall"/>

</LinearLayout>

运行正常并显示指定的可绘制对象(即 orange_flag),但是当我尝试在以下代码中更改它时,这并没有改变:

private class MyListAdapter extends ResourceCursorAdapter { 

    // In your ListActivity class, create a new inner class that extends ResourceCursorAdapter. 
    //This inner class is the custom CursorAdapter we will use to manage how data is bound to a list item:

    public MyListAdapter(Context context, Cursor cursor) { 
        super(context, R.layout.row_location, cursor);
    } 

    @Override
    public void bindView(View view, Context context, Cursor cursor) { 
        TextView title = (TextView) view.findViewById(R.id.location_row_item_main_text);
        title.setText(cursor.getString(
                cursor.getColumnIndex(RMDbAdapter.RACKING_SYSTEM)));
        ImageView flagIcon = (ImageView) view.findViewById(R.id.flagIcon);
        String risk = cursor.getString(cursor.getColumnIndex(RMDbAdapter.RISK));
        if (risk == "Red Risk"){
            flagIcon.setImageResource(R.drawable.red_flag);
        }
        else if (risk == "Green Risk"){
            flagIcon.setImageResource(R.drawable.green_flag);
        }
        else if (risk =="No Risk"){
            flagIcon.setImageResource(R.drawable.note);
        }

有什么想法吗?!

【问题讨论】:

  • 就是这样 - 谢谢僚机。非常感谢-如果您想回答,我会给您打勾!! :)

标签: android image listview drawable


【解决方案1】:

在比较字符串数据类型的内容时,始终使用equals()equalsIgnoreCase()

对于java中的字符串:

  • == 比较字符串是否相同的对象
  • equals() 比较字符串是否具有相同的字符序列

【讨论】:

  • 只是为了确保将“StRIng”与“sTriNG”进行比较并返回true,此方法忽略大小写差异。
【解决方案2】:

设置资源后尝试添加flagIcon.invalidate()

【讨论】:

  • 感谢 Blumer 的快速响应,但这不起作用(我尝试了 flagIcon.invalidate(); 顺便说一句,因为无效不被接受)。还有其他想法吗?
  • 鉴于僚机的优点,您能否在其中放置一条 Log 语句以确认确实达到了设置 ImageResouce 的代码?另外,如果您尝试使用 getResources().getDrawable(R.drawable.green_flag) 而不是 setImageResource() 会发生什么?
【解决方案3】:

而不是尝试

flagIcon.setImageResource(R.drawable.red_flag);

你可以试试这个:

flagIcon.setBackgroundResource(R.drawable.red_flag);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多