Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.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
Java 重写getView()时出现问题_Java_Android_Listview - Fatal编程技术网

Java 重写getView()时出现问题

Java 重写getView()时出现问题,java,android,listview,Java,Android,Listview,我在重写getview方法时不断遇到错误。这就是我所拥有的: 公共类UnwatcheDepisodActivity扩展ListActivity{ private String[] values = new String[]{"Row 1", "Row 2", "Row 3"}; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView

我在重写getview方法时不断遇到错误。这就是我所拥有的:

公共类UnwatcheDepisodActivity扩展ListActivity{

private String[] values = new String[]{"Row 1", "Row 2", "Row 3"};

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.episode_main);
    setListAdapter(new EpisodeListAdapter());

}

private class EpisodeListAdapter extends ArrayAdapter<String>{

    private LayoutInflater li = LayoutInflater.from(this.getContext());

    public EpisodeListAdapter(){
        super(UnwatchedEpisodesActivity.this, 0, values);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = li.inflate(R.layout.episode_row, parent, false);
        }

        ((TextView) convertView.findViewById(R.id.text1)).setText(values[position]);
        return convertView;
    }

}

显然,这只是一个示例;基本上我想知道如何添加这些“分隔符”

您应该通过

android:id="@+id/text1"
此部分:
android:id=“@android:id/text1”
不正确

要分离项目,您应该使用
可扩展列表活动
,而不是
列表活动
,其中您的组将是部分(第一、第二等),子列表将是您当前的

在这种情况下,您应该使用
BaseExpandableListAdapter
来填充视图。
虽然有一些方法需要重写,但使用适当的数据结构不会有问题

然后,您的
getView
方法的主体应该放在
BaseExpandableListAdapter
getChildView
方法中,您应该为
getGroupView
覆盖中的部分使用另一个
TextView
或更复杂的布局

这样你就可以把你的剧集分成几个部分。

使用
android:id=“@+id/text1”
而不是 android:id=“@android:id/text1”第一部分

关于第二期,据我所知,您的需求:

在getView()方法中,应该为不同的位置膨胀不同的xml。 例如:

if (position == 0) {
    convertView = li.inflate(R.layout.episode_row, parent, false);
    //fetch the text view and other things and set their value
    return convertView;
} else if (position == 1) {
    //similarly do here also and return convertview
}

希望这将有助于解决您的问题。

这似乎是问题所在。谢谢!另外:您对我的第二个问题有什么想法?请参阅我的更新以获得解决方案,将您的剧集分成多个部分。
getView()
是否针对阵列的每个位置循环?如果是,我可以看到这项工作,而且看起来很简单。。是的,getView()为每个位置循环,这非常容易,您可以根据需要自定义。最好的…:)注意,尽管此解决方案可以应用于平面数据结构,但您需要将标题值插入到片段列表中,或者必须能够确定每个部分的第一个片段。position=0适用于r第一项,在上面的if子句中,它显示头而不是第一个iem!对于分层数据结构,ExpandableListView是解决方案。
android:id="@+id/text1"
if (position == 0) {
    convertView = li.inflate(R.layout.episode_row, parent, false);
    //fetch the text view and other things and set their value
    return convertView;
} else if (position == 1) {
    //similarly do here also and return convertview
}