Android ListView突出显示会导致奇怪的行为

时间:2013-01-30 01:16:39

标签: android listview view highlighting

我试图让ListView在选中每个项目后显示多个高亮显示,但是我从View中获得了奇怪的行为。如果单击第一个项目,则会突出显示(单击弹出窗口上的“确定”按钮后)。但是,如果单击第二个项目,则最后一个项目也会突出显示。以下是显示renderer.setBackgroundResource仅被调用适当次数的日志。

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)

如果您尝试不同的选择顺序,会发生各种奇怪的行为。我不确定这是否是最好的方法,或者问题是什么。 (使用Android 2.3.3 API 10)

感谢您的帮助,

Curchod。

这是活动:

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;
}

}

这是actrivity_highlight.xml布局

<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>

这是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>

1 个答案:

答案 0 :(得分:0)

你的getView方法可能会获得一个已经设置了背景的循环convertView。

试试这个:

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);
}