我有一个ListView,每行都有一个SeekBar。列表的大小,因此SeekBars的数量会有所不同。
我已经设置了一个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));
}
答案 0 :(得分:1)
这显然会发生。
由于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));
}
}