0

I am using this tutorial to make gridview: https://developer.android.com/guide/topics/ui/layout/gridview.html

I have changed the mThumbIds from array to List<Integer> and I am trying to initialize it with data that came to dataGridFragment from MainActivity via Bundle but it doesn't work. It works if I add element manually. I think that the bundle thata comes too late but have no idea how to synchronise it.

public class GameFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sudoku_game_fragment,
                container, false);
        ImageAdapter adapter = new ImageAdapter(getActivity());
        List<Integer> array ;

        GridView gridview = (GridView) view.findViewById(R.id.gridview);
        gridview.setAdapter(adapter);
        if(getArguments() != null) {
             array =getArguments().getIntegerArrayList("values");
            adapter.mThumbIds.add(0, R.drawable.zero);
            }

        adapter.notifyDataSetChanged();
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Toast.makeText(getActivity(), "" + position,
                        Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }



public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public List<Integer> mThumbIds = new ArrayList<Integer>();
  //  public Integer[] mThumbIds;

    public ImageAdapter(Context c) {
        mContext = c;






    }

    public int getCount() {
        return mThumbIds.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
    //    populateImages();
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(1, 1, 1, 1);
            imageView.setImageResource(R.drawable.one);

        } else {
            imageView = (ImageView) convertView;
        }
        //imageView.setImageResource(R.drawable.two);
       // Log.d("getView", String.valueOf(values[position]));
        imageView.setImageResource(mThumbIds.get(position));

        return imageView;
    }





}
zarcel
  • 1,211
  • 2
  • 16
  • 35
  • First of all, do you success in putting what you want in your adapter independently of your bundle (with mock data)? – Laurent Meyer Jun 29 '15 at 08:50
  • Yes, If I add it outside of the statement that gets thata from bundle it works, if I put it in there it doesn't – zarcel Jun 29 '15 at 08:53
  • Ok, what about loading the bundle directly in the adapter and call the `setAdapter()` later (and not using a useless `notifyDataSetChanged`? 2) Have you checked the data in your bundle? – Laurent Meyer Jun 29 '15 at 08:56
  • Yes I tried this already and this approach has gotten me closest – zarcel Jun 29 '15 at 08:57
  • Can you post your adapter? – Laurent Meyer Jun 29 '15 at 09:08
  • Yes I just posted it, note that it is adapter from googles tutorial that I posted at the beggining I just changed array to list – zarcel Jun 29 '15 at 09:12
  • Try to pass the array list in the constructor, that's cleaner – Laurent Meyer Jun 29 '15 at 09:15
  • Are you calling setArguments() on the Fragment right after you instantiate it? – kris larson Jun 29 '15 at 09:19
  • Yes, thats the second thing I do, strange thing is that I can print data that came in the bundle and have the add to image adapter right after it but it doesn't get called. Well I have found a workaround - I have created addItem method and I call it from activity, but I am still curious why it didn;t work – zarcel Jun 29 '15 at 09:22
  • Where are you passing the array to the adapter? – ashkhn Jun 29 '15 at 10:14

0 Answers0