简体   繁体   English

如何在ExpandableListView中添加多个Textviews

[英]How to add multiple Textviews in a ExpandableListView

I can't figure out how to add more than one Textview in my Expandablelist adapter and MainActivty. 我不知道如何在我的Expandablelist适配器和MainActivty中添加多个Textview。 When ever I tried to add a new Textview in the Adapter, I then can't figure out how to initialize the Textview in the MainAcivity. 每当我尝试在适配器中添加新的Textview时,便无法弄清楚如何在MainAcivity中初始化Textview。 It just copies the text that I put in the first Textview into the new one. 它只是将我在第一个Textview中放入的文本复制到新文本中。 I found some other Stackoverflow posts that regarded this but none of the them has the answer. 我发现其他一些Stackoverflow帖子也对此有所考虑,但都没有答案。 If someone has any idea how to do this, I'd appreciate it. 如果有人知道如何执行此操作,我将不胜感激。 Please put a example as I'm a beginner with this. 请举一个例子,因为我是初学者。 I followed a tutorial from this http://www.tutorialsbuzz.com/2014/07/custom-expandable-listview-image-text.html 我遵循了来自http://www.tutorialsbuzz.com/2014/07/custom-expandable-listview-image-text.html的教程

Also see a couple comments in my code to see what I want done. 在我的代码中还可以看到一些注释,以查看我想要做什么。

ExpandablelistAdapter: ExpandablelistAdapter:

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;
            }

        }

MainActivity: 主要活动:

    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 子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>

You need to edit your Child class to add one more field say Detail and the getter setter methods for this field.Then set this field in the 您需要编辑Child类以添加另一个字段,例如Detail和该字段的getter setter方法。然后在

 public ArrayList<Group> SetStandardGroups()

You can get the field value in your ExpandListAdapter and then you can set to the second text view. 您可以在ExpandListAdapter中获取字段值,然后可以设置为第二个文本视图。 BrandPage activity class is BrandPage活动类别为

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() {

//how to initialize the ltv textview here with the other strings //如何在这里用其他字符串初始化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;
}

} }

Your ExpandableAdapter class is 您的ExpandableAdapter类是

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;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM