得到空的arraylist片段活动

时间:2016-01-11 19:18:40

标签: android android-fragments gridview arraylist

我正在从gridviewadapter向片段发送arraylist,但数据未显示。在将arraylist发送到活动时gridview代码正在工作,所以问题在于片段

这是网格视图适配器

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    System.out.println("entering adapter1");

    View row = convertView;
  final  ViewHolder holder;


    if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(mcontext);
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.imageTitle = (TextView) row.findViewById(R.id.text);
        holder.imageView = (ImageView) row.findViewById(R.id.imageView);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }
    final Listitem item = getItem(position);
    System.out.println("item.getUrl() ");
    System.out.println(item.getUrl());
    Picasso.with(mcontext).setIndicatorsEnabled(true);
    holder.imageTitle.setText(item.getId());

   /* Picasso.
            with(mcontext).
            load(item.getUrl())
            .placeholder(R.drawable.logo)
            .fit()
            .noFade()
            .into(holder.imageView)
            ;*/
    //int maxSize = 4 * 1024 * 1024; // 4MiB

    //Picasso myNewPicasso = new Picasso.Builder(mcontext).memoryCache(new LruCache(maxSize)).build();

    Picasso.with(mcontext)
            .load(item.getUrl())
            .placeholder(R.drawable.logo)
            .fit()
            .noFade()
            .into(holder.imageView);
    holder.imageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            ArrayList<Listitem> personArrayList = new ArrayList<>();
            personArrayList.add(new Listitem(item.getId(), item.getUrl()));

            Intent intent = new Intent(mcontext,SingleViewActivity.class);
            intent.putExtra("Person_List", personArrayList);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mcontext.startActivity(intent);

            Log.d("OnImageButton", "Clicked");
        }
    });


    return row;
}

static class ViewHolder {
    TextView imageTitle;
    ImageView imageView;
}

这是包含视图寻呼机的片段

public class SingleViewActivity extends FragmentActivity {
    ImageView   imageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_view);
        DemoCollectionPagerAdapter      mDemoCollectionPagerAdapter =      new DemoCollectionPagerAdapter( getSupportFragmentManager());
        ViewPager    mViewPager = (ViewPager) findViewById(R.id.pager);
           imageView = (ImageView) findViewById(R.id.funnyimage);
        Bundle bundle = getIntent().getExtras();
        Log.d("s","singleview");
        mViewPager.setAdapter(mDemoCollectionPagerAdapter);
    }

    /*  if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            MyFragment myrag = new MyFragment();
            myrag.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, myrag).commit();
        }
*/
        // Since this is an object collection, use a FragmentStatePagerAdapter,
        // and NOT a FragmentPagerAdapter.

        public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
            public DemoCollectionPagerAdapter(FragmentManager fm) {
                super(fm);
                Log.d("s","adapterview");
            }

            @Override
            public Fragment getItem(int i) {
                Fragment fragment = new DemoObjectFragment();
                Bundle args = new Bundle();
                // Our object is just an integer :-P
                args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
                fragment.setArguments(args);
                return fragment;
            }

            @Override
            public int getCount() {
                return 100;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return "OBJECT " + (position + 1);
            }
        }

// Instances of this class are fragments representing a single
// object in our collection.
        public static class DemoObjectFragment extends Fragment {
            public static final String ARG_OBJECT = "object";
    ImageView   imageView;
    int imageResourceId;
    int numberSelected;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //change to avoid orientation crash
        Log.d("s","fragview");

    }
    public static Fragment newInstance(Context context,ArrayList<Listitem> personArrayList, int numberSelected) {
        DemoObjectFragment f = new DemoObjectFragment();
        Bundle args= new Bundle();

        args.putParcelableArrayList("Person_List", personArrayList);
        //args.putInt("",imageResourceId);
        f.setArguments(args);
        return f;
    }
            @Override
            public View onCreateView(LayoutInflater inflater,
                                     ViewGroup container, Bundle savedInstanceState) {
                // The last two arguments ensure LayoutParams are inflated
                // properly.

                View rootView = inflater.inflate(
                        R.layout.fragment_collection_object, container, false);
              //  Bundle args = getArguments();
             //   ((TextView) rootView.findViewById(R.id.text1)).setText(Integer.toString(args.getInt(ARG_OBJECT)));
                Bundle args= new Bundle();



                ArrayList<Listitem> personArrayList = args.getParcelableArrayList("Person_List");
                System.out.print(personArrayList);

                System.out.print("here1");

                if (personArrayList != null && !personArrayList.isEmpty()) {
                    for (Listitem person : personArrayList) {
                        Picasso.
                                with(getActivity()).
                                load(person.url)
                                .placeholder(R.drawable.logo)
                                .fit()
                                .noFade()
                                .into(imageView);
                        Log.i("PersonsActivity",String.valueOf(person.url));
                    }
                }
                return rootView;
            }
        }
    }

