我想将一个项目从自定义ListView发送到另一个ListView。 我已经标记了我的意图并使StoreListAdapter类可序列化以尝试修复问题。在清单中放置android:launchmode =“singleTop”也没有用。
这肯定是我的代码。我对Android没有经验。
当我尝试从我的应用程序的主页面打开我的收藏页面时,它会崩溃,当我添加一个收藏夹时,它会添加一个,但不是正确的。然后我突然可以访问最喜欢的页面!我不知道发生了什么。
currentItem是StoreItemInfo的一个实例,我尝试将其发送到另一个ListView。谁能告诉我我做错了什么以及为什么?
我的SearchActivity类(浏览商店,这个类有效):
private void populateStoreListView() {
ArrayAdapter<StoreItemInfo> adapter= new StoreListAdapter();
ListView storeMenuList = (ListView) findViewById(R.id.listViewStores);
storeMenuList.setAdapter(adapter);
}
private class StoreListAdapter extends ArrayAdapter<StoreItemInfo> implements Serializable {
public StoreListAdapter() {
super(SearchActivity.this, R.layout.list_item_layout, storeList);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View storeItemView = convertView; // the view which has to be shown
if (storeItemView == null) { // if the view somehow isn't there, create it
storeItemView = getLayoutInflater().inflate(R.layout.list_item_layout, parent, false);
}
currentItem = storeList.get(position); // gets item from ArrayList with stores
TextView nameTxt = (TextView) storeItemView.findViewById(R.id.txtName);
nameTxt.setText(currentItem.getStoreName());
TextView addressTxt = (TextView) storeItemView.findViewById(R.id.txtAddress);
addressTxt.setText(currentItem.getStoreAddress());
TextView cityTxt = (TextView) storeItemView.findViewById(R.id.txtCity);
cityTxt.setText(currentItem.getStoreCity());
ImageView storePhoto = (ImageView) storeItemView.findViewById(R.id.imgStore);
storePhoto.setImageResource(currentItem.getStorePhoto());
final ImageButton favBtn = (ImageButton) storeItemView.findViewById(R.id.btnFavorite);
favBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { // here probably something went wrong
String message = "Your pos is: " + position;
Toast.makeText(SearchActivity.this, message, Toast.LENGTH_SHORT).show();
Intent addFavorite = new Intent(SearchActivity.this, FavoritesActivity.class);
addFavorite.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
addFavorite.putExtra("storeItem", currentItem);
startActivity(addFavorite);
}
});
return storeItemView;
}
}
收藏夹类:
public class FavoritesActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorites);
processIntent();
}
private void processIntent() {
Intent intent = getIntent();
StoreItemInfo receivedItem = (StoreItemInfo) intent.getSerializableExtra("storeItem");
favoriteList.add(receivedItem);
populateStoreListView();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
intent = getIntent();
}
private void populateStoreListView() {
ArrayAdapter<StoreItemInfo> adapter= new StoreListAdapter();
ListView storeMenuList = (ListView) findViewById(R.id.favoriteListView);
storeMenuList.setAdapter(adapter);
}
现在我最喜欢的课程应该怎么做?我有相同的适配器和布局。它必须能够将多个商店添加到收藏列表中。