为什么上下文菜单不在ListView中显示?
问题描述:
我想显示一个上下文菜单长按,在搜索结果活动中的ListView项,我用“registerForContextMenu(View v)”函数,也重写了上下文菜单的创建方法,但是上下文菜单不是生成并且“onCreateContextMenu”方法没有被调用。请解释原因。这是我对含有ListView的活动Java代码:为什么上下文菜单不在ListView中显示?
public class WeatherSearch extends ListActivity implements WeatherServiceCallback {
private String query;
private OpenWeatherService service;
private ListView L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather_search);
handleIntent(getIntent());
Intent intent = getIntent();
String query = intent.getStringExtra("query");
service=new OpenWeatherService(this,getBaseContext(),"SEARCH",query,null);
service.refreshWeather();
l=(ListView)findViewById(android.R.id.list);
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
try {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search
}
}catch(Exception e){
e.printStackTrace();};
}
@Override
public void serviceSuccess(JSONObject obj) {
searchResultParser prs=new searchResultParser();
try {
prs.populate(obj);
}catch(Exception e){};
WeatherAdapter adp = new WeatherAdapter(this,R.layout.list,R.id.text,prs.getList());
l.setAdapter(adp);
registerForContextMenu(l);
}
@Override
public void serviceFailure(Exception exp) {
Toast.makeText(getApplicationContext(), "Location Search Failed ! ", Toast.LENGTH_LONG).show();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.menu_location_set_context, menu);
super.onCreateContextMenu(menu,v,menuInfo);
if (v.getId()==android.R.id.list) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.clearHeader();
menu.add(Menu.NONE, 0, 0, "Set as Default Location");
}
}
@Override
public boolean onContextItemSelected (MenuItem item){
return true;
}}
这里为WeatherSearch活动我的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.adeel.openweatherprework.WeatherSearch"
android:background="@drawable/weather_bkg1">
<LinearLayout
android:id="@+id/forecastLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_below="@+id/textView"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:dividerHeight="2dp"
android:divider="#FFFFFF" />
</LinearLayout>
</RelativeLayout>
这里是自定义列表适配器使用的列表XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical"
android:onClick="clicked"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:textColor="#FFFFFF"
android:textSize="20dp" />
</RelativeLayout>
这里是上下文菜单XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/context_menu"
android:title="@string/Loc_settings" />
</menu>
答
你夸大你的菜单第一
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.my_menu_context, menu);
// Rest of your code goes here
}
还需要太多的菜单:
my_menu_context.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_item_delete"
android:icon="@android:drawable/ic_menu_delete"
android:title="@string/delete_item" />
</menu>
答
我longClick事件addedd这条线,并与我跑。 openContextMenu(adapterView);
与此你可以处理onlongClick事件和onContextItemSelected。
+0
建议添加一些上下文或示例代码来支持您的答案。 – ItamarG3
完成,仍然没有,代码也已经更新上面 –
你确定你的“serviceSuccess”是在onCreate的上下文中调用的? –
不完全,ServiceSuccess在执行AsyncTask后作为回调函数执行。在OnCreate方法中,在第二个最后一行; service.refreshWeather()启动一个AsyncTask作为一个新的线程,在此之后,serviceSuccess从传入的引用被回调。显然serviceSucess会在执行onCreate后调用 –