LogEntryListCursorAdapter.java :  » App » skydiveapp-4-android » org » skydiveapp » ui » Android Open Source

Android Open Source » App » skydiveapp 4 android 
skydiveapp 4 android » org » skydiveapp » ui » LogEntryListCursorAdapter.java
package org.skydiveapp.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.skydiveapp.R;
import org.skydiveapp.data.Database;
import org.skydiveapp.repository.LogEntryRepository;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LogEntryListCursorAdapter extends CursorAdapter
{
  private List<Integer> selectedIds = new ArrayList<Integer>();
  private boolean multiSelect = false;
  
  public LogEntryListCursorAdapter(Context context, Cursor cursor)
  {
    this(context, cursor, false);
  }
  
  public LogEntryListCursorAdapter(Context context, Cursor cursor, boolean multiSelect)
  {
    super(context, cursor);
    this.multiSelect = multiSelect;
  }
  
  public void clearSelection()
  {
    selectedIds.clear();
  }
  
  public void toggleSelection(int id)
  {
    // box it
    Integer boxedId = id;
    if (selectedIds.contains(boxedId))
      selectedIds.remove(boxedId);
    else
      selectedIds.add(boxedId);
  }
  
  public ArrayList<Integer> getSelection()
  {
    return (ArrayList<Integer>)selectedIds;
  }

  @Override
  public void bindView(View view, Context context, Cursor cursor)
  {
    // get data
    int id = Database.getInteger(cursor, Database.LOG_ENTRY_ID);
    int jumpNumber = Database.getInteger(cursor, Database.LOG_ENTRY_JUMP_NUMBER);
    String skydiveTypeName = Database.getString(cursor, LogEntryRepository.SKYDIVE_TYPE_NAME);
    String locationName = Database.getString(cursor, LogEntryRepository.LOCATION_NAME);
    String aircraftName = Database.getString(cursor, LogEntryRepository.AIRCRAFT_NAME);
    Date date = Database.getDate(cursor, Database.LOG_ENTRY_DATE);
    String notes = Database.getString(cursor, Database.LOG_ENTRY_NOTES);
    
    // set jump number
    setText(
        view,
        R.id.LogEntryListItemJumpNumberField,
        FormatUtility.toNumericString(jumpNumber));
    
    // set location
    setText(
        view,
        R.id.LogEntryListItemLocationField,
        locationName);
    
    // set date
    setText(
        view,
        R.id.LogEntryListItemDateTimeField,
        FormatUtility.toDateString(context, date));
    
    // set skydive type
    setText(
        view,
        R.id.LogEntryListItemSkydiveTypeField,
        skydiveTypeName,
        true);
    
    // set aircraft
    setText(
        view,
        R.id.LogEntryListItemAircraftField,
        aircraftName,
        true);
    
    // set notes
    setHint(
        view,
        R.id.LogEntryListItemNotesField,
        notes);

    // if multiselect
    ImageView selectedField = (ImageView)view.findViewById(R.id.LogEntryListSelectedFieldField);
    selectedField.setImageDrawable(null);
    if (multiSelect)
    {
      // set selected state
      if (selectedIds.contains(id))
      {
        selectedField.setImageResource(R.drawable.btn_check_on);
      }
      else
      {
        selectedField.setImageResource(R.drawable.btn_check_off);
      }
    }
  }

  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent)
  {
    // inflate view
    return View.inflate(
        context,
        R.layout.log_entry_view,
        null);
  }
  
  private void setText(View parentView, int id, String text)
  {
    setText(parentView, id, text, false);
  }
  
  private void setText(View parentView, int id, String text, boolean hideIfEmpty)
  {
    TextView tv = (TextView)parentView.findViewById(id);
    tv.setText(text);
    tv.setVisibility(hideIfEmpty && FormatUtility.isEmptyOrNull(text) ? View.GONE : View.VISIBLE);
  }
  
  private void setHint(View parentView, int id, String text)
  {
    TextView tv = (TextView)parentView.findViewById(id);
    tv.setHint(text);
    tv.setVisibility(FormatUtility.isEmptyOrNull(text) ? View.GONE : View.VISIBLE);
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.