gpt4 book ai didi

android - 无法在新 Activity 中传递对象数组

转载 作者:行者123 更新时间:2023-11-30 00:47:27 24 4
gpt4 key购买 nike

我根据用户输入在一个 fragment “最近”中显示一个 ListView 。现在,我想将该 ListView 作为对象数组传递给容器 Activity (主 Activity )(我正在使用界面传递数据),然后当用户单击 MainActivity 中的按钮时,该数组列表将传递给另一个 Activity ,它将再次成为显示为 ListView。当我运行应用程序时,它显示错误“应用程序已停止”。在 logcat 中也没有显示错误。请帮助。

1. My relevant code of interface in Fragment:


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view=inflater.inflate(R.layout.fragment_one, container, false); mListView=(ListView)view.findViewById(R.id.myListView);

placeList.add(new Place("Shoppers Stop (1st Floor)", "Get 10% Off on Clothes & Jewellery", "shop1"));
placeList.add(new Place("Dominos (2nd Floor)", "Get 10% Off on Chef Special Pizzas", "shop2"));
placeList.add(new Place("More MegaStore (3rd Floor)", "Wednesday Bazaar, Get min 10% Off","shop4"));
mPlaces=placeList.toArray(new Place[placeList.size()]);//Convert arraylist to array
passData(mPlaces);//Pass Array of objects for interface


mPlaceAdapter = new PlaceAdapter(getActivity(), R.layout.row, mPlaces);

if (mListView != null) {
mListView.setAdapter(mPlaceAdapter);\\Creating listview in fragment
}

// Inflate the layout for this fragment
return view;
}


//Inetrface definition
public interface OnDataPass {
public void onDataPass(Place[] data);
}

OnDataPass dataPasser;

@Override
public void onAttach(Context context) {
super.onAttach(context);
dataPasser = (OnDataPass) context;
}

@Override
public void onDetach() {
super.onDetach();
dataPasser=null;
}

public void passData(Place[] data) {
dataPasser.onDataPass(data);
}
}

2. Main Activity (Container of fragement):
@Override
public void onDataPass(Place []data) {

Collections.addAll(allOffers,data);

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch(item.getItemId()){
case R.id.all:
Intent intent = new Intent(MainActivity.this, AllOffersActivity.class);
intent.putExtra("AllOffers", (ArrayList<Place>)allOffers);
getApplication().startActivity(intent);
return true;
default: return super.onOptionsItemSelected(item);

}

}

3. AllOffersActivity Class (where again I want to show listviews):


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alloffers);
onePlaces=(ArrayList<Place>)getIntent().getSerializableExtra("AllOffers");
if(onePlaces!=null) {
mPlaces = onePlaces.toArray(new Place[onePlaces.size()]);//To store data into an array

}

allPlaceAdapter = new PlaceAdapter(AllOffersActivity.this, R.layout.row, mPlaces);
allListView=(ListView)findViewById(R.id.allListView);

if (allListView != null) {
allListView.setAdapter(allPlaceAdapter);
}

}
}

4. AndroidManifest.xml:

<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity android:name=".activity.AllOffersActivity"/>
  1. Images-i) 在 fragment 中显示 ListView 的图像,当用户单击底部的“星号”图标时,新 Activity 应显示在 fragment 中显示的 ListView

最佳答案

按照步骤传递一组用户定义的对象。

第 1 步: 公共(public)类 Place 实现 Serializable

第 2 步:在将数据传递给其他类的 Activity 中修改以下内容

Intent data = new Intent(MainActivity.this, AllOffersActivity.class);
data.putExtras("AllOffers", (Serializable) allOffers);
startActivity(data);

第三步:接收类

if(getIntent() != null) {
getIntent().getSerializableExtra("AllOffers");
}

试试这个。

关于android - 无法在新 Activity 中传递对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41548757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com