我有一个ListView每行都有一个SeekBar。列表的大小,因此SeekBar的数量会有所不同。
我已经设置了一个SeekBar.OnSeekBarChangeListener,并从中基于SeekBar所显示的整数更新了一个TextView。
问题在于,列表中仅最后一个SeekBar似乎是从SeekBar.OnSeekBarChangeListener更新的。
如何使所有SeekBars正常工作?
提前致谢。
private class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<?> list;
public MySimpleArrayAdapter(Context context, ArrayList<?> list) {
super(context, R.layout.monitoringrowlayout);
Log.e(TAG, "inside adapter constructor");
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.monitoringrowlayout, parent, false);
final int pos = position;
Log.e(TAG, "list size in adapter = " + list.size());
String record = list.get(position).toString();
rowView.setTag(record);
String[] itemsInRecord = record.split(",");
Log.e(TAG, "itemin record = " + itemsInRecord.length);
String[] recordItem = new String[itemsInRecord.length];
for (int x = 0; x < itemsInRecord.length; x++) {
recordItem[x] = itemsInRecord[x];
Log.e(TAG, "recordItem[x]" + x + " " +recordItem[x] );
}
recordItem[0] = recordItem[0].replace('[', ' ').trim();
recordItem[3] = recordItem[3].replace(']', ' ').trim();
TextView description = (TextView) rowView.findViewById(R.id.rowmonitoringdescription);
description.setText(recordItem[1]);
rating = (TextView) rowView.findViewById(R.id.rowmonitoringrating);
sb = (SeekBar)rowView.findViewById(R.id.seekbarmonitoring);
sb.setOnSeekBarChangeListener(customSeekBarListener);
return rowView;
}
@Override
public int getCount() {
return this.list.size();
}
}// end of adapter class
private SeekBar.OnSeekBarChangeListener customSeekBarListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
upDateTextBox(progress);
}
};
private void upDateTextBox(int progress){
rating.setText(String.valueOf(progress));
}
最佳答案
显然这将会发生。
由于sb
始终是“最后一个” SeekBar,而rating
始终是“最后一个”评级的TextView。
更改您的代码:
private class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<?> list;
public MySimpleArrayAdapter(Context context, ArrayList<?> list) {
super(context, R.layout.monitoringrowlayout);
Log.e(TAG, "inside adapter constructor");
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.monitoringrowlayout, parent, false);
final int pos = position;
Log.e(TAG, "list size in adapter = " + list.size());
String record = list.get(position).toString();
rowView.setTag(record);
String[] itemsInRecord = record.split(",");
Log.e(TAG, "itemin record = " + itemsInRecord.length);
String[] recordItem = new String[itemsInRecord.length];
for (int x = 0; x < itemsInRecord.length; x++) {
recordItem[x] = itemsInRecord[x];
Log.e(TAG, "recordItem[x]" + x + " " +recordItem[x] );
}
recordItem[0] = recordItem[0].replace('[', ' ').trim();
recordItem[3] = recordItem[3].replace(']', ' ').trim();
TextView description = (TextView) rowView.findViewById(R.id.rowmonitoringdescription);
description.setText(recordItem[1]);
SeekBar sb = (SeekBar)rowView.findViewById(R.id.seekbarmonitoring);
sb.setOnSeekBarChangeListener(customSeekBarListener);
sb.setTag(rowView.findViewById(R.id.rowmonitoringrating));
return rowView;
}
@Override
public int getCount() {
return this.list.size();
}
}// end of adapter class
private SeekBar.OnSeekBarChangeListener customSeekBarListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
upDateTextBox((TextView) seekBar.getTag(), progress);
}
};
private void upDateTextBox(TextView tv, int progress){
tv.setText(String.valueOf(progress));
}
}