Domanda

So I made a class extend BaseAdaper that looks like this:

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;
}

}

In my activity a have a GridLayout and set its adapter to an instance of ProfileTileAdapter. In my activity I want to open a context menu when the user long presses on one of the views (in this case a ProfileTile) but I don't know how. I also need to find out what ProfileTile got long pressed when the user chooses an option in the context menu All the tutorials out there keep doing it with static views in the activity but not like this.

È stato utile?

Soluzione

So I ended up figuring out the answer. So apparently when you register a GridView to for context menu in your Activity using Activity.registerForContextMenu(GridView) it registers each view you return from adapter independently. So this is how the Activity looks like (the adapter stays unchanged):

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);
}

}

So the solution was simple enough, but sometimes we over complicate things.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top