我有一个可扩展的列表视图,用户可以单击一个按钮“保存”记录。但是,我的可扩展列表视图的列表视图似乎正确填充(标题不同)。但是,当用户“保存”元素1时,它还会保存元素4,7,10,... n + 3(我知道这一点,因为一旦保存视图就设置为不可见)。如果您看下面,我认为我正在使用getChildView()方法中的setTag()方法正确地回收元素。
我在getChildView()中添加了一些日志记录,以测试视图是否是第一次缓存或绘制的。绘制了前两个视图,其余的被缓存。
因此,如上所述,我的问题是,当有人与expandablelistview上的元素进行交互时,其他元素也会相应地更改。
这是我的适配器:
public class SurveyListAdapter extends BaseExpandableListAdapter {
private final SparseArray<Group> groups;
public LayoutInflater inflater;
private Context appContext;
public String TAG = "SurveyListAdapter";
private static SparseArray<SparseArray<Survey>> results = new SparseArray<SparseArray<Survey>>();
//private static SparseArray<ArrayList<Boolean>> rowSubmitted = new SparseArray<ArrayList<Boolean>>();
public SurveyListAdapter(Context context, SparseArray<Group> groups) {
this.appContext = context;
this.groups = groups;
for(int i = 0;i<groups.size();i++) {
Log.d(TAG, "Creating group: " + i);
results.put(i, new SparseArray<Survey>());
}
//inflater = context.getLayoutInflater();
this.inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
}
@Override
public Survey getChild(int groupPosition, int childPosition) {
//Log.d("getChild", "Group: " + groupPosition + "\tChild: " + childPosition);
return this.groups.get(groupPosition).children.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
//return Long.parseLong(this.groups.get(groupPosition).children.get(childPosition));
return childPosition;
}
public Survey getIdOfChild(int groupPosition, int childPosition) {
return this.groups.get(groupPosition).children.get(childPosition);
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View row = convertView;
SurveyViewHolder svh;
Survey child = getChild(groupPosition, childPosition);
if (row == null) {
row = inflater.inflate(R.layout.list_view_survey_row, parent, false);
svh = new SurveyViewHolder();
svh.title = (TextView) row.findViewById(R.id.tvSurveyTitle);
svh.rg = (RadioGroup) row.findViewById(R.id.radioShared);
svh.interest = (CheckBox) row.findViewById(R.id.checkBoxInterest);
svh.privacy = (CheckBox) row.findViewById(R.id.checkBoxPrivacy);
svh.rating = (RatingBar) row.findViewById(R.id.ratingBar);
svh.submit = (Button) row.findViewById(R.id.btnSubmitRow);
row.setTag(svh);
Log.d(TAG, "Built from scratch");
} else {
Log.d(TAG, "Cached");
svh = (SurveyViewHolder) row.getTag();
}
svh.title.setText(child.getTitle());
svh.children = child.getTitle();
svh.submit.setOnClickListener(new CustomOnClickListener(row, svh, groupPosition, childPosition));
//this.registerClickListeners(groupPosition, childPosition, convertView);
return row;
}
@Override
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).children.size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.xplist_view_survey_group, null);
}
Group group = (Group) getGroup(groupPosition);
((TextView) convertView).setText(group.string);
//((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public SparseArray<SparseArray<Survey>> getResults() {
return results;
}
public void emptyResults() {
for(int i=0; i<results.size();i++) {
for(int j=0;j<results.get(i).size();j++) {
results.get(i).remove(j);
}
}
}
public class SurveyViewHolder {
public TextView title;
public RadioGroup rg;
public RatingBar rating;
public CheckBox interest;
public CheckBox privacy;
public Button submit;
public String children;
}
public class CustomOnClickListener implements View.OnClickListener {
private View row;
private SurveyViewHolder svh;
private int group;
private int child;
public CustomOnClickListener(View r, SurveyViewHolder s, int g, int c) {
this.row = r;
this.svh = s;
this.group = g;
this.child = c;
}
@Override
public void onClick(View v) {
boolean addIt = false;
try{
if(results.get(this.group).get(this.child, null) == null) {
addIt = true;
}
} catch(NullPointerException e) {
Log.d(TAG, "NPE");
addIt = true;
}
if (addIt) {
if (svh.rg.getCheckedRadioButtonId() > 0 && svh.rating.getRating() > 0) {
Boolean[] reasons = new Boolean[]{false, false, false};
// Hide the view
this.row.setVisibility(View.INVISIBLE);
// Start saving the record
Survey s = new Survey(svh.title.getText().toString());
s.setRating(svh.rating.getRating());
if (svh.privacy.isChecked()) {
reasons[0] = true;
} else if (svh.interest.isChecked()) {
reasons[1] = true;
}
s.setReason(reasons);
results.get(this.group).put(this.child, s);
Toast.makeText(appContext, "Record Saved", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(appContext, "You must give a rating and shared status", Toast.LENGTH_SHORT).show();
}
} else {
Log.d(TAG, "Record already saved");
this.row.setVisibility(View.INVISIBLE);
}
}
}
}
谢谢您的帮助,我真的很难解决这个问题!
最佳答案
您将可见性设置为CustomOnClickListener
中的行,然后将此不可见的View
重新用于新项目。
假设您在第二项上按下了Save
。现在是不可见的。然后,如果滚动,则第二个项目的视图将被重新用于新项目。假设它是项目5。当然,您会看到第5个项目是隐藏的,因为它与第二个项目相同。
尝试在更新标题的同一位置更新View
中行的可见性。