phoon
phoon

Reputation: 369

Android ArrayAdapter getView cannot get correct position

I have 2 fragments, both work with array adapter to display their category. First fragment is display item category, second fragment is display job category. However, it display the job category in both fragment. I don't know which part is going wrong.

This is my ViewPager to display the fragment when the user select the corresponding tab. The setCategory() will called when received the data from database.

public class ViewPagerAdapter extends FragmentPagerAdapter {

private List<Category> categoryList = new ArrayList();
private DisplayItemFragment itemFragment;
private DisplayJobFragment jobFragment;

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    Log.d("categoryList", categoryList.size()+" ");
    if (position ==0) {
        itemFragment = new DisplayItemFragment(categoryList);
        return itemFragment;
    }else{
        jobFragment = new DisplayJobFragment(categoryList);
        return jobFragment;
    }

}

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

@Override
public CharSequence getPageTitle(int position) {
    switch (position){
        //
        //Your tab titles
        //
        case 0:return "Item";
        case 1:return "Job";
        default:return null;
    }
}

public void setCategory(List<Category> list){
    List<Category> tempList = new ArrayList();
    for(int i=0;i<list.size();i++){
        if(list.get(i).getType().equals("item")){
            tempList.add(list.get(i));
        }
    }
    itemFragment.update(tempList);
    Log.d("this.categoryListitem",tempList.size()+"");
    tempList.clear();
    for(int i=0;i<list.size();i++){
        if(list.get(i).getType().equals("job")){
            tempList.add(list.get(i));
        }
    }
    jobFragment.update(tempList);
    Log.d("this.categoryListjob",tempList.size()+"");
}
}

This is the fragment to display item category

public class DisplayItemFragment extends Fragment {

private View rootView;
private GridView gridView;
private List<Category> categoryList;
private DisplayFragmentPresenter presenter;
private CategoryGridAdapter categoryAdapter;
private TextView text;

public DisplayItemFragment(List<Category> categoryList) {
    this.categoryList=categoryList;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

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

    rootView = inflater.inflate(R.layout.fragment_display, container, false);
    text = (TextView) rootView.findViewById(R.id.test);
    categoryAdapter = new CategoryGridAdapter(getActivity(), R.layout.fragment_display, categoryList);
    gridView = (GridView) rootView.findViewById(R.id.gridview);
    gridView.setAdapter(categoryAdapter);
    gridView.setTextFilterEnabled(true);

    //Log.d("category",categoryList.get(0).getName());
    return rootView;
}

public void update(List<Category> list) {
    this.categoryList.clear();
    for(int i=0;i<list.size();i++){
        if(list.get(i).getType().equals("item")){
            this.categoryList.add(list.get(i));
        }
    }
    text.setText("item refresh");
    for(int i=0;i<this.categoryList.size();i++){
        Log.d("categoryListitem",categoryList.get(i).getName());
    }
    }

This is the fragment to display job category

public class DisplayJobFragment extends Fragment {

private View rootView;
private GridView gridView;
private List<Category> categoryList;
private DisplayFragmentPresenter presenter;
private TextView text;
private CategoryGridAdapter categoryAdapter;

public DisplayJobFragment(List<Category> categoryList) {
    this.categoryList=categoryList;
}



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

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

    rootView = inflater.inflate(R.layout.fragment_display, container, false);
    text = (TextView) rootView.findViewById(R.id.test);
    categoryAdapter = new CategoryGridAdapter(getActivity(), R.layout.fragment_display, categoryList);
    gridView = (GridView) rootView.findViewById(R.id.gridview);
    gridView.setAdapter(categoryAdapter);
    gridView.setTextFilterEnabled(true);

    //Log.d("category",categoryList.get(0).getName());
    return rootView;
}

public void update(List<Category> list) {
    this.categoryList.clear();
    for(int i=0;i<list.size();i++){
        if(list.get(i).getType().equals("job")){
            this.categoryList.add(list.get(i));
        }
    }
    text.setText("job refresh");
    //categoryAdapter = new CategoryGridAdapter(getActivity(), R.layout.fragment_display, this.categoryList);
    for(int i=0;i<this.categoryList.size();i++){
        Log.d("categoryListjob",categoryList.get(i).getName());
    }

}
}

Both fragment will called CategoryGridAdapter to display category on gridview

public class CategoryGridAdapter extends ArrayAdapter<Category> {

private List<Category> categoryList;
private Context context;

public CategoryGridAdapter(Context context, int resource, List<Category> categoryList) {
    super(context, resource, categoryList);
    this.context = context;
    this.categoryList = categoryList;
}

@Override
public boolean isEnabled(int position) {
    return true;
}

@Override
public int getCount() {
    return ((null != categoryList) ?
            categoryList.size() : 0);
}

@Override
public Category getItem(int position) {
    return ((null != categoryList) ?
            categoryList.get(position) : null);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (null == view) {
        view = layoutInflater.inflate(R.layout.grid_category, null);
    }

    final Category category = categoryList.get(position);
    Log.d("categorygridlist",category.getName()+" "+position+ " " + categoryList.size());

    if (category != null) {
        final CardView categoryGridLayout = (CardView) view.findViewById(R.id.category_gridlayout);
        final TextView categoryName = (TextView) view.findViewById(R.id.category_name);
        final ImageView categoryIcon = (ImageView) view.findViewById(R.id.category_icon);

        categoryName.setText(category.getName());
        categoryName.setSelected(true);
        try {
            byte[] decodedString = Base64.decode(category.getImage(), Base64.DEFAULT);
            BitmapFactory.Options options=new BitmapFactory.Options();// Create object of bitmapfactory's option method for further option use
            options.inPurgeable = true; // inPurgeable is used to free up memory while required
            Bitmap decodedByte1 = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);//Decode image, "thumbnail" is the object of image file
            Bitmap decodedByte = Bitmap.createScaledBitmap(decodedByte1, 50 , 50 , true);// convert decoded bitmap into well scalled Bitmap format.

            categoryIcon.setImageBitmap(decodedByte);
        } catch (Exception e) {
            e.printStackTrace();
        }

        categoryGridLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "worked", Toast.LENGTH_LONG).show();
            }
        });
    }

    return view;
}

I expect it will display item category at view pager when user select item tab and job category at view pager when user select job tab but it only display the job category. Anyone got solutions about this? Thanks

Upvotes: 1

Views: 115

Answers (1)

Mike M.
Mike M.

Reputation: 39191

In the ViewPagerAdapter class, you have a List<Category> categoryList that you're passing to both Fragments in their constructors, and each Fragment is setting that same list as its dataset. The log prints you have in the setCategory() and update() methods only seem correct, because the updates are happening sequentially, and you're clearing the list in between.

Instead of passing that List to each Fragment, just have each one create its own new ArrayList<Category>, and remove that parameter from each constructor.

Upvotes: 3

Related Questions