带数组的Android ListView

带数组的Android ListView,android,arrays,listview,android-listview,Android,Arrays,Listview,Android Listview,所以,我对安卓还是有点陌生,试图在网上找到一些帮助,但似乎找不到明确的解决方案。我正在创建一个只包含ListView的应用程序。我创建了一个由2个文本视图组成的xml文件(它基本上与内置的android.R.layout.simple_list_item_2布局相同,但我猜我可能需要更改它)。看起来是这样的: TextView1 TextView2 TextView1 TextView1A TextView1B TextView1C TextView1D TextView2 TextView

所以,我对安卓还是有点陌生,试图在网上找到一些帮助,但似乎找不到明确的解决方案。我正在创建一个只包含ListView的应用程序。我创建了一个由2个文本视图组成的xml文件(它基本上与内置的android.R.layout.simple_list_item_2布局相同,但我猜我可能需要更改它)。看起来是这样的:

TextView1
TextView2
TextView1
TextView1A
TextView1B
TextView1C
TextView1D

TextView2
TextView2A
TextView2B

TextView3
TextView3A
TextView3B
TextView3C
没什么特别的。我遇到的问题是使用阵列。对于我的应用程序,TextView1只是一个设置字符串。不过,从技术上讲,Textview2将不仅仅是一个单一的TextView。我想创建一个数组,创建必要数量的TextView,并将它们放在TextView1下ListView的每一行中

例如,如果我有以下3个阵列:

String[] array1 = {TextView1A, TextView1B, TextView1C, TextView1D};
String[] array2 = {TextView2A, TextView2B};
String[] array3 = {TextView3A, TextView3B, TextView3C};
我希望输出像这样:

TextView1
TextView2
TextView1
TextView1A
TextView1B
TextView1C
TextView1D

TextView2
TextView2A
TextView2B

TextView3
TextView3A
TextView3B
TextView3C

就像我说的,我对教自己安卓还是有点陌生。我已经通过ArrayAdapters完成了简单的列表视图,但我不确定这是否适用于这里。我希望创建更多的数组,每个数组可以有不同数量的元素,所以我真的不认为简单地创建大量自定义ListView行是一种选择。我真的不知道如何开始实施这一点,因此任何朝着正确方向推进的举措都将受到赞赏。对于如何做到这一点,我们也欢迎提出任何其他建议。谢谢。

那么您只想将所有数组合并到一个列表中

在我看来,你仍然可以用一个ArrayAdapter来完成这个任务。您需要一个额外的数组作为ArrayAdapter的数据源,它包含连接在一起的所有数组,您只需手动保持它的同步

如果这个设置不适合你,你也可以看看
它将任意数量的任意类型的适配器包装起来,并将它们全部组合到一个列表中。

我认为如果您将该
列表视图中的每一行都视为其数据的所有者,则会有更好的实现。例如,创建一个类MyObject,该类具有一行所需的所有属性。在我下面的自定义适配器中,MyObject可以有四个不同的
字符串
,但它可以是任何东西

然后实现一个附加到
列表视图的自定义适配器:

public class MyCustomAdapter extends ArrayAdapter<MyObject> {

    Context context;

    public MyCustomAdapter(Context context, int resourceId, List<MyObject> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        MyObject myObj = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.my_listview_item, null);

            holder = new ViewHolder();
            holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
            holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);

            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        String one = myObj.getString1();
        String two = myObj.getString2();
        String three = myObj.getString3();
        String four = myObj.getString4();

        // Depending if any of those strings are empty/null set textView1 and textView2 appropriatelly

        return convertView;
    }

    /* private view holder class */
    private class ViewHolder {
        TextView textView1;
        TextView textView2;
    }
}
公共类MyCustomAdapter扩展了ArrayAdapter{
语境;
公共MyCustomAdapter(上下文上下文、int resourceId、列表项){
超级(上下文、资源ID、项目);
this.context=上下文;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
ViewHolder=null;
MyObject MyObject=getItem(位置);
LayoutInflater mInflater=(LayoutInflater)context.getSystemService(Activity.LAYOUT\u INFLATER\u SERVICE);
if(convertView==null){
convertView=mInflater.inflate(R.layout.my_listview_项目,空);
holder=新的ViewHolder();
holder.textView1=(TextView)convertView.findViewById(R.id.textView1);
holder.textView2=(TextView)convertView.findViewById(R.id.textView2);
convertView.setTag(支架);
}否则
holder=(ViewHolder)convertView.getTag();
字符串1=myObj.getString1();
字符串2=myObj.getString2();
字符串3=myObj.getString3();
字符串4=myObj.getString4();
//根据这些字符串是否为空/空,适当设置textView1和textView2
返回视图;
}
/*私有视图持有者类*/
私有类视窗持有者{
文本视图文本视图1;
文本视图文本视图2;
}
}

您可以查看ExpandableListView。这里有一个参考:谢谢你给我指出这一点。我从未尝试过使用ExpandableListView(甚至不知道它的存在),但在阅读它之后,这可能是最好的选择。