1 package com.koushikdutta.widgets;
3 import android.content.Context;
4 import android.view.LayoutInflater;
5 import android.view.View;
6 import android.widget.CompoundButton;
7 import android.widget.CompoundButton.OnCheckedChangeListener;
8 import android.widget.ImageView;
9 import android.widget.TextView;
11 public class ListItem {
13 private String Summary;
14 private ActivityBaseFragment Context;
15 private boolean Enabled = true;
19 public ListItem setEnabled(boolean enabled) {
21 Context.mAdapter.notifyDataSetChanged();
25 public boolean getEnabled() {
29 public ListItem setTitle(int title) {
31 return setTitle(null);
33 return setTitle(Context.getString(title));
36 public String getTitle() {
40 public ListItem setTitle(String title) {
42 Context.mAdapter.notifyDataSetChanged();
46 public ListItem setSummary(int summary) {
48 return setSummary(null);
50 return setSummary(Context.getString(summary));
53 public ListItem setSummary(String summary) {
55 Context.mAdapter.notifyDataSetChanged();
59 public ListItem(ActivityBaseFragment context, int title, int summary) {
61 Title = context.getString(title);
63 Summary = context.getString(summary);
67 public ListItem(ActivityBaseFragment context, String title, String summary) {
73 public ListItem(ActivityBaseFragment context, int title, int summary, int icon) {
74 this(context, title, summary);
78 public ListItem(ActivityBaseFragment context, String title, String summary, int icon) {
79 this(context, title, summary);
83 private boolean CheckboxVisible = false;
84 private boolean checked = false;
86 public ListItem setCheckboxVisible(boolean visible) {
87 CheckboxVisible = visible;
88 Context.mAdapter.notifyDataSetChanged();
92 public boolean getCheckboxVisible() {
93 return CheckboxVisible;
96 public boolean getChecked() {
100 public ListItem setChecked(boolean isChecked) {
102 CheckboxVisible = true;
103 Context.mAdapter.notifyDataSetChanged();
107 // boolean mUseOnOff = false;
108 // public void useYesNo() {
112 // private void setSwitch(View view) {
113 // Switch s = (Switch)view;
114 // s.setTextOn(Context.getString(R.string.yes).toUpperCase());
115 // s.setTextOff(Context.getString(R.string.no).toUpperCase());
116 // view.setTag(view);
119 public View getView(Context context, View convertView) {
120 if (convertView == null || convertView.getTag() != null)
121 convertView = LayoutInflater.from(context).inflate(Context.getListItemResource(), null);
124 TextView title = (TextView)convertView.findViewById(R.id.title);
125 TextView summary = (TextView)convertView.findViewById(R.id.summary);
126 CompoundButton cb = (CompoundButton)convertView.findViewById(R.id.checkbox);
127 cb.setOnCheckedChangeListener(null);
128 cb.setChecked(checked);
129 final View cv = convertView;
130 cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
132 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
134 ListItem.this.onClick(cv);
137 // if (mUseOnOff && !(cb instanceof CheckBox)) {
140 cb.setVisibility(CheckboxVisible ? View.VISIBLE : View.GONE);
141 cb.setChecked(checked);
143 cb.setEnabled(Enabled);
144 title.setEnabled(Enabled);
145 summary.setEnabled(Enabled);
148 title.setVisibility(View.VISIBLE);
149 title.setText(Title);
152 title.setVisibility(View.GONE);
153 if (Summary != null) {
154 summary.setVisibility(View.VISIBLE);
155 summary.setText(Summary);
158 summary.setVisibility(View.GONE);
160 ImageView iv = (ImageView)convertView.findViewById(R.id.image);
163 iv.setVisibility(View.VISIBLE);
164 iv.setImageResource(Icon);
167 iv.setVisibility(View.GONE);
174 void onClickInternal(View view) {
175 if (CheckboxVisible) {
176 CompoundButton cb = (CompoundButton)view.findViewById(R.id.checkbox);
177 // this will trigger onclick
178 cb.setChecked(!cb.isChecked());
185 public void onClick(View view) {
188 public boolean onLongClick() {