如何从可扩展列表视图中获取更新值
how to get update values from expandable listview
当我从 activity class 单击确定按钮时,我从可扩展列表视图中获取旧值,我有一个 activity class 和一个适配器和两个 getter setter class 根据可扩展列表视图的子项和父项,我想根据屏幕截图添加和子数量,但我没有获得更新值,请帮助
这是我的 activity class 代码
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;
}
}
这是我的适配器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 和 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;
}
}
我可以看到代码中有 2 个问题 --
1) 它在 getChildView() 方法中,该方法会针对适配器中的每个元素触发。
你在做
tvnum.setText("0");
所以对于你的适配器的每个元素,每次视图膨胀时,它都会被设置为 0。所以你需要做的不是它
tvnum.setText(String.valueOf(child.getId()));// or whatever field you have
2).第二个问题是,您正在更改文本视图的值而不是适配器。为适配器中的每个元素调用 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();
}
});
并记住将 child 声明为最终的,因为它是通过侦听器内部类访问的。
现在应该可以了。
当我从 activity class 单击确定按钮时,我从可扩展列表视图中获取旧值,我有一个 activity class 和一个适配器和两个 getter setter class 根据可扩展列表视图的子项和父项,我想根据屏幕截图添加和子数量,但我没有获得更新值,请帮助
这是我的 activity class 代码
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;
}
}
这是我的适配器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 和 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;
}
}
我可以看到代码中有 2 个问题 --
1) 它在 getChildView() 方法中,该方法会针对适配器中的每个元素触发。 你在做
tvnum.setText("0");
所以对于你的适配器的每个元素,每次视图膨胀时,它都会被设置为 0。所以你需要做的不是它
tvnum.setText(String.valueOf(child.getId()));// or whatever field you have
2).第二个问题是,您正在更改文本视图的值而不是适配器。为适配器中的每个元素调用 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();
}
});
并记住将 child 声明为最终的,因为它是通过侦听器内部类访问的。 现在应该可以了。