对我正在做什么的一点解释 在我的主要活动中,我有一个图像的网格视图,当我点击一个图像时,我得到单个图像 - 点击一个新活动。我使用parcelable arraylist将数据从gridview发送到singleview acitivity。它正在为这项活动而努力。但是当我实现了viewpager和片段和片段适配器时,没有显示我不知道为什么,

这是我的logcat,因为您注意到它没有继续进行其余操作,因此在我的代码中添加了log print

01-11 20:54:40.814 3323-3323/com.justedhak.www.i D/Activity: performCreate Call secproduct feature valuefalse
01-11 20:54:40.814 3323-3323/com.justedhak.www.i D/Activity: performCreate Call debug elastic valuetrue
01-11 20:54:40.829 3323-3323/com.justedhak.www.i D/s: fragview
01-11 20:54:40.839 3323-3323/com.justedhak.www.i D/s: fragview
01-11 20:54:40.954 3323-3323/? I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@211f949f time:9248062

这是Singleviewactivity.java中片段代码的一部分

        public static class DemoObjectFragment extends Fragment {
        public static final String ARG_OBJECT = "object";
ImageView   imageView;
int imageResourceId;
int numberSelected;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //change to avoid orientation crash
    Log.d("s","fragview");

}
public static Fragment newInstance(Context context,ArrayList<Listitem> personArrayList, int numberSelected) {
    DemoObjectFragment f = new DemoObjectFragment();
    Bundle args= new Bundle();

    args.putParcelableArrayList("Person_List", personArrayList);
    //args.putInt("",imageResourceId);
    f.setArguments(args);
    return f;
}
        @Override
        public View onCreateView(LayoutInflater inflater,
                                 ViewGroup container, Bundle savedInstanceState) {
            // The last two arguments ensure LayoutParams are inflated
            // properly.

            View rootView = inflater.inflate(
                    R.layout.fragment_collection_object, container, false);
          //  Bundle args = getArguments();
         //   ((TextView) rootView.findViewById(R.id.text1)).setText(Integer.toString(args.getInt(ARG_OBJECT)));
            Bundle args= new Bundle();



            ArrayList<Listitem> personArrayList = args.getParcelableArrayList("Person_List");
            System.out.print(personArrayList);

            System.out.print("here1");

            if (personArrayList != null && !personArrayList.isEmpty()) {
                for (Listitem person : personArrayList) {
                    Picasso.
                            with(getActivity()).
                            load(person.url)
                            .placeholder(R.drawable.logo)
                            .fit()
                            .noFade()
                            .into(imageView);
                    Log.i("PersonsActivity",String.valueOf(person.url));
                }
            }
            return rootView;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

Moudiz,

您需要更改DemoCollectionPagerAdapter构造函数以接受人员的ArrayList:

DemoCollectionPagerAdapter(FragmentManager fm, ArrayList<Listitem> personArrayList)

使用新构造函数时,请确保在首次在SinglePageActivity View中创建它时将其传递给适配器对象。

public class SingleViewActivity extends FragmentActivity {
    ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_view);

        Bundle bundle = getIntent().getExtras();

        ArrayList<Listitem> personList = getIntent().getParcelableArrayListExtra("Person_List");

        // Pass it here using your new constructor. 
        DemoCollectionPagerAdapter mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager(), personList); 

        ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
        imageView = (ImageView) findViewById(R.id.funnyimage);

        Log.d("s","singleview");
        mViewPager.setAdapter(mDemoCollectionPagerAdapter);
    }

下一部分是你要弄清楚的,因为在你在这里创建一个Fragment对象时传递你的ArrayList是相当令人困惑的:

public static Fragment newInstance(Context context,ArrayList<Listitem> personArrayList, int numberSelected) {

如果有帮助,请告诉我。