Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.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 困惑于Android中使用哪种listView和适配器来实现小型阵列_Java_Android_Listview_Android Listview_Android Arrayadapter - Fatal编程技术网

Java 困惑于Android中使用哪种listView和适配器来实现小型阵列

Java 困惑于Android中使用哪种listView和适配器来实现小型阵列,java,android,listview,android-listview,android-arrayadapter,Java,Android,Listview,Android Listview,Android Arrayadapter,我很少使用ListView,现在我需要帮助决定使用哪种适配器 我需要三个标题: String fruit = "Fruits"; String vegetable = "Vegetables"; String bread = "Breads" 我还有三个数组要用作listView的数据(不使用此示例应用程序的数据库)。我可能希望有三个ArrayList,但现在我将使用数组 String[] fruits = {"Apple","Orange"}; String[] vegetables = {

我很少使用ListView,现在我需要帮助决定使用哪种适配器

我需要三个标题:

String fruit = "Fruits";
String vegetable = "Vegetables";
String bread = "Breads"
我还有三个数组要用作listView的数据(不使用此示例应用程序的数据库)。我可能希望有三个ArrayList,但现在我将使用数组

String[] fruits = {"Apple","Orange"};
String[] vegetables = {"Potato","Carrot"};
String[] breads = {"Rye","Wheat"};
我希望ListView看起来像Android 4.x设置屏幕,带有“标题”标题


有人能为我指出这样的事情的正确方向吗?再一次如果允许我在运行时添加水果,我可能想切换到ArrayList。

有一个android库可用于此,它可能会解决您的问题。

您可能希望使用自定义阵列适配器。基本上,您需要在模型类中封装一行的所有数据。并以该模型类的数组作为数据源制作一个数组适配器

在自定义适配器部分查看此链接以供参考:

在Android中有一个用于创建设置的链接。它看起来就像用户应用程序设置活动一样


或者,如果您想要自己的实现。你应该用它来管理这件事。创建一些类来包装这3个数组,他将根据其状态返回活动数组。

最简单的方法是为每个组使用单独的列表视图。但是当你想把所有的方法都列在一个列表中时,我认为你应该使用并阅读两种方法:=2在你的例子中,和。主要问题是,您必须计算哪个位置上的类型:

  • 果实
  • 苹果
  • 橙色的
  • 蔬菜
  • 马铃薯
  • 胡萝卜

  • 使用自定义适配器。以下是示例。

    另外,

  • 创建
    head.xml
    body.xml

  • 做点什么

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi = convertView;
        if (vi == null) {
            if ( isHead() ) // check whether the view is head?
                vi = inflater.inflate(R.layout.head, null);
            else
                vi = inflater.inflate(R.layout.body, null);
        }
        TextView text = (TextView) vi.findViewById(R.id.text);
        text.setText(data[position]);
        return vi;
    }
    

  • github上有类似的东西吗?我不知道如何通过code.google.com下载这个库。。。我知道使用SharedReferences可以很容易地为Android设置屏幕,但我正在尝试将此listView与三个不同的数组组合在一起。@EGHDK您应该使用MVC。
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi = convertView;
        if (vi == null) {
            if ( isHead() ) // check whether the view is head?
                vi = inflater.inflate(R.layout.head, null);
            else
                vi = inflater.inflate(R.layout.body, null);
        }
        TextView text = (TextView) vi.findViewById(R.id.text);
        text.setText(data[position]);
        return vi;
    }