ListActivity的奇怪行为

时间:2012-10-12 15:55:25

标签: android

我在ListView中链接的ListView中的ViewImage有一种奇怪的行为。

所以,我有列表活动,列表由3个文本字段和一个图像组成。     

 <TextView android:id="@+id/me_games_won"
     android:textSize="16sp"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

<TextView android:id="@+id/gamesmate_username"
     android:textSize="16sp"
     android:textStyle="bold"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

 <TextView android:id="@+id/him_games_won"
     android:textSize="16sp"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

 <ImageView
    android:id="@+id/imageActionHomepage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/im_completed" />
</LinearLayout>

我的适配器会根据某些条件查看更改图像。我有以下代码:

public View getView(int position, View convertView, ViewGroup parent) {
        // assign the view we are converting to a local variable

        View v;

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.homepage_listitem, null);

        /*
         * Recall that the variable position is sent in as an argument to this method.
         * The variable simply refers to the position of the current object in the list. (The ArrayAdapter
         * iterates through the list we sent it)
         * 
         * Therefore, i refers to the current Item object.
         */
        JSONGame i = (JSONGame) objects.get(position);

        if (i != null) {
            // This is how you obtain a reference to the TextViews.
            // These TextViews are created in the XML files we defined.
            TextView tt = (TextView) v.findViewById(R.id.me_games_won);
            TextView ttd = (TextView) v.findViewById(R.id.gamesmate_username);
            TextView mt = (TextView) v.findViewById(R.id.him_games_won);
            ImageView iv = (ImageView) v.findViewById(R.id.imageActionHomepage);

            // check to see if each individual textview is null.
            // if not, assign some text!
            if (tt != null) {
                tt.setText(Long.toString(i.getScore()));
            }
            if (ttd != null) {
                ttd.setText(i.getGamemateUsername());
            }
            if (mt != null) {
                mt.setText(Long.toString(i.getGamemateScore()));
            }
            if (i.getAction().equals(JSONGame.GAME_NONE))
                iv.setImageResource(R.drawable.im_completed);
            else if (i.getAction().equals(JSONGame.GAME_ACCEPT)) {
                iv.setOnClickListener(new HomepageMenuAdapter(this.activity, position, R.drawable.im_accept));
                iv.setImageResource(R.drawable.im_accept);
            } else if (i.getAction().equals(JSONGame.GAME_WAITING))
                iv.setImageResource(R.drawable.im_awaiting);
            else
                iv.setOnClickListener(new HomepageMenuAdapter(this.activity, position, R.drawable.im_continue));
                iv.setImageResource(R.drawable.im_continue);
        }

        // the view must be returned to our activity
        return v;
    }

列表框中显示的图像是随机的,但通常等于最后一行。我每次都要给行充气,以确保创建了一个新的行实例。显示的图像不是布局文件中设置的图像。

干杯。 大卫。

1 个答案:

答案 0 :(得分:2)

您似乎错过了上一个else细分的大括号:

    :
    } else if (i.getAction().equals(JSONGame.GAME_WAITING))
            iv.setImageResource(R.drawable.im_awaiting);
    else { // <-- missing
            iv.setOnClickListener(new HomepageMenuAdapter(this.activity, position, R.drawable.im_continue));
            iv.setImageResource(R.drawable.im_continue);
    } // <-- missing
    :