I’m trying to make a ListView show multiple highlights after each item is selected, but I get strange behavior from the View. If you click on the first item, it is highlighted (after clicking the OK button on the popup window). If you click on the second item however, the last item is also highlighted. Here is the log showing that the renderer.setBackgroundResource is only called the appropriate number of times.
01-30 00:54:07.957: I/HighlightActivity(343): ListView.onItemClick: selected = 0
01-30 00:54:09.757: I/HighlightActivity(343): FINISHED_WORDS[0].equals(set)
01-30 00:54:11.387: I/HighlightActivity(343): ListView.onItemClick: selected = 1
01-30 00:54:12.757: I/HighlightActivity(343): FINISHED_WORDS[0].equals(set)
01-30 00:54:12.776: I/HighlightActivity(343): FINISHED_WORDS[1].equals(set)
If you try different selection orders, all kinds of weird behavior happens. I’m not sure if this is the best way to do this, or what the problem is. (Using Android 2.3.3 API 10)
Thanks for your help,
Curchod.
Here is the Activity:
public class HighlightActivity extends ListActivity
{
private static final String DEBUG_TAG = "HighlightActivity";
final Context context = this;
private String[] FINISHED_WORDS = {"","","","","",""};
private String[] WORDS = {"one","two","three","four","five","six"};
int selected;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_highlight,
R.id.highlight_layout, WORDS)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final View renderer = super.getView(position, convertView, parent);
if (FINISHED_WORDS[position].equals("set"))
{
renderer.setBackgroundResource(android.R.color.darker_gray);
Log.i(DEBUG_TAG, "FINISHED_WORDS["+position+"].equals(set)");
}
return renderer;
}
});
ListView list_view = getListView();
list_view.setTextFilterEnabled(true);
list_view.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
selected = position;
Log.i(DEBUG_TAG, "ListView.onItemClick: selected = "+selected);
final String selected_word = WORDS[position];
LayoutInflater layout_inflater = LayoutInflater.from(context);
View popup_view = layout_inflater.inflate(R.layout.highlight_popup, null);
final AlertDialog.Builder alert_dialog_builder = new AlertDialog.Builder(context);
alert_dialog_builder.setView(popup_view);
final TextView ard_player_words_popup_text = (TextView) popup_view.findViewById(R.id.highlight_popup_text);
ard_player_words_popup_text.setText(selected_word+" selected");
alert_dialog_builder.setCancelable(false).setPositiveButton("OK",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id)
{
FINISHED_WORDS[selected] = "set";
dialog.cancel();
ListView list_view = getListView();
ArrayAdapter<?> adapter = (ArrayAdapter<?>) list_view.getAdapter();
adapter.notifyDataSetChanged();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id)
{
dialog.cancel();
}
});
AlertDialog alert_dialog = alert_dialog_builder.create();
alert_dialog.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_highlight, menu);
return true;
}
}
Here is the actrivity_highlight.xml layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/highlight_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp" >
</TextView>
</RelativeLayout>
And here is the highlight_popup.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/highlight_popup_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
It's possible your getView method is getting a recycled convertView, with a background already set.
Try this:
if (FINISHED_WORDS[position].equals("set"))
{
renderer.setBackgroundResource(android.R.color.darker_gray);
Log.i(DEBUG_TAG, "FINISHED_WORDS["+position+"].equals(set)");
}
else
{
renderer.setBackgroundResource(R.color.normal_background);
}