Kumar
Kumar

Reputation: 39

how to get update values from expandable listview

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

enter image description here

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


}

Upvotes: 1

Views: 1366

Answers (1)

Nicks
Nicks

Reputation: 16317

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.

Upvotes: 1

Related Questions