Skip to content

Instantly share code, notes, and snippets.

@csdear
Created March 5, 2014 18:32
Show Gist options
  • Select an option

  • Save csdear/ac8faf6677094ae28d6b to your computer and use it in GitHub Desktop.

Select an option

Save csdear/ac8faf6677094ae28d6b to your computer and use it in GitHub Desktop.
BaseAdapter
package <<packageName>>;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import course.labs.todomanager.ToDoItem.Status;
public class <<newAdapterClassName>> extends BaseAdapter {
// List of <<itemClassName>>
private final List<<<itemClassName>>> mItems = new ArrayList<<<itemClassName>>>();
private final Context mContext;
private static final String TAG = "<<tagString>>";
public <<newAdapterClassName>>(Context context) {
mContext = context;
}
// Add a Item to the adapter
// Notify observers that the data set has changed
public void add(<<itemClassName>> item) {
mItems.add(item);
notifyDataSetChanged();
}
// Clears the list adapter of all items.
public void clear() {
mItems.clear();
notifyDataSetChanged();
}
// Returns the number of Items
@Override
public int getCount() {
return mItems.size();
}
// Retrieve the number of Items
@Override
public Object getItem(int pos) {
return mItems.get(pos);
}
// Get the ID for the Item
// In this case it's just the position
@Override
public long getItemId(int pos) {
return pos;
}
// Create a View to display the ToDoItem
// at specified position in mItems
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the current Item
final <<itemClassName>> <<instanceItemName>> = (<<itemClassName>>) getItem(position);
//Inflate the View for this ToDoItem
RelativeLayout <<newItemViewName>> = (RelativeLayout) LayoutInflater.from(
mContext).inflate(R.layout.<<itemXMLview>>, null);
/* Fill in specific Item data
* Data that goes in this View, corresponds to the user interface
* elements defined in the layout file
*/
//Display Title in TextView
final TextView <<instanceTextViewName>> = (TextView) itemLayout
.findViewById(R.id.<<XMLLayoutTextView>>);
<<instanceTextViewName>>.setText(<<itemClassName>>.getTitle());
// Set up Status CheckBox
final CheckBox <<checkboxInstanceName>> = (CheckBox) itemLayout.findViewById(R.id.statusCheckBox);
statusView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
log("Entered onCheckedChanged()");
// TODO - Set up and implement an OnCheckedChangeListener, which
// is called when the user toggles the status checkbox
if(isChecked)
toDoItem.setStatus(Status.DONE);
else
toDoItem.setStatus(Status.NOTDONE);
}
});
final CheckBox statusView = (CheckBox) itemLayout
.findViewById(R.id.statusCheckBox);
statusView.setChecked(Status.DONE.equals(toDoItem.getStatus()) ? true
: false);
statusView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
log("Entered onCheckedChanged()");
// TODO - Set up and implement an OnCheckedChangeListener, which
// is called when the user toggles the status checkbox
buttonView.setChecked(isChecked);
toDoItem.setStatus(isChecked ? Status.DONE : Status.NOTDONE);
}
});
*/
// TODO - Display Priority of an item in a TextView
;
final TextView priorityView = (TextView) itemLayout.findViewById(R.id.priorityView);
priorityView.setText(<<itemCLassName>>.getPriority().toString());
// TODO - Display Time and Date.
// Hint - use ToDoItem.FORMAT.format(toDoItem.getDate()) to get date and
// time String
final TextView dateView = (TextView) itemLayout
.findViewById(R.id.dateView);
dateView.setText(<<itemClassName>>.FORMAT.format(<<itemClassName>>.getDate()));
// Return the View you just created
return itemLayout;
}
private void log(String msg) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(TAG, msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment