gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:58:19 27 4
gpt4 key购买 nike

当我从我的 Activity 类中单击确定按钮时,我从可扩展 ListView 中获取旧值,我有一个 Activity 类和一个适配器以及两个 getter setter 类,根据可扩展 ListView 的子级和父级,我想添加和子根据屏幕截图的数量,但我没有获得更新值,请帮助

enter image description here

这是我的 Activity 类代码

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

}

setter和getter

  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");

所以对于你的适配器的每个元素,每次 View 膨胀时,它都会被设置为 0。所以你需要做的不是它

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

2).第二个问题是,您正在更改 TextView 的值而不是适配器。为适配器中的每个元素调用 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,因为它是通过监听器内部类访问的。现在这应该可以工作了。

关于android - 如何从可扩展 ListView 中获取更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31849119/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com