Created
August 29, 2013 01:01
-
-
Save neiraza/6373191 to your computer and use it in GitHub Desktop.
今日は僕の誕生日なので、誕生日を公開するSpinnerネタ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<!-- birthday_permission --> | |
<string-array name="birthday_permission_data"> | |
<item>@array/birthday_permission_public</item> | |
<item>@array/birthday_permission_protected</item> | |
<item>@array/birthday_permission_private</item> | |
</string-array> | |
<string-array name="birthday_permission_public"> | |
<item>0</item> | |
<item>@string/birthday_permission_public</item> | |
</string-array> | |
<string-array name="birthday_permission_protected"> | |
<item>1</item> | |
<item>@string/birthday_permission_protected</item> | |
</string-array> | |
<string-array name="birthday_permission_private"> | |
<item>2</item> | |
<item>@string/birthday_permission_private</item> | |
</string-array> | |
</resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by togu on 2013/07/23. | |
*/ | |
public class FormActivity extends Activity implements OnItemSelectedListener { | |
private Spinner birthdayPermission; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); | |
setContentView(R.layout.form); | |
//誕生日を公開するか、どうするか決めてもらいたくて、Spinner様登場 | |
birthdayPermission = (Spinner) findViewById(R.id.birthday_permission_spinner); | |
birthdayPermission.setOnItemSelectedListener(this); | |
//adapterを再利用可能な感じにしてみた | |
KeyValueAdapter adapter = SpinnerHelper.getKeyValueAdapter(cxt, getResources(), R.array.birthday_permission_data); | |
birthdayPermission.setAdapter(adapter); | |
} | |
//OnItemSelectedListener | |
@Override | |
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { | |
long selectedItemId = adapterView.getSelectedItemId(); | |
/* | |
ここのkey-valueを管理したかっただけなのー | |
selectedItemId : 0 すべて公開、1 誕生日のみ公開、2 非公開 | |
*/ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by togu on 2013/07/26. | |
*/ | |
public class KeyValueAdapter extends ArrayAdapter<String> { | |
private ArrayList<? extends KeyValueItem> items; | |
private Context context; | |
public KeyValueAdapter(Context context, int resourceId, ArrayList<? extends KeyValueItem> items) { | |
super(context, resourceId); | |
this.context = context; | |
this.items = items; | |
} | |
public int getCount() { | |
return items.size(); | |
} | |
public String getItem(int position) { | |
return items.get(position).getOptionLabel(); | |
} | |
public long getItemId(int position) { | |
return items.get(position).getOptionValue(); | |
} | |
public int getPosition(long itemId) { | |
for (int i = 0; i < items.size(); i++) { | |
if (items.get(i).getOptionValue() == itemId) { | |
return i; | |
} | |
} | |
return 0; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
TextView label = new TextView(context); | |
label.setTextColor(Color.BLACK); | |
label.setText(items.get(position).getOptionLabel()); | |
return label; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by togu on 2013/07/26. | |
*/ | |
public class SpinnerHelper { | |
public static ArrayList<SpinnerObject> getList(Resources res, int arrayId) { | |
TypedArray spinner_data = res.obtainTypedArray(arrayId); | |
ArrayList<SpinnerObject> list = new ArrayList<SpinnerObject>(); | |
for (int i = 0; i < spinner_data.length(); ++i) { | |
int id = spinner_data.getResourceId(i, -1); | |
if (id > -1) { | |
String[] item = res.getStringArray(id); | |
list.add(new SpinnerObject(Integer.valueOf(item[0]), item[1])); | |
} | |
} | |
spinner_data.recycle(); | |
return list; | |
} | |
public static KeyValueAdapter getKeyValueAdapter(Context cxt, Resources res, int arrayId) { | |
ArrayList<SpinnerObject> list = getList(res, arrayId); | |
KeyValueAdapter adapter = new KeyValueAdapter(cxt, android.R.layout.simple_spinner_item, list); | |
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
return adapter; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by togu on 2013/07/26. | |
*/ | |
public class SpinnerObject implements KeyValueItem { | |
private int id; | |
private String name; | |
public SpinnerObject(int id, String name) { | |
this.id = id; | |
this.name = name; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Override | |
public long getOptionValue() { | |
return id; | |
} | |
@Override | |
public String getOptionLabel() { | |
return name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment