Reputation: 2494
What i found was that i needed a ViewBinder not a custom cursor addapter However the issue i'm having is that on each row it doesn't re-determine if it needs to be hidden or not. Its just always keeping it hidden
Edit: We've gotting it to show the images and hide them when needed but they are disheartening when i scroll.
public class HistoryFragment extends Fragment {
ListView listTimeline;
SimpleCursorAdapter adapter;
TextView txtCreatedAt, txtFertile, txtTemp, txtCervix;
ImageView imgIntercorse, imgEnergy, imgPregancy, imgMood, imgHeadache, imgPeriod;
IntentFilter filter;
static final String[] FROM = { StatusData.KEY_CHARTING_DATE, StatusData.KEY_CHARTING_NOTES, StatusData.KEY_CHARTING_FERTILE,
StatusData.KEY_CHARTING_TEMPERATURE, StatusData.KEY_CHARTING_PERIOD, StatusData.KEY_CHARTING_INTERCORSE,
StatusData.KEY_CHARTING_MOOD, StatusData.KEY_CHARTING_HEADACHE, StatusData.KEY_CHARTING_TEST,
StatusData.KEY_CHARTING_ENERGY };
static final int[] TO = { R.id.txtCreatedAt, R.id.txtNote, R.id.txtFertile,
R.id.txtTemp, R.id.imgPeriod, R.id.imgIntercorse,
R.id.imgMood, R.id.imgHeadache, R.id.imgPregancy,
R.id.imgEnergy};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
listTimeline = (ListView) view.findViewById(R.id.listTimeline);
return view;
}
@Override
public void onResume() {
super.onResume();
this.setupList();
}
@Override
public void onPause() {
super.onPause();
}
private void setupList() {
txtCreatedAt = (TextView) getActivity().findViewById(R.id.txtCreatedAt);
txtFertile = (TextView) getActivity().findViewById(R.id.txtFertile);
txtTemp = (TextView) getActivity().findViewById(R.id.txtTemp);
txtCervix = (TextView) getActivity().findViewById(R.id.txtCervix);
imgIntercorse = (ImageView) getActivity().findViewById(R.id.imgIntercorse);
imgEnergy = (ImageView) getActivity().findViewById(R.id.imgEnergy);
imgPregancy = (ImageView) getActivity().findViewById(R.id.imgPregancy);
imgMood = (ImageView) getActivity().findViewById(R.id.imgMood);
imgHeadache = (ImageView) getActivity().findViewById(R.id.imgHeadache);
imgPeriod = (ImageView) getActivity().findViewById(R.id.imgPeriod);
// Get the data
Cursor c = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_CHARTING, null, null , null, StatusData.KEY_CHARTING_DATE + " DESC"); // <3>
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
adapter = new SimpleCursorAdapter(getActivity(), R.layout.history_row, c, FROM, TO);
adapter.setViewBinder(new CustomViewBinder());
// Assign adapter to ListView
listTimeline.setAdapter(adapter);
}
private class CustomViewBinder implements ViewBinder {
private Date parseDate(String date) {
SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd");
Date dateObj = new Date();
try {
dateObj = curFormater.parse(date);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dateObj;
}
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_DATE)) {
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String date = cursor.getString(columnIndex);
Date dateObj = parseDate(date);
String formatedDate = format.format(dateObj);
TextView tv = (TextView) view;
tv.setText(formatedDate);
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_PERIOD)) {
// If the column is IS_STAR then we use custom view.
String is_period = cursor.getString(columnIndex);
if (is_period != null) {
if (is_period.equalsIgnoreCase("no")){
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_INTERCORSE)) {
// If the column is IS_STAR then we use custom view.
String is_intercorse = cursor.getString(columnIndex);
if (is_intercorse != null) {
if (is_intercorse.equalsIgnoreCase("no")) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_MOOD)) {
// If the column is IS_STAR then we use custom view.
String is_mood = cursor.getString(columnIndex);
if (is_mood != null) {
if (is_mood.equalsIgnoreCase("no") ) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_HEADACHE)) {
// If the column is IS_STAR then we use custom view.
String is_headache = cursor.getString(columnIndex);
if (is_headache != null) {
if (is_headache.equalsIgnoreCase("no")) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_TEST)) {
// If the column is IS_STAR then we use custom view.
String is_test = cursor.getString(columnIndex);
if (is_test != null) {
if (is_test.equalsIgnoreCase("no")) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_ENERGY)) {
// If the column is IS_STAR then we use custom view.
String is_energy = cursor.getString(columnIndex);
if (is_energy != null) {
if (is_energy.equalsIgnoreCase("no")) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
// For others, we simply return false so that the default binding
// happens.
return false;
}
}
}
Upvotes: 0
Views: 327
Reputation: 526
You've got some null variables. If this is an acceptable value, then just check for null values in your if
conditions. If it is not, then you need to change the SQLite table that you're pulling this data from. Something like:
CREATE TABLE some_table(_id INTEGER PRIMARY KEY AUTOINCREMENT, some_column TEXT NOT NULL)
etc.
With this structure, if an insertion doesn't include a value for this column, then it will fail.
Upvotes: 1
Reputation: 30088
I think that the NullPointerException is due to the fact that your layout, android.R.layout.simple_list_item_1
(a standard Android list item layout) does not contain a view with an id of R.id.txtCreatedAt
Did you mean to use a custom view of your own?
Upvotes: 0