OSDN Git Service

initial commit
[android-x86/external-koush-Widgets.git] / Widgets / src / com / koushikdutta / widgets / ListItem.java
1 package com.koushikdutta.widgets;
2
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;
10
11 public class ListItem {
12     private String Title;
13     private String Summary;
14     private ActivityBaseFragment Context;
15     private boolean Enabled = true;
16
17     public int Icon;
18     
19     public ListItem setEnabled(boolean enabled) {
20         Enabled = enabled;
21         Context.mAdapter.notifyDataSetChanged();
22         return this;
23     }
24     
25     public boolean getEnabled() {
26         return Enabled;
27     }
28     
29     public ListItem setTitle(int title) {
30         if (title == 0)
31             return setTitle(null);
32         else
33             return setTitle(Context.getString(title));
34     }
35     
36     public String getTitle() {
37         return Title;
38     }
39     
40     public ListItem setTitle(String title) {
41         Title = title;
42         Context.mAdapter.notifyDataSetChanged();
43         return this;
44     }
45
46     public ListItem setSummary(int summary) {
47         if (summary == 0)
48             return setSummary(null);
49         else
50             return setSummary(Context.getString(summary));
51     }
52     
53     public ListItem setSummary(String summary) {
54         Summary = summary;
55         Context.mAdapter.notifyDataSetChanged();
56         return this;
57     }
58     
59     public ListItem(ActivityBaseFragment context, int title, int summary) {
60         if (title != 0)
61             Title = context.getString(title);
62         if (summary != 0)
63             Summary = context.getString(summary);
64         Context = context;
65     }
66     
67     public ListItem(ActivityBaseFragment context, String title, String summary) {
68         Title = title;
69         Summary = summary;
70         Context = context;
71     }
72     
73     public ListItem(ActivityBaseFragment context, int title, int summary, int icon) {
74         this(context, title, summary);
75         Icon = icon;
76     }
77     
78     public ListItem(ActivityBaseFragment context, String title, String summary, int icon) {
79         this(context, title, summary);
80         Icon = icon;
81     }
82     
83     private boolean CheckboxVisible = false;
84     private boolean checked = false;
85     
86     public ListItem setCheckboxVisible(boolean visible) {
87         CheckboxVisible = visible;
88         Context.mAdapter.notifyDataSetChanged();
89         return this;
90     }
91
92     public boolean getCheckboxVisible() {
93         return CheckboxVisible;
94     }
95     
96     public boolean getChecked() {
97         return checked;
98     }
99     
100     public ListItem setChecked(boolean isChecked) {
101         checked = isChecked;
102         CheckboxVisible = true;
103         Context.mAdapter.notifyDataSetChanged();
104         return this;
105     }
106 //    
107 //    boolean mUseOnOff = false;
108 //    public void useYesNo() {
109 //        mUseOnOff = true;
110 //    }
111 //    
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);
117 //    }
118     
119     public View getView(Context context, View convertView) {
120         if (convertView == null || convertView.getTag() != null)
121             convertView = LayoutInflater.from(context).inflate(Context.getListItemResource(), null);
122         
123         
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() {
131             @Override
132             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
133                 checked = isChecked;
134                 ListItem.this.onClick(cv);
135             }
136         });
137 //        if (mUseOnOff && !(cb instanceof CheckBox)) {
138 //            setSwitch(cb);
139 //        }
140         cb.setVisibility(CheckboxVisible ? View.VISIBLE : View.GONE);
141         cb.setChecked(checked);
142
143         cb.setEnabled(Enabled);
144         title.setEnabled(Enabled);
145         summary.setEnabled(Enabled);
146
147         if (Title != null) {
148             title.setVisibility(View.VISIBLE);
149             title.setText(Title);
150         }
151         else
152             title.setVisibility(View.GONE);
153         if (Summary != null) {
154             summary.setVisibility(View.VISIBLE);
155             summary.setText(Summary);
156         }
157         else
158             summary.setVisibility(View.GONE);
159
160         ImageView iv = (ImageView)convertView.findViewById(R.id.image);
161         if (iv != null) {
162             if (Icon != 0) {
163                 iv.setVisibility(View.VISIBLE);
164                 iv.setImageResource(Icon);
165             }
166             else {
167                 iv.setVisibility(View.GONE);
168             }
169         }
170         
171         return convertView;
172     }
173     
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());
179         }
180         else {
181             onClick(view);
182         }
183     }
184     
185     public void onClick(View view) {
186     }
187     
188     public boolean onLongClick() {
189         return false;
190     }
191 }