そこで、次のような BaseAdaper を拡張するクラスを作成しました。
public class ProfileTileAdapter extends BaseAdapter {
private Context context;
private ForwardingProfile[] profiles;
public ProfileTileAdapter(Context context, ForwardingProfile[] profiles) {
this.context = context;
this.profiles = profiles;
}
@Override
public int getCount() {
return profiles.length;
}
@Override
public Object getItem(int position) {
return profiles[position];
}
@Override
public long getItemId(int position) {
return profiles[position].getID();
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ProfileTile tile = null;
if (convertView == null) {
tile = new ProfileTile(context, profiles[position]);
LayoutParams lp = new GridView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
tile.setLayoutParams(lp);
} else {
tile = (ProfileTile) convertView;
}
return tile;
}
}
私のアクティビティでは、GridLayout があり、そのアダプターを ProfileTileAdapter のインスタンスに設定します。私のアクティビティでは、ユーザーがビューの 1 つ (この場合は ProfileTile) を長押ししたときにコンテキスト メニューを開きたいのですが、方法がわかりません。また、ユーザーがコンテキスト メニューのオプションを選択したときに、どの ProfileTile が長押しされたかを調べる必要があります。そこにあるすべてのチュートリアルは、アクティビティの静的ビューでそれを実行し続けますが、これは好きではありません。