I have a strange behaviour with the ViewImage in the ListView linked in a ListActivity.
So, I have list activity for which the list is made of 3 text fields and one image.
<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>
My adapter looks at changing the image based on some criteria. I have the following code:
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;
}
The images displayed in the list box are random but often equal to the last row. I inflating the row each time to make sure that a new instance of the row is created. The image displayed in not the one set up in the layout file.
Cheers.
David.
Looks like you are missing the braces for the last
elsesegment: