如何从扩展列表视图更新值 [英] how to get update values from expandable listview

查看:294
本文介绍了如何从扩展列表视图更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到从扩展列表视图旧值当我从我的活动类单击确定按钮,我有一个活动类和一个适配器和两个吸气二传手类每名儿童和可扩展列表视图的父母,我想要做的添加和子数量根据屏幕截图,但即时通讯没有得到更新值,请帮助

在这里输入的形象描述

这是我的活动课code

 公共类ExpandMainActivity延伸活动{    私有静态最后的ArrayList< ExpndChild>数据= NULL;
    ExpandableListView expList;
    ExpndListview expndAdapter;
    ArrayList的< ExpndParent> expndarrayParent;
    ExpndDatabase exdb;
    ExpndChild exchild;
    ArrayList的< ExpndChild> exchld;
    ExpndParent exparent;
    Button按钮;    的String [] =状态{快餐,软饮料};    串1,名称2,NAME3;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.expan_spinner);        exdb =新ExpndDatabase(本​​); //数据库
        Toast.makeText(这一点,数据库被称为,Toast.LENGTH_SHORT).show();        exdb.addContactall(1,比萨);在数据库// addContactall方法
        exdb.addContactall(1,汉堡);        exdb.addContactall(2,古柯);
        exdb.addContactall(2,百事);        //父名         的for(int i = 0; I< state.length;我++){             exparent =新ExpndParent(州[I]);
             exdb.addContactParent(exparent); //数据库addContactParent方法            }         NAME1 = exdb.getContactparent(1);
         名称2 = exdb.getContactparent(2);        Toast.makeText(这一点,增加caontact,Toast.LENGTH_SHORT).show();        expList =(ExpandableListView)findViewById(R.id.myList);
        expndarrayParent =调用setParent();
        expndAdapter =新ExpndListview(ExpandMainActivity.this,expndarrayParent);        expList.setAdapter(expndAdapter);        Toast.makeText(getApplicationContext()中,可膨胀,Toast.LENGTH_SHORT).show();        按钮=(按钮)findViewById(R.id.button);        button.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根                的for(int i = 0; I< expndAdapter.getGroupCount();我++)
                {                    为(中间体J = 0; J&下; expndAdapter.getChildrenCount(ⅰ); J ++){                        查看列表视图= expndAdapter.getChildView(I,J,假,空,expList);
                        TextView的tvChild =(TextView中)listview.findViewById(R.id.tvChild);
                        TextView的tvnum =(TextView中)listview.findViewById(R.id.tvnum);                        Toast.makeText(ExpandMainActivity.this,+ tvChild.getText()的toString()+ tvnum.getText()的toString(),Toast.LENGTH_SHORT。).show();                    }
                }
            }
        });    }    私人的ArrayList< ExpndParent>的setParent(){        ArrayList的< ExpndParent> listParent =新的ArrayList< ExpndParent>();
        ArrayList的< ExpndChild> listChild =新的ArrayList< ExpndChild>();        / * ************ ************ * /
        //父集名称
        ExpndParent exParent1 =新ExpndParent();        exParent1.setName(名称1);        listChild = exdb.getAllContactssatic(1);        exParent1.setItems(listChild);        listChild =新的ArrayList< ExpndChild>();
    / * ************ **************** * /
                      ExpndParent exParent2 =新ExpndParent();        exParent2.setName(名2);                      listChild = exdb.getAllContactssatic(2);        exParent2.setItems(listChild);        listChild =新的ArrayList< ExpndChild>();    / * ************ **************** * /         listParent.add(exParent1);
         listParent.add(exParent2);
         返回listParent;
    }}

这是我的适配器类

 公共类ExpndListview扩展BaseExpandableListAdapter {    上下文语境;
    ArrayList的< ExpndParent> exParent;
    ArrayList的< ExpndChild> exchild;    公共ExpndListview(上下文的背景下,ArrayList的< ExpndParent> exParent){        this.context =背景;
        this.exParent = exParent;
        Toast.makeText(上下文中,在适配器,Toast.LENGTH_SHORT).show();
    }    @覆盖
    公共对象getChild(INT groupPosition,诠释childPosition){        ArrayList的< ExpndChild> chList = exParent.get(groupPosition).getItems();
        返回chList.get(childPosition);
    }    @覆盖
    众长getChildId(INT groupPosition,诠释childPosition){
        返回childPosition;
    }    @覆盖
    公共查看getChildView(INT groupPosition,最终诠释childPosition,
            布尔isLastChild,查看convertView,父母的ViewGroup){    最终的VI = convertView;        ExpndChild孩子=(ExpndChild)getChild(groupPosition,childPosition);
        如果(convertView == NULL){            LayoutInflater infalInflater =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.expand_child,NULL);
        }        TextView的tvChild =(TextView中)convertView.findViewById(R.id.tvChild);
        TextView的tvsub =(TextView中)convertView.findViewById(R.id.tvsub);
        最终的TextView tvnum =(TextView中)convertView.findViewById(R.id.tvnum);
        TextView的tvadd =(TextView中)convertView.findViewById(R.id.tvadd);        tvnum.setText(0);        (child.getname()的toString())tvChild.setText;        tvsub.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){            INT I =的Integer.parseInt(tvnum.getText()的toString());
            一世 - ;
            tvnum.setText(+ I);            }
        });        tvadd.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                // notifyDataSetChanged();
                INT I =的Integer.parseInt(tvnum.getText()的toString());
                我++;
                tvnum.setText(+ I);
            }
        });        返回convertView;
    }
    @覆盖
    公众诠释getChildrenCount(INT groupPosition){        ArrayList的< ExpndChild> chList = exParent.get(groupPosition).getItems();
        返回chList.size();
    }    @覆盖
    公共对象getGroup(INT groupPosition){        返回exParent.get(groupPosition);
    }    @覆盖
    公众诠释getGroupCount(){        返回exParent.size();
    }    @覆盖
    众长getGroupId(INT groupPosition){        返回groupPosition;
    }    @覆盖
    公共查看getGroupView(INT groupPosition,布尔isExpanded,
            查看convertView,父母的ViewGroup){        ExpndParent parentgroup =(ExpndParent)getGroup(groupPosition);
        如果(convertView == NULL){            LayoutInflater INF =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.expand_parent,NULL);
        }        TextView的tvGroup =(TextView中)convertView.findViewById(R.id.tvGroup);
        tvGroup.setText(parentgroup.getName());        返回convertView;
    }    @覆盖
    公共布尔hasStableIds(){        返回true;
    }    @覆盖
    公共布尔isChildSelectable(INT groupPosition,诠释childPosition){        返回true;
    }}

getter和setter

  ExpndChild公共类ExpndChild {    INT ID;
    字符串名称;
    INT idstatic;    公共ExpndChild(){    }    公共ExpndChild(INT ID,INT idstatic,字符串名称){
        this.id = ID;
        this.idstatic = idstatic;
        this.name =名称;    }    公共ExpndChild(INT ID,字符串名称){        this.id = ID;
        this.name =名称;    }    公共ExpndChild(字符串名称){
        this.name =名称;    }
    //获取ID
    公众诠释的getID(){
        返回this.id;
    }    公共无效SETID(INT ID){
        this.id = ID;
    }    公共字符串的getName(){
        返回this.name;
    }    公共无效setname可以(字符串名称){
        this.name =名称;
    }    公众诠释getidstatic(){
        返回this.idstatic;
    }    公共无效setidstatic(INT idstatic){
        this.idstatic = idstatic;
    }}
ExpndParent.java公共类ExpndParent {
        私人字符串名称;        私人的ArrayList< ExpndChild>项目;
        公共ExpndParent(字符串名称){
            // TODO自动生成构造函数存根            this.Name =名称;
        }        公共ExpndParent(){
            // TODO自动生成构造函数存根
        }        公共字符串的getName(){            返回名称;
        }        公共无效setname可以(字符串名称){            this.Name =名称;
        }        公众的ArrayList< ExpndChild> getItems(){            返回的项目;
        }        公共无效setItems(ArrayList的< ExpndChild>产品){            this.Items =项目;
        }
}


解决方案

我可以看到有2 issuesin的code -

1),其在getChildView()方法,此方法在适配器的每个元素被解雇。
你正在做的。

  tvnum.setText(0);

让您的适配器的每一个元素,每一次的观点充气,它将被设置为0。因此,而不是它,你需要做的。

  tvnum.setText(将String.valueOf(child.getId())); //或任何领域你有

