Android ArrayAdapter getView无法获得正确的位置

时间:2017-09-15 03:51:53

标签: android android-fragments android-viewpager android-arrayadapter android-gridview

我有2个片段,都使用数组适配器来显示其类别。第一个片段是显示项目类别,第二个片段是显示工作类别。但是,它会在两个片段中显示作业类别。我不知道哪个部分出了问题。

这是我的ViewPager,当用户选择相应的选项卡时显示该片段。当从数据库接收数据时,将调用setCategory()。

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()+"");
}
}

这是显示项目类别的片段

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());
    }
    }

这是显示职位类别的片段

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());
    }

}
}

两个片段都会调用CategoryGridAdapter来显示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;
}

我希望当用户选择作业选项卡时,当用户在视图寻呼机中选择项目选项卡和作业类别时,它将在视图寻呼机上显示项目类别,但它仅显示作业类别。有人有解决方案吗?感谢

1 个答案:

答案 0 :(得分:3)

ViewPagerAdapter课程中,您有一个List<Category> categoryList,您在其构造函数中传递给Fragment,并且每个Fragment都设置了相同的列表它的数据集。您在setCategory()update()方法中打印的日志只显示正确,因为更新是按顺序进行的,而您正在清除它们之间的列表。

不是将List传递给每个Fragment,而是让每个人创建自己的新ArrayList<Category>,并从每个构造函数中删除该参数。