具有在ExpandableListView具有多于一个的TextView麻烦 [英] Having trouble having more than one TextView in a ExpandableListView

查看:202
本文介绍了具有在ExpandableListView具有多于一个的TextView麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎遇到了麻烦,我expandablelist初始化另一个TextView的。我已经有一个,但我尝试添加在同一个孩子第二个。我做了的TextView(LTV)一个新的setter和getter,和我不断收到java.lang.ArrayIndexOutOfBoundsException:长度= 0;指数= 0的错误在我的logcat每当我试图用我的另外一个进行初始化。请在下面看到我的code,我错过什么?

编辑:大小为5,我有每个阵列20个元素的例子

MainActivity:

 公共类Brandspage延伸活动{    私人ExpandListAdapter ExpAdapter;
    私人ExpandableListView ExpandList;
    私人的ArrayList<组> ExpListItems;
    私人TextView的brandtv;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_brandspage);
        按钮B1 =(按钮)findViewById(R.id.button4);
        b1.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                意图I =新意图(Brandspage.this,MainActivity.class);
                startActivity(ⅰ);
            }
        });
        ExpandList =(ExpandableListView)findViewById(R.id.exp_list);
        ExpListItems = SetStandardGroups();
        ExpAdapter =新ExpandListAdapter(Brandspage.this,ExpListItems);
        ExpandList.setAdapter(ExpAdapter);
    }
    公众的ArrayList<组> SetStandardGroups(){
        字符串GROUP_NAMES [] = {A-G,H-N,O-U,V-Z
        };        串粗体[] = {1,2,3,4,5,
6,7,8,9,10,
11,12,13,14,15,16,
17,18,19,20
//这是我想要的LTV的TextView初始化数组        };
      字符串名称[] = {巴西,墨西哥,克罗地亚,喀
            荷兰,智,西班牙,澳大利亚,哥伦比亚
            希腊,希腊,智,智,智,智,哥伦比亚,哥伦比亚,哥伦比亚,哥伦比亚,哥伦比亚
        };
        INT图片[] = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
            R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
            R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
            R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
        };
        ArrayList的<组>名单=新的ArrayList<组>();
        ArrayList的<儿童> ch_list;
        INT大小= 5;
        INT J = 0;
        INT K = 0;
        对于(字符串GROUP_NAME:GROUP_NAMES){
            集团GRU =新集团();
            gru.setName(组名);
            ch_list =新的ArrayList<儿童>();                为(J = 0; J<大小; J ++){
                    儿童CH =新的儿童();
//这是不工作?
                    ch.setDetail(粗体[J]);
                    ch.setName(地名[J]);                    ch.setImage(图片[J]);
                    ch_list.add(CH);                }
                gru.setItems(ch_list);
                list.add(GRU);                大小= + 5;
            }
            返回列表;
        }
    }

ExpandablelistAdapter:

 公共类ExpandListAdapter扩展BaseExpandableListAdapter {    私人上下文的背景下;
    私人的ArrayList<组>组;    公共ExpandListAdapter(上下文的背景下,ArrayList的<组>组){
        this.context =背景;
        this.groups =组;
    }
    @覆盖
    公共对象getChild(INT groupPosition,诠释childPosition){
        ArrayList的<儿童> chList = groups.get(groupPosition).getItems();
        返回chList.get(childPosition);    }    @覆盖
    众长getChildId(INT groupPosition,诠释childPosition){
        返回childPosition;
    }    @覆盖
    公共查看getChildView(INT groupPosition,诠释childPosition,
                             布尔isLastChild,查看convertView,父母的ViewGroup){        孩童=(儿童)getChild(groupPosition,childPosition);        如果(convertView == NULL){
            LayoutInflater infalInflater =(LayoutInflater)上下文
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_item,NULL);
        }
        TextView的电视=(TextView中)convertView.findViewById(R.id.country_name);
        ImageView的IV =(ImageView的)convertView.findViewById(R.id.flag);
        TextView的按揭成数=(TextView中)convertView.findViewById(R.id.large);
        tv.setText(child.getName());
        iv.setImageResource(child.getImage());
        //试图做到这一点TextView的按揭成数
        ltv.setTypeface(NULL,Typeface.BOLD);
        ltv.setText(child.getDetail());
            返回convertView;
    }    @覆盖
    公众诠释getChildrenCount(INT groupPosition){
        ArrayList的<儿童> chList = groups.get(groupPosition).getItems();
        返回chList.size();
    }    @覆盖
    公共对象getGroup(INT groupPosition){
        返回groups.get(groupPosition);
    }    @覆盖
    公众诠释getGroupCount(){
        返回groups.size();
    }    @覆盖
    众长getGroupId(INT groupPosition){
        返回groupPosition;
    }    @覆盖
    公共查看getGroupView(INT groupPosition,布尔isExpanded,
                             查看convertView,父母的ViewGroup){
        群组=(集团)getGroup(groupPosition);
        如果(convertView == NULL){
            LayoutInflater INF =(LayoutInflater)上下文
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.group_item,NULL);
        }
        TextView的电视=(TextView中)convertView.findViewById(R.id.group_name);
        tv.setText(group.getName());
        返回convertView;
    }    @覆盖
    公共布尔hasStableIds(){
        返回true;
    }    @覆盖
    公共布尔isChildSelectable(INT groupPosition,诠释childPosition){返回false;
    }}

