我目前有一个带有自定义数组适配器和过滤器的网格视图。我试图基于对gridview项的长按来创建上下文菜单。但是,在添加过滤器之前,我会根据其位置说明已按下哪个项目,现在,当项目被过滤时,位置会发生变化,因此所采取的任何操作都会使用错误的资源/选择错误的项目。围绕这个想法吗?
这是我的gridview初始化:
final GridView gridview = (GridView) findViewById(R.id.gridview);
aAdpt = new shareadapter(planetsList, this);
gridview.setAdapter(aAdpt);
gridview.setTextFilterEnabled(true);
this.registerForContextMenu(gridview);
这是我的onClickListener(作为我通常如何获取要在活动/意图中使用的过滤项目的示例:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Planet link = (Planet) gridview.getItemAtPosition(position);
Intent i = new Intent(ShareActivity.this,
ShareActivitySecond.class);
i.putExtra("IdImgFull", link.getIdImgFull());
i.putExtra("IdImgShare", link.getIdImgShare());
startActivity(i);
// startSecondActivity(position);
}
});
这是我的创建上下文菜单,你可以看到它取决于位置,我不知道如何添加变量int link.getIdImgShare()
进入它。 @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.shareactivity, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
int IndexSelected = info.position;
switch (item.getItemId()) {
case R.id.share:
shareMethod(IndexSelected);
return true;
case R.id.save:
saveMethod(IndexSelected);
return true;
default:
return true;
}
}
答案 0 :(得分:0)
管理解决它!
在onContextItemSelected中调用我的适配器和数组,并从中可以将变量设置为操作。关键的两行是:
Planet planet = aAdpt.getItem(aInfo.position);
int IdImgShare = planet.getIdImgShare();
完整部分:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) item
.getMenuInfo();
Planet planet = aAdpt.getItem(aInfo.position);
int IdImgShare = planet.getIdImgShare();
switch (item.getItemId()) {
case R.id.share:
shareMethod(IdImgShare);
return true;
case R.id.save:
saveMethod(IdImgShare);
return true;
default:
return true;
}
}