【问题标题】:How to add multiple Textviews in a ExpandableListView如何在 ExpandableListView 中添加多个 Textview
【发布时间】:2015-01-04 16:56:44
【问题描述】:

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

还可以在我的代码中查看几个 cmets,看看我想要做什么。

可扩展列表适配器:

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;


                    }


                }

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

【问题讨论】:

    标签: java android textview expandablelistview


    【解决方案1】:

    您需要编辑您的 Child 类以再添加一个字段,例如 Detail 和该字段的 getter setter 方法。然后在

    中设置该字段
     public ArrayList<Group> SetStandardGroups()
    

    您可以在 ExpandListAdapter 中获取字段值,然后您可以设置为第二个文本视图。 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() {
    

    //这里如何用其他字符串初始化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 类是

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

    }

    【讨论】:

    • 好的,我在 SetStandardGroups 中添加了另一个字符串数组(将此数组命名为 large),当我尝试将其添加到 ch.setDetail(large[j]);我收到一个错误,应用程序关闭。你知道为什么会这样吗?
    • 检查 logcat。也许你得到一个 indexOutOfBound 异常。检查您正在使用的数组的大小和索引。
    • Array large[j] 应该有更多的元素。数组 large[j] 中的元素数应等于或大于 'size'。
    • 抱歉回答太多。我无法解释更多。请先了解编程的基础知识。
    • 这对我有帮助,但这个问题对我的帮助更大:stackoverflow.com/questions/28015793/…
    猜你喜欢
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多