2)。第二个问题是,你正在改变textviews的价值观,而不是适配器。该getChildView被要求在适配器的每个元素,所以你需要使用适配器,而不是文本框的发挥。所以,你的code应该是: -

  tvsub.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                INT I = child.getId();
// INT I =的Integer.parseInt(tvnum.getText()的toString());
                一世 - ;
// tvnum.setText(+ I);
                child.setId(ⅰ);
                notifyDataSetChanged();
            }
        });

和记宣布孩子一样最终,因为它是通过监听内部类访问。
现在,这应该工作。

I am getting old values from expandable listview when i click ok button from my activity class, i have one activity class and one adapter and two getter setter class as per child and parent of expandable listview , i want to do add and sub qty as per screen shot but i m not getting update value please help

this is my activity class code

public class ExpandMainActivity extends Activity {

    private static final ArrayList<ExpndChild> data = null;
    ExpandableListView expList;
    ExpndListview expndAdapter;
    ArrayList<ExpndParent> expndarrayParent;
    ExpndDatabase exdb;
    ExpndChild exchild;
    ArrayList<ExpndChild> exchld; 
    ExpndParent exparent;
    Button button;

    String[] state={"FastFood","SoftDrinks"};

    String name1,name2,name3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expan_spinner);

        exdb=new ExpndDatabase(this);   // database
        Toast.makeText(this, "database called", Toast.LENGTH_SHORT).show();

        exdb.addContactall(1,"pizza");  // addContactall method in database
        exdb.addContactall(1,"burger");

        exdb.addContactall(2,"coca");
        exdb.addContactall(2,"pepsi");

        // parent name

         for(int i=0;i<state.length;i++){

             exparent=new ExpndParent(state[i]);
             exdb.addContactParent(exparent);      // addContactParent method in database

            }

         name1=exdb.getContactparent(1);
         name2=exdb.getContactparent(2);    

        Toast.makeText(this, "add caontact", Toast.LENGTH_SHORT).show();

        expList=(ExpandableListView)findViewById(R.id.myList);
        expndarrayParent=setParent();
        expndAdapter=new ExpndListview(ExpandMainActivity.this,expndarrayParent);

        expList.setAdapter(expndAdapter);

        Toast.makeText(getApplicationContext(), "in the expandable", Toast.LENGTH_SHORT).show();

        button=(Button)findViewById(R.id.button);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                for(int i = 0; i <expndAdapter.getGroupCount();i++)
                {

                    for (int j = 0; j < expndAdapter.getChildrenCount(i); j++) {

                        View listview = expndAdapter.getChildView(i, j, false, null, expList);
                        TextView tvChild = (TextView)listview.findViewById(R.id.tvChild);
                        TextView tvnum=(TextView)listview.findViewById(R.id.tvnum);

                        Toast.makeText(ExpandMainActivity.this,""+ tvChild.getText().toString()+tvnum.getText().toString(),Toast.LENGTH_SHORT).show();

                    }


                }


            }
        });

    }



    private ArrayList<ExpndParent> setParent() {

        ArrayList<ExpndParent> listParent=new ArrayList<ExpndParent>();
        ArrayList<ExpndChild> listChild=new ArrayList<ExpndChild>();

        /* ************************************************************   */
        // in the parent set name 
        ExpndParent exParent1=new ExpndParent();

        exParent1.setName(name1);

        listChild=exdb.getAllContactssatic(1);

        exParent1.setItems(listChild);

        listChild=new ArrayList<ExpndChild>();
    /* ****************************************************************  */ 
                      ExpndParent exParent2=new ExpndParent();

        exParent2.setName(name2);

                      listChild=exdb.getAllContactssatic(2);

        exParent2.setItems(listChild);

        listChild=new ArrayList<ExpndChild>();

    /* ****************************************************************  */ 

         listParent.add(exParent1);
         listParent.add(exParent2); 
         return listParent;
    }

}

this is my adapter class

public class ExpndListview extends BaseExpandableListAdapter{

    Context context;
    ArrayList<ExpndParent> exParent;
    ArrayList<ExpndChild> exchild;

