我有一个listview支持自定义arrayadapterI写的。每个listview项都包含textview和seekbar。这背后的想法是用户应该能够移动每个搜索栏,并且应用程序存储该搜索栏的值(因此如果用户退出片段,则可以将搜索栏恢复到其正确的值)。这些值存储在一个hashmap中,其中key是权限的名称(AKA是元素的textview)
我的问题是:listview包含45个元素,但是,在第8个元素之后,我的适配器似乎认为listview回到了起点。例如:如果我移动第9个元素的搜索条然后继续向上滚动,则第1个元素将具有我给第9个元素的值。列表视图本身确实填充正确(或者似乎无论如何),因为每个元素的文本视图都不同。
这是我的自定义arrayadapter:
public class CompetenceJudgementListAdapter extends ArrayAdapter<String[]> {
private Context mContext;
private ArrayList mData;
int mLayoutResourceId;
ArrayList<CompetenceItem> mCompetenceItemList;
HashMap<String, Integer> valueList;
public CompetenceJudgementListAdapter(Context context, int resource, ArrayList<String[]> data) {
super(context, resource, data);
mContext = context;
this.mLayoutResourceId = resource;
mData = data;
mCompetenceItemList = new ArrayList<CompetenceItem>();
valueList = new HashMap<String, Integer>();
}
public ArrayList<String[]> getCompetenceItemData(){
ArrayList<String[]> list = new ArrayList<String[]>();
for(CompetenceItem item : mCompetenceItemList){
String[] data = new String[3];
data[0] = (String) item.title.getText();
if(valueList.get(item.competence_id) != null){
data[1] = String.valueOf(valueList.get(item.competence_id).toString());
}
data[2] = item.competence_id;
list.add(data);
}
return list;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String[] getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
View row = convertView;
CompetenceItem competenceItem = null;
String[] measurementData = (String[]) mData.get(position);
String c_title = measurementData[0];
String c_id = measurementData[2];
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(mLayoutResourceId, parent, false);
competenceItem = new CompetenceItem();
mCompetenceItemList.add(competenceItem);
competenceItem.title = (TextView) row.findViewById(R.id.textview_header_competence_name);
competenceItem.seekbar = (SeekBar) row.findViewById(R.id.seekbar_competence);
competenceItem.seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
Integer newProgress = 0;
String competenceId;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
private SeekBar.OnSeekBarChangeListener init(String var) {
competenceId = var;
return this;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
newProgress = seekBar.getProgress();
valueList.put(competenceId, newProgress);
}
}.init(c_title));
row.setTag(competenceItem);
} else {
competenceItem = (CompetenceItem) row.getTag();
}
competenceItem.title.setText(measurementData[0]);
competenceItem.competence_id = measurementData[2];
int max = SharedData.COMPETENCE_JUDGEMENT_MAX_VALUE.getIntValue();
competenceItem.seekbar.setMax(max);
if(measurementData[1] != null){
String competenceValue = measurementData[1];
competenceItem.seekbar.setProgress(Integer.parseInt(competenceValue));
}else{
competenceItem.seekbar.setProgress(0);
}
if((valueList.get(competenceItem.title.getText() )!= null)){
competenceItem.seekbar.setProgress(valueList.get(competenceItem.title.getText()));
}
return row;
}
static class CompetenceItem{
TextView title;
SeekBar seekbar;
String competence_id;
}
答案 0 :(得分:2)
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(mLayoutResourceId, parent, false);
competenceItem = new CompetenceItem();
mCompetenceItemList.add(competenceItem);
competenceItem.title = (TextView) row.findViewById(R.id.textview_header_competence_name);
competenceItem.seekbar = (SeekBar) row.findViewById(R.id.seekbar_competence);
row.setTag(competenceItem);
} else {
competenceItem = (CompetenceItem) row.getTag();
}
competenceItem.seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
Integer newProgress = 0;
String competenceId;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
private SeekBar.OnSeekBarChangeListener init(String var) {
competenceId = var;
return this;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
newProgress = seekBar.getProgress();
valueList.put(competenceId, newProgress);
}
}.init(c_title));
我觉得这应该可行。因为这个监听器对于循环视图是相同的。
答案 1 :(得分:0)
由回收您的观点引起的
if (row == null) {
//******
}else {
competenceItem = (CompetenceItem) row.getTag(); //here you are doing the recycle
}
// here you must reset your seekbar