组类:

 进口的java.util.ArrayList;公共类组{    私人字符串名称;
    私人的ArrayList<儿童>项目;    公共字符串的getName(){
        返回名称;
    }    公共字符串getDetail(){
        返回名称;
    }    公共无效setDetail(字符串名称){
        this.Name =名称;
    }        公共无效setname可以(字符串名称){
            this.Name =名称;
        }        公众的ArrayList<儿童> getItems(){
            返回的项目;
        }        公共无效setItems(ArrayList的<儿童>产品){
            this.Items =项目;
        }    }


解决方案

您所有的数组大小不同,但你在同一个循环遍历他们。你的粗体[] 5 元素,名称[] 10 图片[] 8 ;但是,你正在使用的变量访问它们在你的循环Ĵ设置为迭代 20 时间(从 0 19 )。

这就是为什么你得到的ArrayIndexOutOfBounds 例外。他们都应该是相同的长度和匹配尺寸变量,让你初始化尺寸 <$ C $的数C>孩子的对象。



问题在于你的类。请注意如何既的setName() setDetail()正在保存的数据名称只类成员。因此,你看到你的数据的两个副本。

 公共类组{    私人字符串名称;
    私人字符串细节; //添加一个新成员详细信息    私人的ArrayList&LT;儿童&GT;项目;    公共字符串的getName(){
        返回名称;
    }    公共字符串getDetail(){
        返回细节; //改变吸气
    }    公共无效setDetail(字符串描述){
        this.Detail =细节; //改变二传手
    }    公共无效setname可以(字符串名称){
        this.Name =名称;
    }    ...
}

只需添加一个新的详细信息类成员和更新相应 getDetail() setDetail()方法,因为我之前所做的那样,以解决您的问题。

I seem to be having trouble initializing another textview in my expandablelist. I already have one, but I'm trying to add a second one in the same child. I made a new setter and getter for the textview(ltv),and I keep getting java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 error in my logcat whenever I try to initialize it with my other one. Please see my code below, what am I missing?

EDIT: size is 5 and I have 20 elements per array for an example.

MainActivity:

public class Brandspage extends Activity {

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


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


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

        String bold[] = {"1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10", 
"11", "12", "13", "14", "15","16",
"17","18","19","20"
//This is the array I'm trying to initialize with the ltv textview 





        };


      String names[] = { "Brazil", "Mexico", "Croatia", "Cameroon",
            "Netherlands", "chile", "Spain", "Australia", "Colombia",
            "Greece", "Greece", "chile", "chile", "chile", "chile","Colombia","Colombia","Colombia","Colombia","Colombia"




        };


        int Images[] = {R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher,R.drawable.ic_launcher,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 = 5;
        int j = 0;
        int k = 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();
//this is not working?
                    ch.setDetail(bold[j]);
                    ch.setName(names[j]);



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



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

                size = size + 5;


            }
            return list;
        }
    }

ExpandablelistAdapter:

    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);
        ImageView iv = (ImageView) convertView.findViewById(R.id.flag);
        TextView ltv = (TextView) convertView.findViewById(R.id.large);
        tv.setText(child.getName());
        iv.setImageResource(child.getImage());
        //Trying to do this textview ltv
        ltv.setTypeface(null, Typeface.BOLD);
        ltv.setText(child.getDetail());




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


    }

}

Group Class:

import java.util.ArrayList;

public class Group {

    private String Name;
    private ArrayList<Child> Items;

    public String getName() {
        return Name;
    }

    public String getDetail() {
        return Name;
    }

    public void setDetail(String name) {
        this.Name = name;
    }

        public void setName(String name) {
            this.Name = name;
        }

        public ArrayList<Child> getItems() {
            return Items;
        }

        public void setItems(ArrayList<Child> Items) {
            this.Items = Items;
        }

    }

解决方案

All of your arrays are of different sizes but you're iterating over them in the same for loop. Your bold[] has 5 elements, names[] has 10, Images[] only 8; but, you're accessing them in your for loop using the variable j set to iterate 20 times (from 0 to 19).

That's why you're getting the ArrayIndexOutOfBounds exception. They should all be of the same length and match your size variable to let you initialize size number of Child objects.


The problem lies with your Group class. Notice how both setName() and setDetail() are saving the data to Name class member only. Hence, you are seeing two copies of your data.

public class Group {

    private String Name;
    private String Detail; // Add a new Detail member

    private ArrayList<Child> Items;

    public String getName() {
        return Name;
    }

    public String getDetail() {
        return Detail; // change getter
    }

    public void setDetail(String Detail) {
        this.Detail = Detail; // change setter
    }

    public void setName(String name) {
        this.Name = name;
    }

    ...
}

Just add a new Detail class member and update the corresponding getDetail() and setDetail() methods as I've done above to fix your problem.

这篇关于具有在ExpandableListView具有多于一个的TextView麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