0

そこで、次のような 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 が長押しされたかを調べる必要があります。そこにあるすべてのチュートリアルは、アクティビティの静的ビューでそれを実行し続けますが、これは好きではありません。

4

1 に答える 1

3

ということで、やっと答えが見えてきました。したがって、アクティビティのコンテキスト メニューに GridView を登録するとActivity.registerForContextMenu(GridView)、アダプタから返される各ビューが個別に登録されます。アクティビティは次のようになります (アダプターは変更されません)。

public class SMSForwarderActivity extends Activity {
private GridView profilesGridView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    setUpProfilesGrid();
}

    private void setUpProfilesGrid() {
    profilesGridView = (GridView) this.findViewById(R.id.profilesGrid);
    this.registerForContextMenu(profilesGridView);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterContextMenuInfo aMenuInfo = (AdapterContextMenuInfo) menuInfo;
    ProfileTile tile = (ProfileTile) aMenuInfo.targetView;//This is how I get a grip on the view that got long pressed.
}

    @Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();
    ProfileTile tile = (ProfileTile) info.targetView;//Here we get a grip again of the view that opened the Context Menu
    return super.onContextItemSelected(item);
}

}

したがって、解決策は十分に単純でしたが、物事を複雑にしすぎることもあります。

于 2011-08-12T02:57:03.217 に答える