我正在使用ExpandableListView
并且我有一个adapter
。现在,在点击子广告时,我会看到一个dialog
框,其中包含EditText
字段,而EditText
中输入的值应该位于TextView
字段内孩子点击了。这似乎有效,但是当我展开ExpListView
的另一个标题时,该值会进入其他一些孩子的TextViews
。我该如何解决这个问题?
这是我的代码:
ExpandableListAdapter:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<Artikl>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<Artikl>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@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 Artikl child = (Artikl) getChild(groupPosition, childPosition);
final String childText = child.getNaziv();
final String cenaArtikl = Integer.toString(child.getCena());
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.from_name);
TextView txtCena = (TextView) convertView
.findViewById(R.id.cena_view);
txtCena.setText(cenaArtikl);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override public boolean areAllItemsEnabled(){
return true;
}
}
ExpListView子项的OnChildClickListener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(Order.this);
final EditText input = new EditText(Naracka.this);
final int GP = groupPosition;
final View vi = v;
final int CP=childPosition;
input.setInputType(InputType.TYPE_CLASS_NUMBER);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
builder.setMessage( listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition).getNaziv())
.setCancelable(false)
.setView(input)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
TextView text=(TextView)vi.findViewById(R.id.editText);
text.setText(input.getText().toString());
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle("Enter amount");
alert.show();
return false;
}
});
编辑:这是Artikl模型代码,它只是一个带有getter和setter的模型:
public class Artikl {
private String sifra, naziv;
private int cena;
public Artikl() {
}
public Artikl(String sifra, String naziv, int cena
) {
this.naziv = naziv;
this.sifra = sifra;
this.cena = cena;
}
public String getNaziv() {
return naziv;
}
public void setNaziv(String name) {
this.naziv = name;
}
public String getSifra() {
return sifra;
}
public void setSifra(String sifra) {
this.sifra = sifra;
}
public int getCena() {
return cena;
}
public void setCena(int cena) {
this.cena = cena;
}
}
以下是截图(抱歉该应用不是英文) So, I enter the amount on the second child here,然后当我展开第一个标题时,我得到它Here too