如何在 ExpandableListView 中添加多个 Textview

How to add multiple Textviews in a ExpandableListView

我不知道如何在我的 Expandablelist 适配器和 MainActivty 中添加多个 Textview。当我试图在适配器中添加一个新的 Textview 时,我无法弄清楚如何在 MainAcivity 中初始化 Textview。它只是将我放入第一个 Textview 中的文本复制到新的文本视图中。我发现了一些其他的 Whosebug 帖子,但其中 none 有答案。如果有人知道如何执行此操作,我将不胜感激。请举个例子,因为我是初学者。我遵循了 http://www.tutorialsbuzz.com/2014/07/custom-expandable-listview-image-text.html

中的教程

另请参阅我的代码中的一些注释,了解我想要完成的操作。

可扩展列表适配器:

import android.view.LayoutInflater;


                import android.content.Context;

                import android.view.View;
                import android.view.ViewGroup;
                import android.widget.BaseExpandableListAdapter;
                import android.widget.ImageView;
                import android.widget.TextView;

        import java.util.ArrayList;

        public class ExpandListAdapter extends BaseExpandableListAdapter {

            private Context context;
            private ArrayList<Group> groups;

            public ExpandListAdapter(Context context, ArrayList<Group> groups) {
                this.context = context;
                this.groups = groups;

            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                ArrayList<Child> chList = groups.get(groupPosition).getItems();
                return chList.get(childPosition);

            }

            @Override
            public long getChildId(int groupPosition, int childPosition) {
                return childPosition;
            }

            @Override
            public View getChildView(int groupPosition, int childPosition,
                                     boolean isLastChild, View convertView, ViewGroup parent) {

                Child child = (Child) getChild(groupPosition, childPosition);
                if (convertView == null) {
                    LayoutInflater infalInflater = (LayoutInflater) context
                            .getSystemService(context.LAYOUT_INFLATER_SERVICE);
                    convertView = infalInflater.inflate(R.layout.child_item, null);

                }
                TextView tv = (TextView) convertView.findViewById(R.id.country_name);
//trying to add this textview ltv
                TextView ltv = (TextView) convertView.findViewById(R.id.large_text);
                ImageView iv = (ImageView) convertView.findViewById(R.id.flag);
                tv.setText(child.getName().toString());
                iv.setImageResource(child.getImage());
                ltv.setText(child.getName().toString());












                    return convertView;
            }

            @Override
            public int getChildrenCount(int groupPosition) l

            @Override
            public Object getGroup(int groupPosition) {
                return groups.get(groupPosition);
            }

            @Override
            public int getGroupCount() {
                return groups.size();
            }

            @Override
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                                     View convertView, ViewGroup parent) {
                Group group = (Group) getGroup(groupPosition);
                if (convertView == null) {
                    LayoutInflater inf = (LayoutInflater) context
                            .getSystemService(context.LAYOUT_INFLATER_SERVICE);
                    convertView = inf.inflate(R.layout.group_item, null);
                }
                TextView tv = (TextView) convertView.findViewById(R.id.group_name);
                tv.setText(group.getName());
                return convertView;
            }

            @Override
            public boolean hasStableIds() {
                return true;
            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return true;
            }

        }

主要活动:

    import java.util.ArrayList;
            import java.util.Set;

            import android.app.Activity;
            import android.content.Intent;
            import android.os.Bundle;

            import android.view.View;
            import android.widget.Button;
            import android.widget.ExpandableListView;





            public class Brandspage extends Activity {

                private ExpandListAdapter ExpAdapter;
                private ExpandableListView ExpandList;
                private ArrayList<Group> ExpListItems;


                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_brandspage);
                    Button b1 = (Button) findViewById(R.id.button4);
                    b1.setOnClickListener(new View.OnClickListener() {


                        @Override
                        public void onClick(View v) {
                            Intent i = new Intent(Brandspage.this, MainActivity.class);
                            startActivity(i);

                        }
                    });


                    ExpandList = (ExpandableListView) findViewById(R.id.exp_list);
                    ExpListItems = SetStandardGroups();
                    ExpAdapter = new ExpandListAdapter(Brandspage.this, ExpListItems);
                    ExpandList.setAdapter(ExpAdapter);


                }


                public ArrayList<Group> SetStandardGroups() {

//how to initialize the ltv textview here with the other strings 


                    String group_names[] = {"A-G", "H-N", "O-U", "V-Z"
                    };


                    String country_names[] = {

                            " " +

                                    "\n" +
                                    "\n" +
                                    "  \n" +
                                    "\n" +
                                    "\n" +
                                    "\n" +
                                    "  \n" +
                                    "\n" +
                                    " \n" +
                                    "\n" +
                                    " \n "

                                   };




                    int Images[] = {R.drawable.ic_launcher, R.drawable.ic_launcher,
                            R.drawable.ic_launcher, R.drawable.ic_launcher


                    };


                    ArrayList<Group> list = new ArrayList<Group>();

                    ArrayList<Child> ch_list;

                    int size = 20;
                    int j = 0;


                    for (String group_name : group_names) {
                        Group gru = new Group();
                        gru.setName(group_name);





                            ch_list = new ArrayList<Child>();
                            for (; j < size; j++) {
                                Child ch = new Child();
                                ch.setName(country_names[j]);


                                ch.setImage(Images[j]);
                                ch_list.add(ch);


                            }
                            gru.setItems(ch_list);
                            list.add(gru);

                            size = size + 20;
                        }

                        return list;


                    }


                }

