我想长按一下上下文菜单,在Search Result Activity中的ListView项目上,我使用了“registerForContextMenu(View v)”功能,并且还覆盖了上下文菜单创建方法,但是上下文菜单不是正在生成并且未调用“onCreateContextMenu”方法。请解释原因。这是我的包含ListView的活动的Java代码:
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 Activity的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>
HERE是自定义列表适配器中使用的列表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>
答案 0 :(得分:0)
你必须首先给菜单充气
@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>
答案 1 :(得分:0)
我在longClick事件中添加了这一行,它与我一起运行。 openContextMenu(适配器视图);
使用它可以处理onlongClick事件和onContextItemSelected。