Java onContextItemSelected()中的gridview变量错误

Java onContextItemSelected()中的gridview变量错误,java,android,android-viewpager,Java,Android,Android Viewpager,我有两个使用FragmentStatePagerAdapter的页面,每个页面都包含GridView。 问题是,当我尝试使用onContextItemSelected()中的适配器(例如myAdapter.remove(object))执行任何操作时-此操作由来自不同页面的适配器完成。 我尝试使用getUserVisibleHint()但没有成功 我很困惑,因为当GridView中的项在方法onCreateContextMenu()中被长时间剪辑时,gridAdatper来自实际页面,而在onC

我有两个使用FragmentStatePagerAdapter的页面,每个页面都包含GridView。 问题是,当我尝试使用onContextItemSelected()中的适配器(例如myAdapter.remove(object))执行任何操作时-此操作由来自不同页面的适配器完成。 我尝试使用getUserVisibleHint()但没有成功

我很困惑,因为当GridView中的项在方法onCreateContextMenu()中被长时间剪辑时,gridAdatper来自实际页面,而在onContextItemSelected中,gridAdapter来自不同的页面

预期行为: 长时间单击后,上下文菜单膨胀,而不是通过从菜单列表中选择项(例如删除),适当的gridAdapter(来自当前页面上的片段)处理此操作

当前行为: 上下文菜单膨胀后,onContextItemSeleted从不同的页面保存gridAdapter,所以当我尝试从第2页的网格中删除项时,该项将从第1页中删除

  @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        //super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getActivity().getMenuInflater();
        AdapterView.AdapterContextMenuInfo info;
        try {
            // Casts the incoming data object into the type for AdapterView objects.
            info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        } catch (ClassCastException e) {
            // If the menu object can't be cast, logs an error.
            Log.e(getClass().getName(), "bad menuInfo", e);
            return;
        }
        Log.d("ContextCreate", "grid adapter count " + gridAdapter.getCount() + " gridview count " + gridView.getCount());
        Scene scene =  gridAdapter.getItem(info.position);
        if (scene == null) {
            // For some reason the requested item isn't available, do nothing
            return;
        }
        menu.setHeaderTitle(scene.getLabel());
        inflater.inflate(R.menu.grid_scene_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {


if (getUserVisibleHint()) {

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    int positionInGrid = info.position;
    switch (item.getItemId()) {
        case R.id.action_edit_scene:

            // get item from adapter and pass it to another class
            return true;

        case R.id.action_delete:
            gridAdapter.remove(gridAdapter.getItem(positionInGrid));
            gridAdapter.notifyDataSetChanged();
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}
else
    return false;

    }
网格适配器:

public class SceneGridArrayAdapter extends ArrayAdapter<Scene> {
List<Scene> sceneList;
private static final char[] alphabet= "ABCDEFGHIJKLMNOP".toCharArray();
Context context;

boolean[] isPressed;
public SceneGridArrayAdapter(Context context, List<Scene> scenes) {
    super(context, 0, scenes);
    this.context=context;

    this.sceneList=scenes;
    isPressed= new boolean[scenes.size()];

}


@Override
public void add(Scene sceneToAdd) {
    DriversSingleton.getCurrentDriver().getConfiguration().addActiveScene(sceneToAdd);
    super.add(sceneToAdd);

 }


@Override
public void remove(Scene scene) {
    int sceneIndex=scene.getIndex();
    DriversSingleton.getCurrentDriver().getConfiguration().removeSceneAt(scene.getIndex());
    super.remove(scene);

    new CommunicationService(context).writeSceneAt(DriversSingleton.getCurrentDriver(), sceneIndex, 0xFF, 0xFF);
    notifyDataSetChanged();

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
   final Scene scene= getItem(position);
    if (convertView==null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_layout, parent, false);
    }

    final ImageView imageBackground=(ImageView) convertView.findViewById(R.id.image_rect);
    TextView sceneTv = (TextView) convertView.findViewById(R.id.scene_label);
    TextView groupTv = (TextView) convertView.findViewById(R.id.group);

    sceneTv.setText(scene.getLabel());
    if(scene.getGroup()<16) {
        groupTv.setVisibility(View.VISIBLE);
        groupTv.setText("Group " + alphabet[scene.getGroup()]);
    }
    else
        groupTv.setVisibility(View.INVISIBLE);



    if(isPressed[position]==true){

        imageBackground.setImageResource(R.drawable.scene_item_selected);
        isPressed[position]=true;

    }
    else if(isPressed[position]==false){

        imageBackground.setImageResource(R.drawable.scene_item);
        isPressed[position]=false;

    }


    return convertView;
}


public void setSelection (int position)
{
    if(isPressed[position]==false){

        isPressed[position]=true;

    }
    else if(isPressed[position]==true){
        isPressed[position]=false;
    }
    this.notifyDataSetChanged();
}
public boolean isSelected (int position)
{
    return isPressed[position];
}

}
公共类SceneGridArrayAdapter扩展了ArrayAdapter{
列出场景列表;
私有静态final char[]alphabet=“ABCDEFGHIJKLMNOP”.toCharArray();
语境;
布尔[]表示;
公共场景GridarayAdapter(上下文、列表场景){
超级(上下文,0,场景);
this.context=context;
this.sceneList=场景;
isPressed=new boolean[scenes.size()];
}
@凌驾
公共空间添加(场景场景场景添加){
DriversSingleton.getCurrentDriver().getConfiguration().addActiveScene(sceneToAdd);
super.add(sceneToAdd);
}
@凌驾
删除公共空间(场景){
int sceneIndex=scene.getIndex();
DriversSingleton.getCurrentDriver().getConfiguration().removeSceneAt(scene.getIndex());
超级。移除(场景);
新的通信服务(context).writeSceneAt(DriversSingleton.getCurrentDriver(),sceneIndex,0xFF,0xFF);
notifyDataSetChanged();
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
最终场景场景=getItem(位置);
if(convertView==null){
convertView=LayoutInflater.from(getContext()).flate(R.layout.grid\u布局,父级,false);
}
最终ImageView imageBackground=(ImageView)convertView.findViewById(R.id.image_rect);
TextView场景电视=(TextView)convertView.findViewById(R.id.scene_标签);
TextView-groupTv=(TextView)convertView.findViewById(R.id.group);
sceneTv.setText(scene.getLabel());
如果(scene.getGroup()我找到了我的bug

在onResume中安装新的pagerAdapter以刷新寻呼机标签顺序,这是一个丑陋的代码

因此,要使用上下文菜单和pagerView正确处理gridAdapter对象,请执行以下操作:

  • 设置适配器后调用registerForContextMenu(视图v)
  • 通过-getUserVisibleHint()对项目select发出指令,仅对可见片段进行检查,否则返回false
  • 不要每次都恢复pagerAdapter

  • 您好,欢迎来到Stackoverflow!您的问题并没有从您的问题中立即显现出来。请尝试重新格式化或改写您的第一段,以便清楚地显示您的错误。理想情况下,请清楚地提供您期望的内容和您实际得到的内容。@Knossos感谢您的关注。我添加了两段,以便更好地解释您的问题我的意思是。@Blackbelt我添加了GridAdapter,我想注意,当创建上下文菜单时,适配器来自检测到click事件的片段(页面),但在选择菜单项之后,适配器来自不同的页面(第一次扩展上下文菜单的页面)不,我没有任何页眉/页脚视图。很好,请确保您接受自己的答案,以便完成此问题。您可能还无法完成。