重用相同的布局时,getview()在ListView中无法正常工作

时间:2013-12-10 23:05:17

标签: android listview view checkbox

我正在尝试使用此示例代码库“android-section-list”:

public class SectionListActivity extends Activity {

private class StandardArrayAdapter extends ArrayAdapter<SectionListItem> {

    private final SectionListItem[] items;

    public StandardArrayAdapter(final Context context,
            final int textViewResourceId, final SectionListItem[] items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView(final int position, final View convertView,
            final ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            final LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.example_list_view, null);
        }
        final SectionListItem currentItem = items[position];
        if (currentItem != null) {
            final TextView textView = (TextView) view
                    .findViewById(R.id.example_text_view);
            if (textView != null) {
                textView.setText(currentItem.item.toString());
            }
        }
        return view;
    }
}

SectionListItem[] exampleArray = { // Comment to prevent re-format
new SectionListItem("Test 1 - A", "A"), //
        new SectionListItem("Test 2 - A", "A"), //
        new SectionListItem("Test 3 - A", "A"), //
        new SectionListItem("Test 4 - A", "A"), //
        new SectionListItem("Test 5 - A", "A"), //
        new SectionListItem("Test 6 - B", "B"), //
        new SectionListItem("Test 7 - B", "B"), //
        new SectionListItem("Test 8 - B", "B"), //
        new SectionListItem("Test 9 - Long", "Long section"), //
        new SectionListItem("Test 10 - Long", "Long section"), //
        new SectionListItem("Test 11 - Long", "Long section"), //
        new SectionListItem("Test 12 - Long", "Long section"), //
        new SectionListItem("Test 13 - Long", "Long section"), //
        new SectionListItem("Test 14 - A again", "A"), //
        new SectionListItem("Test 15 - A again", "A"), //
        new SectionListItem("Test 16 - A again", "A"), //
        new SectionListItem("Test 17 - B again", "B"), //
        new SectionListItem("Test 18 - B again", "B"), //
        new SectionListItem("Test 19 - B again", "B"), //
        new SectionListItem("Test 20 - B again", "B"), //
        new SectionListItem("Test 21 - B again", "B"), //
        new SectionListItem("Test 22 - B again", "B"), //
        new SectionListItem("Test 23 - C", "C"), //
        new SectionListItem("Test 24 - C", "C"), //
        new SectionListItem("Test 25 - C", "C"), //
        new SectionListItem("Test 26 - C", "C"), //
};

private StandardArrayAdapter arrayAdapter;

private SectionListAdapter sectionAdapter;

private SectionListView listView;

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    arrayAdapter = new StandardArrayAdapter(this, R.id.example_text_view,
            exampleArray);
    sectionAdapter = new SectionListAdapter(getLayoutInflater(),
            arrayAdapter);
    listView = (SectionListView) findViewById(getResources().getIdentifier(
            "section_list_view", "id",
            this.getClass().getPackage().getName()));
    listView.setAdapter(sectionAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.test_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.standard_list:
        arrayAdapter = new StandardArrayAdapter(this,
                R.id.example_text_view, exampleArray);
        sectionAdapter = new SectionListAdapter(getLayoutInflater(),
                arrayAdapter);
        listView.setAdapter(sectionAdapter);
        return true;
    case R.id.empty_list:
        arrayAdapter = new StandardArrayAdapter(this,
                R.id.example_text_view, new SectionListItem[] {});
        sectionAdapter = new SectionListAdapter(getLayoutInflater(),
                arrayAdapter);
        listView.setAdapter(sectionAdapter);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

}

问题如下:

当我选中一个复选框并向下或向上滚动时,会检查另一个以前未选中的复选框。 我认为这是getView类型的问题,但不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:2)

据我了解,你的列表项有一个TextView和一个CheckBox。在getView中,您只设置TextView,而CheckBox由用户设置。

列表视图的单元格可以重复使用,因此当用户选中一个并向下滚动时,最终将重新出现带有已检查CheckBox的相同单元格。例如,假设您随时可以看到3个单元格。首先,你在屏幕上有A,B,C,用户点击A.当用户滚动时,系统可能会创建一个新的单元格D,然后重复使用A.所以你可能会遇到C,D的情况,A现在在屏幕上。仍然检查了CheckBox,因为它从未重置过。要防止这种情况,您应该在getView()中显式跟踪和设置CheckBox的状态。

例如,您可以向SectionListItem添加一个布尔值,以跟踪已检查的状态。当用户单击CheckBox时,您将设置布尔值isChecked,然后在getView()中,您将根据currentItem中的布尔值设置CheckBox。

Checkbox checkbox =  (CheckBox) view.findViewById(R.id.example_checkbox);
checkbox.setChecked(currentItem.getIsChecked());

或者,如果currentItem.getIsChecked()返回一个整数:

checkbox.setChecked(currentItem.getIsChecked() != 0);