    public ExpndListview(Context context,ArrayList<ExpndParent> exParent) {

        this.context=context;
        this.exParent=exParent;


        Toast.makeText(context, "in the Adapter", Toast.LENGTH_SHORT).show();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {

        ArrayList<ExpndChild> chList=exParent.get(groupPosition).getItems();
        return chList.get(childPosition);
    }

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

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

    final   View vi=convertView;

        ExpndChild child=(ExpndChild)getChild(groupPosition, childPosition);
        if(convertView==null){

            LayoutInflater infalInflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView=infalInflater.inflate(R.layout.expand_child, null);
        }

        TextView tvChild=(TextView)convertView.findViewById(R.id.tvChild);
        TextView tvsub=(TextView)convertView.findViewById(R.id.tvsub);
        final TextView tvnum=(TextView)convertView.findViewById(R.id.tvnum);
        TextView tvadd=(TextView)convertView.findViewById(R.id.tvadd);

        tvnum.setText("0");

        tvChild.setText(child.getname().toString());

        tvsub.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            int i=Integer.parseInt(tvnum.getText().toString());
            i--;
            tvnum.setText(""+i);

            }
        });

        tvadd.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //notifyDataSetChanged();
                int i=Integer.parseInt(tvnum.getText().toString());
                i++;
                tvnum.setText(""+i);


            }
        });

        return convertView;
    }


    @Override
    public int getChildrenCount(int groupPosition) {

        ArrayList<ExpndChild> chList=exParent.get(groupPosition).getItems();
        return chList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {

        return exParent.get(groupPosition);
    }

    @Override
    public int getGroupCount() {

        return exParent.size();
    }

    @Override
    public long getGroupId(int groupPosition) {

        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {

        ExpndParent parentgroup=(ExpndParent)getGroup(groupPosition);
        if(convertView==null){

            LayoutInflater inf=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView=inf.inflate(R.layout.expand_parent, null);
        }

        TextView tvGroup=(TextView)convertView.findViewById(R.id.tvGroup);
        tvGroup.setText(parentgroup.getName());

        return convertView;
    }

    @Override
    public boolean hasStableIds() {

        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {

        return true;
    }

}

getter and setter

  ExpndChild

public class ExpndChild  {

    int id;
    String name;
    int idstatic;

    public ExpndChild(){

    }

    public ExpndChild(int id,int idstatic, String name){
        this.id = id;
        this.idstatic=idstatic;
        this.name = name;

    }

    public ExpndChild(int id, String name){

        this.id = id;
        this.name = name;

    }

    public ExpndChild(String name){
        this.name = name;

    }
    // getting ID
    public int getID(){
        return this.id;
    }

    public void setID(int id){
        this.id = id;
    }

    public String getname(){
        return this.name;
    }

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

    public int getidstatic(){
        return this.idstatic;
    }

    public void setidstatic(int idstatic){
        this.idstatic = idstatic;
    }

}


ExpndParent.java

public class ExpndParent {


        private String Name;

        private ArrayList<ExpndChild> Items;


        public ExpndParent(String name) {
            // TODO Auto-generated constructor stub

            this.Name=name;
        }

        public ExpndParent() {
            // TODO Auto-generated constructor stub
        }

        public String getName() {

            return Name;
        }                     

        public void setName(String name) {

            this.Name = name;
        }

        public ArrayList<ExpndChild> getItems() {

            return Items;
        }

        public void setItems(ArrayList<ExpndChild> Items) {

            this.Items = Items;
        }


}

解决方案

I can see there are 2 issuesin the code --

1) Its in getChildView() method, which gets fired for each element in adapter. You are doing

tvnum.setText("0");

so for every element of your adapter, every time the view is inflated, it will be set to 0. So instead of it you need to do

tvnum.setText(String.valueOf(child.getId()));// or whatever field you have

2). The second issue is, you are changing the values of textviews and not the adapter. The getChildView is called for each element in adapter, so you need to play with the adapter, not the textfields. So your code should be:--

     tvsub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i=child.getId();
//                int i=Integer.parseInt(tvnum.getText().toString());
                i--;
//                tvnum.setText(""+i);
                child.setId(i);
                notifyDataSetChanged();
            }
        });

and remember to declare the child as final, as it is accessed through the listener innerclass. Now This should work.

这篇关于如何从扩展列表视图更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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