child item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:layout_marginLeft="50dp"
    >

    <TextView
        android:id="@+id/country_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="20dp"
        android:layout_below="@+id/large_text"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="66dp" />

    <ImageView
        android:id="@+id/flag"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher"

        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />


    //Want this Large Text to be in the ExpandableListView
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/large_text"
            android:layout_below="@+id/flag"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

    </RelativeLayout>

您需要编辑 Child class 以添加一个字段,例如 Detail 和此 field.Then 设置此字段的 getter setter 方法在

 public ArrayList<Group> SetStandardGroups()

您可以在 ExpandListAdapter 中获取字段值,然后您可以设置为第二个文本视图。 BrandPage activity class 是

public class BrandPage extends ActionBarActivity {

private ExpandListAdapter ExpAdapter;
private ExpandableListView ExpandList;
private ArrayList<Group> ExpListItems;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_brand_page);
    Button b1 = (Button) findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            Intent i = new Intent(BrandPage.this, MyActivity.class);
            startActivity(i);

        }
    });


    ExpandList = (ExpandableListView) findViewById(R.id.exp_list);
    ExpListItems = SetStandardGroups();
    ExpAdapter = new ExpandListAdapter(BrandPage.this, ExpListItems);
    ExpandList.setAdapter(ExpAdapter);

}


public ArrayList<Group> SetStandardGroups() {

//这里如何用其他字符串初始化ltv textview

    String group_names[] = {"A-G", "H-N", "O-U", "V-Z"
    };

    String country_names[] = {"1","2"};

    int Images[] = {R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher
    };


    ArrayList<Group> list = new ArrayList<Group>();
    ArrayList<Child> ch_list;
    int size = 2;
    int j = 0;

    for (String group_name : group_names) {
        Group gru = new Group();
        gru.setName(group_name);
        ch_list = new ArrayList<Child>();
        for (j=0; j < size; j++) {
            Child ch = new Child();
            // Set the fields
            ch.setDetails("item:"+j);
            ch.setName(country_names[j]);
            ch.setImage(Images[j]);
            ch_list.add(ch);
        }
        gru.setItems(ch_list);
        list.add(gru);
        //size = size + 2;
    }
    return list;
}

}

您的 ExpandableAdapter class 是

public class ExpandListAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<Group> groups;

public ExpandListAdapter(Context context, ArrayList<Group> groups) {
    this.context = context;
    this.groups = groups;

}

@Override
public Object getChild(int groupPosition, int childPosition) {
    ArrayList<Child> chList = groups.get(groupPosition).getItems();
    return chList.get(childPosition);

}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    Child child = (Child) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_item, null);

    }
    TextView tv = (TextView) convertView.findViewById(R.id.country_name);
    //trying to add this textview ltv
    TextView ltv = (TextView) convertView.findViewById(R.id.large_text);
    ImageView iv = (ImageView) convertView.findViewById(R.id.flag);
    tv.setText(child.getName());
    iv.setImageResource(child.getImage());
    //Set the large textview
    ltv.setText(child.getDetails());


    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    ArrayList<Child> chList = groups.get(groupPosition).getItems();
    return chList.size();
}

@Override
public Object getGroup(int groupPosition) {
    return groups.get(groupPosition);
}

@Override
public int getGroupCount() {
    return groups.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    Group group = (Group) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.group_item, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.group_name);
    tv.setText(group.getName());
    return convertView;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

}