Last active
December 11, 2015 02:28
-
-
Save STAR-ZERO/4530564 to your computer and use it in GitHub Desktop.
【Android】リストダイアログの項目管理を楽にする
This file contains 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> | |
<string-array name="list_dialog"> | |
<item>@string/list1</item> | |
<item>@string/list2</item> | |
<item>@string/list3</item> | |
</string-array> | |
</resources> |
This file contains 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
public class ListDialogBuilder extends AlertDialog.Builder { | |
private final Context mContext; | |
public ListDialogBuilder(Context context) { | |
super(context); | |
mContext = context; | |
} | |
@SuppressLint("NewApi") | |
public ListDialogBuilder(Context context, int theme) { | |
super(context, theme); | |
mContext = context; | |
} | |
public void setItems(final int itemsId, final OnItemClickListener listener) { | |
setItems(itemsId, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
TypedArray items = mContext.getResources().obtainTypedArray(itemsId); | |
int resId = items.getResourceId(which, 0); | |
listener.onClick(dialog, resId); | |
} | |
}); | |
} | |
public interface OnItemClickListener { | |
void onClick(DialogInterface dialog, int resId); | |
} | |
} |
This file contains 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
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Button button = (Button) findViewById(R.id.button); | |
button.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
showListDialog(); | |
} | |
}); | |
} | |
private void showListDialog() { | |
ListDialogBuilder builder = new ListDialogBuilder(this); | |
builder.setItems(R.array.list_dialog, new ListDialogBuilder.OnItemClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int resId) { | |
switch (resId) { | |
case R.string.list1: | |
Toast.makeText(MainActivity.this, "リスト1", Toast.LENGTH_SHORT).show(); | |
break; | |
case R.string.list2: | |
Toast.makeText(MainActivity.this, "リスト2", Toast.LENGTH_SHORT).show(); | |
break; | |
case R.string.list3: | |
Toast.makeText(MainActivity.this, "リスト3", Toast.LENGTH_SHORT).show(); | |
break; | |
} | |
} | |
}); | |
builder.create().show(); | |
} | |
} |
This file contains 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> | |
<string name="list1">リスト1</string> | |
<string name="list2">リスト2</string> | |
<string name="list3">リスト3</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment