I have to display a ListView and each view will have different values depend on Type. All those values need to be populated from a single DB table. If I have 10 columns in Table, then in ListView, some rows will contain values from few columns and others from some different columns, depends on value of Type column of same table. Some data is common. View will be mix of all these different rows.
I wrote SimpleCursorAdapter, to fetch all column values and in setViewBinder(), depending on Type, I am setting values for different TextView. But somehow, in display, it comes blank. In debugger, I can see the values getting populated and set correctly.
Any way to fix this? Or any alternate approach to achieve the same?
Please note that, only at runtime, after querying DB, I can get Type value for that particular ROW.
Below is the code.
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
dbHelper = new BilloDatabaseHelper(getActivity());
db = dbHelper.getReadableDatabase();
Bundle args = getArguments();
long billId = args.getLong(IntentConstant.SOMEID.name());
ListView listView = (ListView) getActivity().findViewById(R.id.itemListView);
cursor = db.query("ITEM_DETAILS",
new String[]{"_id", "COLUMN1", "TYPE", "COLUMN2", "COLUMN3", "COLUMN4", "COLUMN5", "COLUMN6", "COLUMN7", "COLUMN8", "COLUMN9", "COLUMN10", "COLUMN11", "COLUMN12"},
"SOME_ID = ?", //Where clause
new String[]{Long.toString(billId)},
null, //Group by
null,
null
);
SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.item_list_view,
cursor,
new String[]{"COLUMN1", "TYPE", "COLUMN2", "COLUMN3", "COLUMN4", "COLUMN5", "COLUMN6", "COLUMN7", "COLUMN8", "COLUMN9", "COLUMN10", "COLUMN11", "COLUMN12"},
new int[]{R.id.COLUMN1, R.id.TYPE, R.id.COLUMN2, R.id.COLUMN3, R.id.COLUMN4, R.id.COLUMN5, R.id.COLUMN6, R.id.COLUMN7, R.id.COLUMN8, R.id.COLUMN9, R.id.COLUMN10, R.id.COLUMN9, R.id.COLUMN10},
0);
final int[] TYPE = {0};
listAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == 2){
TYPE[0] = cursor.getInt(cursor.getColumnIndex("TYPE"));
}else if(columnIndex == 4){
if(TYPE[0] == 1) {
String val = cursor.getString(cursor.getColumnIndex("COLUMN3"));
TextView COLUMN3 = (TextView) view.findViewById(R.id.COLUMN3);
COLUMN3.setText(val);
return true;
}
}else if(columnIndex == 9){
if(TYPE[0] == 1) {
long date = cursor.getLong(cursor.getColumnIndex("COLUMN8"));
TextView dayText = (TextView) view.findViewById(R.id.COLUMN8);
dayText.setText(DateUtility.getDDMMYYYYDate(date));
return true;
}
}else if(columnIndex == 10){
if(TYPE[0] == 1) {
long date = cursor.getLong(cursor.getColumnIndex("COLUMN9"));
TextView dayText = (TextView) view.findViewById(R.id.COLUMN9);
dayText.setText(DateUtility.getDDMMYYYYDate(date));
return true;
}
}else if(columnIndex == 11){
if(TYPE[0] == 2) {
String sizeVal = cursor.getString(cursor.getColumnIndex("COLUMN10"));
TextView size = (TextView) view.findViewById(R.id.COLUMN9);
size.setText(sizeVal);
return true;
}
}else if(columnIndex == 12){
if(TYPE[0] == 2) {
String fitVal = cursor.getString(cursor.getColumnIndex("COLUMN11"));
TextView fit = (TextView) view.findViewById(R.id.COLUMN10);
fit.setText(fitVal);
return true;
}
}
return false;
}
});
listView.setAdapter(listAdapter);
}
Values for R.id.COLUMN9 and R.id.COLUMN10 are not getting visible for column's value COLUMN9 and COLUMN10. They are setting blank even for Type 1. For Type 2, with columns COLUMN11 and COLUMN12, they are visible on screen.
This is the work around solution. I convert cursor data to List of JSONObject and pass that JSONObject list new ListAdapter where based on Type, I can easily modify and update the TextView values.