Adapter 中的多个 SeekBar,仅最后一次 SeekBar 更新
Multiple SeekBars in Adapter, only last SeekBar updates
我有一个 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
将始终是 "last" SeekBar,而 rating
将始终是 "last" 评级 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));
}
}
我有一个 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
将始终是 "last" SeekBar,而 rating
将始终是 "last" 评级 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));
}
}