我正在尝试在单击列表视图中的项目时启动新活动,但它似乎不起作用。具体来说,正在调用onClick方法,如果我注释掉startActivity,一切正常,但这不会改变活动。所以,我认为它与Intent上下文有关 这是代码:
public class ListViewRemovalAnimation extends Activity {
StableArrayAdapter mAdapter;
ListView mListView;
BackgroundContainer mBackgroundContainer;
boolean mSwiping = false;
boolean mItemPressed = false;
HashMap<Long, Integer> mItemIdTopMap = new HashMap<Long, Integer>();
public List<Recipe> recipes = new ArrayList<Recipe>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_deletion);
mBackgroundContainer = (BackgroundContainer) findViewById(R.id.listViewBackground);
mListView = (ListView) findViewById(R.id.listview);
mAdapter = new StableArrayAdapter(this,R.layout.opaque_text_view, cheeseList,
mTouchListener);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent = new Intent(ListViewRemovalAnimation.this, NewRecipe.class);
intent.putExtra("title", recipes.get(position).toString());
Gson gs = new Gson();
String bookmarks;
for (int i = 0; i < recipes.size(); i++) {
bookmarks = gs.toJson(recipes.get(i));
intent.putExtra("bookmarked" + i, bookmarks);
}
ListViewRemovalAnimation.this.startActivity(intent);
}
});
}
是的,我已在android清单中声明了这两项活动。
<application
android:allowBackup="true"
android:icon="@drawable/home_icon"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" >
<activity
android:name="com.example.feastbeast.MainActivity"
android:label="Home" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.feastbeast.NewRecipe"
android:label="Recipe" >
</activity>
<activity
android:name="com.example.feastbeast.ListViewRemovalAnimation"
android:label="Bookmarked" >
</activity>
</application>
如果需要,您可以在此处查看完整代码:https://github.com/david-hong/feastBeast
我检查了以下内容,但似乎都不起作用(How do i use the OnItemClickListener to start a new intent based on which item is clicked?和onItemClick, Intent, startActivity errors)。我假设第二个不起作用,因为活动扩展了ListActivity,我正在扩展Activity。
非常感谢任何帮助:)