I am using custom SimpleCursorAdapter
to include a SeekBar
in a ListView
. But I can't seem to link the progress value to display in the TextView
. Here is my sample code.
class TmpSimpleCursorAdapter extends SimpleCursorAdapter {
public TmpSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.row_item_temperature, parent, false);
return view;
}
@Override
public void bindView(View view, Context Context, final Cursor cursor) {
//Room name, displaying in ListView Item
String title = cursor.getString(cursor.getColumnIndex(DBTemperature.COLUMN_TITLE));
//Set temperature value, adjusting by SeekBar
temperature = (TextView) view.findViewById(R.id.roomTemperature);
SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
temperature.setText("+" + progressValue);
Log.d(MEGA, "Progress Changed");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.d(MEGA, "Progress Started");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.d(MEGA, "Progress Stopped");
}
});
}
}
The logs even shows that it works properly, but values from the SeekBar
are not transmitting to the TextView
. Should I use ViewBinder
for this? Or is there something missing in my code?
======================================================================
Next stage - using ViewBinder I've changed the way to set ListView Rows, but there is still problem with SeekBar.
public class FragmentTemperatureDB extends Fragment implements LoaderCallbacks {
ListView lvTemperature;
DBTemperature dbHelperTmp;
SimpleCursorAdapter dataAdapterTmp;
int LIST_TMP_ID = 0;
ActionMode tmpActionMode;
private int operatePosition = 0;
final static String MEGADOR = "megadr";
public FragmentTemperatureDB() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vTemperatures = inflater.inflate(R.layout.fragment_thermal, container, false);
// Initialize data for ListView
dbHelperTmp = new DBTemperature(FragmentTemperatureDB.this.getActivity());
dbHelperTmp.open();
String[] from = new String[] { DBTemperature.COLUMN_TITLE,
DBTemperature.COLUMN_CURRENT_TEMP,
DBTemperature.COLUMN_ADJUST_TEMP};
int[] to = new int[] { R.id.roomTitleTmp,
R.id.roomTemperature,
R.id.adjustTemperature};
// Setup Adapter for ListView
dataAdapterTmp = new SimpleCursorAdapter(FragmentTemperatureDB.this.getActivity(),
R.layout.row_item_temperature, null, from, to, LIST_TMP_ID);
dataAdapterTmp.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == cursor.getColumnIndex(DBTemperature.COLUMN_ADJUST_TEMP)) {
int adjustProgress = cursor.getInt(columnIndex);
SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekBar);
((TextView) view).setText("++" + adjustProgress);
seekBar.setProgress(adjustProgress);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
//((TextView) view).setText("+" + progressValue);
Log.d(MEGADOR, "Progress Changed: " + "+" + progressValue);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.d(MEGADOR, "Progress Started:");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.d(MEGADOR, "Progress Stopped");
}
});
return true;
}
return false;
}
});
lvTemperature = (ListView) vTemperatures.findViewById(R.id.temperatureListView);
lvTemperature.setAdapter(dataAdapterTmp);
setHasOptionsMenu(true);
lvTemperature.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) {
operatePosition = pos;
tmpActionMode = FragmentTemperatureDB.this.getActivity().startActionMode(new ActionBarCallBack());
return true;
}
});
// Make loader for getting data
getLoaderManager().initLoader(LIST_TMP_ID, null, this);
// Inflate the layout for this fragment
return vTemperatures;
}
SeekBar can't be initialized during process. Stacked why?
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setProgress(int)' on a null object reference
at com.gigavar.adkmegador01.FragmentTemperatureDB$1.setViewValue(FragmentTemperatureDB.java:69)
ROW 69 is seekBar.setProgress(adjustProgress);
Also if I uncomment row
//((TextView) view).setText("+" + progressValue);
view can't be accessed within inner class. And this is second problem.
Problem solved by getting needed TextView through Parent layout:
viewHolder.adjust = (TextView) ((View)seekBar.getParent()).findViewById(R.id.adjustTemperature);
viewHolder.adjust.setText("+" + progressValue);
Also ViewHolder inserted
static class ViewHolder {
TextView roomTitleTmp;
TextView adjust;
TextView temperature;
SeekBar seekBar;
}
Finally we have working SeekBar in every row with displaying progressValue
class TmpSimpleCursorAdapter extends SimpleCursorAdapter {
public TmpSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
ViewHolder viewHolder;
@Override
public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.row_item_temperature, parent, false);
return view;
}
@Override
public void bindView(View view, Context Context, final Cursor cursor) {
String title = cursor.getString(cursor.getColumnIndex(DBTemperature.COLUMN_TITLE));
viewHolder = new ViewHolder();
viewHolder.roomTitleTmp = (TextView) view.findViewById(R.id.roomTitleTmp);
viewHolder.roomTitleTmp.setText(title);
viewHolder.temperature = (TextView) view.findViewById(R.id.roomTemperature);
viewHolder.adjust = (TextView) view.findViewById(R.id.adjustTemperature);
viewHolder.seekBar = (SeekBar) view.findViewById(R.id.seekBar);
viewHolder.adjust.setText("+" + viewHolder.seekBar.getProgress());
viewHolder.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
viewHolder.adjust = (TextView) ((View)seekBar.getParent()).findViewById(R.id.adjustTemperature);
viewHolder.adjust.setText("+" + progressValue);
Log.d(MEGADOR, "Progress Changed");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.d(MEGADOR, "Progress Started");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.d(MEGADOR, "Progress Stopped");
}
});
}
}