Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 重用同一布局时getview()在ListView中无法正常工作_Android_Listview_View_Checkbox - Fatal编程技术网

Android 重用同一布局时getview()在ListView中无法正常工作

Android 重用同一布局时getview()在ListView中无法正常工作,android,listview,view,checkbox,Android,Listview,View,Checkbox,我正在尝试使用具有以下示例代码的库“”: public class SectionListActivity extends Activity { private class StandardArrayAdapter extends ArrayAdapter<SectionListItem> { private final SectionListItem[] items; public StandardArrayAdapter(final Context cont

我正在尝试使用具有以下示例代码的库“”:

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);
    }
}
public类SectionListActivity扩展活动{
私有类StandardArrayAdapter扩展了ArrayAdapter{
私人最终部分清单项目[]项目;
公共标准ArrayAdapter(最终上下文,
final int textViewResourceId,final SectionListItem[]项){
super(上下文、textViewResourceId、项);
这个项目=项目;
}
@凌驾
公共视图getView(最终整型位置,最终视图转换视图,
最终视图组(父视图){
视图=转换视图;
如果(视图==null){
final LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
视图=vi.充气(R.layout.example\u list\u视图,空);
}
最终章节列表项目当前项目=项目[位置];
如果(currentItem!=null){
最终文本视图文本视图=(文本视图)视图
.findViewById(R.id.example\u text\u view);
if(textView!=null){
setText(currentItem.item.toString());
}
}
返回视图;
}
}
SectionListItem[]exampleArray={//防止重新格式化的注释
新章节列表项(“测试1-A”、“A”)//
新章节列表项(“测试2-A”、“A”)//
新章节列表项(“测试3-A”、“A”)//
新章节列表项(“测试4-A”、“A”)//
新的章节列表项(“测试5-A”、“A”)//
新章节列表项(“试验6-B”、“B”)//
新章节列表项(“测试7-B”、“B”)//
新章节列表项(“测试8-B”、“B”)//
新章节列表项目(“测试9-长”、“长章节”)//
新章节列表项目(“测试10-长”、“长章节”)//
新章节列表项目(“测试11-长”、“长章节”)//
新章节列表项目(“测试12-长”、“长章节”)//
新章节列表项目(“试验13-长”、“长章节”)//
新的章节列表项(“测试14-再次“A”)//
新的章节列表项(“测试15-再次“A”)//
新的章节列表项(“测试16-再次“A”)//
新的章节列表项(“测试17-B再次出现”,“B”)//
新的章节列表项(“再次测试18-B”,“B”)//
新的章节列表项(“测试19-B再次出现”,“B”)//
新的章节列表项(“再次测试20-B”,“B”)//
新的章节列表项(“测试21-B再次出现”,“B”)//
新的章节列表项(“测试22-B再次出现”,“B”)//
新章节列表项(“测试23-C”、“C”)//
新章节列表项(“测试24-C”、“C”)//
新章节列表项目(“测试25-C”、“C”)//
新章节列表项(“试验26-C”、“C”)//
};
专用标准阵列适配器阵列适配器;
私有部分列表适配器部分适配器;
私有部分列表视图列表视图;
@凌驾
创建时的公共void(最终捆绑包savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arrayAdapter=新标准arrayAdapter(此,R.id.example\u text\u视图,
示例(阵列);
sectionAdapter=新的SectionListAdapter(GetLayoutFlater(),
阵列适配器);
listView=(SectionListView)findViewById(getResources().getIdentifier(
“节列表视图”、“id”,
这个.getClass().getPackage().getName());
setAdapter(sectionAdapter);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
MenuInflater充气机=getMenuInflater();
充气机。充气(右菜单。测试菜单,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.标准清单:
arrayAdapter=新标准arrayAdapter(此,
R.id.example_text_view,exampleArray);
sectionAdapter=新的SectionListAdapter(GetLayoutFlater(),
阵列适配器);
setAdapter(sectionAdapter);
返回true;
案例R.id.empty\u列表:
arrayAdapter=新标准arrayAdapter(此,
R.id.example_text_view,新的SectionListItem[]{});
sectionAdapter=新的SectionListAdapter(GetLayoutFlater(),
阵列适配器);
setAdapter(sectionAdapter);
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
}

问题如下:

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

据我所知,您的列表项有一个TextView和一个复选框。在getView中,您仅设置TextView,复选框由用户设置

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

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

Checkbox checkbox =  (CheckBox) view.findViewById(R.id.example_checkbox);
checkbox.setChecked(currentItem.getIsChecked());
或者,如果currentItem.getIsChecked()返回一个整数:

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

没错。该复选框将由用户设置。当我向下或向上滚动时,另一个复选框也被选中。我不明白你跟踪他的状态是什么意思。这与本主题中的“getItemViewType”和“getViewTypeCount”方法有关吗?但我不知道