无法在新活动中传递对象数组

时间:2017-01-09 13:05:14

标签: android listview android-fragments

我根据用户输入在一个片段“Recent”中显示列表视图。现在,我想将listview作为对象数组传递给容器活动(主要活动)(我使用接口传递数据),然后当用户单击MainActivity中的按钮时,该arraylist将被传递到另一个活动,它将再次出现显示为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. 图像-i)图像以片段形式显示列表视图,当用户点击底部的“星”图标时,新活动应显示以片段显示的列表视图

2 个答案:

答案 0 :(得分:1)

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

第1步: public class Place implements Serializable

第2步:修改您将活动数据传递到其他类的活动中的以下内容

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

第3步:接收课程

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

试试这个。

答案 1 :(得分:0)

您可以使用Bundle将数组对象从一个片段传递到其他活动

Bundle bundle = new Bundle();

bundle.putStringArray(mMyList,“my_list_key”);

fragment.setArguments(束);

check this