選択リストを実装しようとしています。各項目は画像ビューです。
ユーザーがビューをクリックすると、リスト内の56個のImageViewを表示するカスタムダイアログが開きます。
ユーザは、それをクリックして選択することができます。
イメージビューには、このitems_r1_c1 ... items_r56_c1のようなイメージがあります。
それぞれのimageviewsにonClickListenersを実装する必要があります。私は私が期待されるふるまいを得ていないのですが配列の複数のアイテムのonClickリスナーを設定する
private int i; // This is int the outer class.
...
private ImageView [] spec = new ImageView[56];
myView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//set up dialog
try {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.myCustomList);
dialog.setTitle("Select one of 56");
dialog.setCancelable(true);
dialog.show();
String s = null;
//This is where I automate the ImageView object creation
for (i=2; i<=56; i++) {
s = "items_r"+Integer.toString(i)+"_c1";
spec[i] = (ImageView) findViewById(getResources().getIdentifier(s,"drawable",getPackageName()));
spec[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myItem.setItem(Integer.toString(i));
if(i == 0) myItem.setItem("invalid");
Log.e(tag, myItem.getItem());
dialog.dismiss();
}
});
}
} catch (Exception e) {
Log.e(tag, e.toString());
}
}
:
代わりに、私はこれをしませんでした。
私は何が間違っていますか? 56のonClickリスナーを書く代わりに、これを行う効率的な方法は何ですか?
ありがとうございます。
ありがとうございます。変更を行います。 – Brahadeesh