2

我试图让 ListView 在选择每个项目后显示多个亮点,但我从视图中得到奇怪的行为。如果单击第一项,它会突出显示(单击弹出窗口上的 OK 按钮后)。但是,如果您单击第二项,则最后一项也会突出显示。这是显示 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)

谢谢你的帮助,

库尔乔德。

这是活动:

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

}

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

1 回答 1

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);
}
于 2013-01-30T01:43:43.613 回答