Android 尝试从内部OnLongClickListener类使用AppCompat调用getActivity()
我是Android开发新手,刚刚完成了一系列教程/实验。我目前正在处理的文件位于以下位置: 我有点偏离了实验室,我使用的是AppCompat而不是ActionBarSherlock 我所讲的当前部分要求使用上下文操作模式创建一个菜单来删除实验室的笑话4.2.2。实验室提到,实现这一点的方法位于此处: 为方便起见,以下是他们建议的代码:Android 尝试从内部OnLongClickListener类使用AppCompat调用getActivity(),android,Android,我是Android开发新手,刚刚完成了一系列教程/实验。我目前正在处理的文件位于以下位置: 我有点偏离了实验室,我使用的是AppCompat而不是ActionBarSherlock 我所讲的当前部分要求使用上下文操作模式创建一个菜单来删除实验室的笑话4.2.2。实验室提到,实现这一点的方法位于此处: 为方便起见,以下是他们建议的代码: someView.setOnLongClickListener(new View.OnLongClickListener() { // Called when t
someView.setOnLongClickListener(new View.OnLongClickListener() {
// Called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
// Start the CAB using the ActionMode.Callback defined above
mActionMode = getActivity().startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
})
实验室还提到getActivity不可用,因为它们使用ActionBarSherlock,并给出了这样的提示。我认为这个提示导致了这样一个事实,即您应该使用getSherlockActivity
使用AppCompat,我不知道如何做到这一点
我的代码如下:
package edu.calpoly.android.lab3;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.view.ActionMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class AdvancedJokeList extends ActionBarActivity
{
protected static final String TAG = "lab3sahil";
// Filter Options
protected static final int FILTER_SHOW_ALL = Joke.LIKE | Joke.DISLIKE
| Joke.UNRATED;
protected static final int FILTER_LIKE = Joke.LIKE;
protected static final int FILTER_DISLIKE = Joke.DISLIKE;
protected static final int FILTER_UNRATED = Joke.UNRATED;
// Contextual Action Mode
protected ActionMode m_action_mode;
protected ActionMode.Callback action_mode_callback;
/** Contains the name of the Author for the jokes. */
protected String m_strAuthorName;
/** Contains the list of Jokes the Activity will present to the user. */
protected ArrayList<Joke> m_arrJokeList;
/**
* Contains the list of filtered Jokes the Activity will present to the
* user.
*/
protected ArrayList<Joke> m_arrFilteredJokeList;
/** Adapter used to bind an AdapterView to List of Jokes. */
protected JokeListAdapter m_jokeAdapter;
/** ViewGroup used for maintaining a list of Views that each display Jokes. */
protected ListView m_vwJokeLayout;
/**
* EditText used for entering text for a new Joke to be added to
* m_arrJokeList.
*/
protected EditText m_vwJokeEditText;
/**
* Button used for creating and adding a new Joke to m_arrJokeList using the
* text entered in m_vwJokeEditText.
*/
protected Button m_vwJokeButton;
/** Menu used for filtering Jokes. */
protected Menu m_vwMenu;
/**
* Background Color values used for alternating between light and dark rows
* of Jokes. Add a third for text color if necessary.
*/
protected int m_nDarkColor;
protected int m_nLightColor;
protected int m_nCurrentBGColor;
protected int m_nTextColor;
/**
* Context-Menu MenuItem IDs. IMPORTANT: You must use these when creating
* your MenuItems or the tests used to grade your submission will fail.
* These are commented out for now.
*/
// protected static final int FILTER = Menu.FIRST;
// protected static final int FILTER_LIKE = SubMenu.FIRST;
// protected static final int FILTER_DISLIKE = SubMenu.FIRST + 1;
// protected static final int FILTER_UNRATED = SubMenu.FIRST + 2;
// protected static final int FILTER_SHOW_ALL = SubMenu.FIRST + 3;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
initLayout();
initAddJokeListeners();
// Get Resources
Resources resources = this.getResources();
String[] jokeResources = resources.getStringArray(R.array.jokeList);
m_nDarkColor = resources.getColor(R.color.dark);
m_nLightColor = resources.getColor(R.color.light);
m_nCurrentBGColor = m_nDarkColor;
m_nTextColor = resources.getColor(R.color.text);
m_strAuthorName = resources.getString(R.string.author_name);
m_arrJokeList = new ArrayList<Joke>();
m_arrFilteredJokeList = new ArrayList<Joke>();
m_jokeAdapter = new JokeListAdapter(this, m_arrFilteredJokeList);
m_vwJokeLayout.setAdapter(m_jokeAdapter);
for (String joke : jokeResources)
{
Log.d(TAG, "Adding new joke: " + joke);
addJoke(new Joke(joke, m_strAuthorName));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.submenu_like:
applyFilter(FILTER_LIKE);
return true;
case R.id.submenu_dislike:
applyFilter(FILTER_DISLIKE);
return true;
case R.id.submenu_unrated:
applyFilter(FILTER_UNRATED);
return true;
case R.id.submenu_show_all:
applyFilter(FILTER_SHOW_ALL);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Method is used to encapsulate the code that initializes and sets the
* Layout for this Activity.
*/
protected void initLayout()
{
setContentView(R.layout.advanced);
this.m_vwJokeLayout = (ListView) findViewById(R.id.JokeListViewGroup);
this.m_vwJokeEditText = (EditText) findViewById(R.id.newJokeEditText);
this.m_vwJokeButton = (Button) findViewById(R.id.addJokeButton);
// Contextual Action Mode
action_mode_callback = new ActionMode.Callback()
{
// Called when the action mode is created; startActionMode() was
// called
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
{
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.actionmenu, menu);
return true;
}
// Called each time the action mode is shown.
// Always called after onCreateActionMode
// However, may be called multiple times if the mode is invalidated
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu)
{
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_remove:
Toast.makeText(AdvancedJokeList.this, "Removed",
Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode)
{
m_action_mode = null;
}
};
//Set your ListView to have an onItemLongClickListener
m_vwJokeLayout.setOnLongClickListener(new View.OnLongClickListener()
{
public boolean onLongClick(View v)
{
if (m_action_mode != null)
{
return false;
}
//Start the CAB using the ACtionMode.Callback
m_action_mode = getActivity().startActionMode(action_mode_callback);
v.setSelected(true);
return true;
}
}
);
}
/**
* Method is used to encapsulate the code that initializes and sets the
* Event Listeners which will respond to requests to "Add" a new Joke to the
* list.
*/
protected void initAddJokeListeners()
{
m_vwJokeButton.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
// Create joke string
String joke = m_vwJokeEditText.getText().toString();
if (joke != null && !joke.equals(""))
{
// Reset Joke EditText Box
m_vwJokeEditText.setText("");
// Add Joke
addJoke(new Joke(joke, m_strAuthorName));
// Hide Soft Keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
m_vwJokeEditText.getWindowToken(), 0);
}
}
});
}
/**
* Method used for encapsulating the logic necessary to properly add a new
* Joke to m_arrJokeList, and display it on screen.
*
* @param joke
* The Joke to add to list of Jokes.
*/
protected void addJoke(Joke joke)
{
// Add joke to Joke List
m_arrJokeList.add(joke);
m_arrFilteredJokeList.add(joke);
m_jokeAdapter.notifyDataSetChanged();
}
protected void applyFilter(int submenu_option)
{
// Clear previous filter
this.m_arrFilteredJokeList.clear();
// Apply filter
for (Joke joke : this.m_arrJokeList)
{
Log.i(TAG,
"Value: " + String.valueOf(submenu_option)
+ String.valueOf(joke.getRating()));
if (joke.getRating() == submenu_option
| submenu_option == FILTER_SHOW_ALL)
{
this.m_arrFilteredJokeList.add(joke);
}
}
// Update Adapter
m_jokeAdapter.notifyDataSetChanged();
}
}
这些方法都不起作用
任何帮助和指导都将不胜感激。谢谢。您已经参加了一项活动,只需拨打startActionMode。。。getActivity用于获取对片段中活动的引用。谢谢!这很有效。对于任何稍后遇到此问题并在这些教程之后遇到类似问题的人,下一步是更改startActionMode。。。要启动SupportActionMode。。。。我现在的问题是操作菜单没有出现。我想这是因为我不应该将OnLongClickListener设置为m_vwLayout。动作菜单应该在长时间单击各个JokeView元素时显示,但由于您可以任意创建更多JokeView,我不确定您将OnClickListener设置为哪个视图对象。您是否检查了长时间单击listener是否被触发?通过为这些视图添加android:focusable=false属性,确保行视图将其可聚焦视图(如按钮、图像按钮)设置为非聚焦。谢谢。改变了项目的焦点,这让我意识到LongClickListener没有被触发。主要问题是我没有使用长ClickListener。我使用的是一个OnLongClickListener。好消息是,因为这是一个非常耗时的小错误,我再也不会犯了。。。有希望地:。再次感谢@DashAnimal你能分享一下你所做的吗
((ActionBarActivity)getActivity()).startActionMode(action_mode_callback);
((AdvancedJokeList)getActivity()).startActionMode(action_mode_callback);
ActionBarActivity.getActivity().startActionMode(action_mode_callback);
AdvancedJokeList.getActivity().startActionMode(action_mode_callback);
getActionBarActivity.startActionMode(action_mode_callback);