当我从活动类中单击确定按钮时,我正在从可扩展列表视图获取旧值,我有一个活动类和一个适配器,以及每个可扩展列表视图的子级和父级的两个getter设置器类,我想按屏幕截图,但我没有获得更新值,请帮助

android - 如何从可扩展 ListView 获取更新值-LMLPHP

这是我的活动课程代码

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

}


这是我的适配器类

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

}


吸气剂和塞特

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


}

最佳答案

我可以看到代码中有2个问题-

1)在getChildView()方法中,该方法会为适配器中的每个元素触发。
你在做

tvnum.setText("0");


因此,对于适配器的每个元素,每当视图放大时,它都将设置为0。因此,您需要做的不是

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


2)。第二个问题是,您正在更改textviews的值,而不是适配器。适配器中的每个元素都会调用getChildView,因此您需要使用适配器而不是文本字段。因此您的代码应为:-

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


并记住将子级声明为final,因为它是通过侦听器内部类访问的。
现在,这应该工作。

10-08 02:51