【问题标题】:Android ArrayAdapter getView cannot get correct positionAndroid ArrayAdapter getView 无法获得正确的位置
【发布时间】:2017-09-15 03:51:53
【问题描述】:

我有 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;
}

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

【问题讨论】:

  • @Mike M. 我使用 Log.d 来检查,两个 tempList 都正确更新
  • 是的,我一开始没有看到你的update()方法,所以我删除了我的评论。
  • 哦,我现在明白了。您的两个Fragments 都使用您在ViewPagerAdapter 中定义的相同List&lt;Category&gt; categoryList,并传入它们的构造函数。不要那样做。只需在每个中创建一个新的ArrayList&lt;Category&gt;,然后从构造函数中删除该参数(无论如何,它不应该存在)。
  • 效果很好,谢谢!你介意发布你的答案吗?那我就可以接受了。

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


【解决方案1】:

ViewPagerAdapter 类中,您有一个List&lt;Category&gt; categoryList,您将在其构造函数中将其传递给Fragment,并且每个Fragment 都将相同的列表设置为其数据集。您在 setCategory()update() 方法中打印的日志似乎只是正确的,因为更新是按顺序进行的,而您正在清除中间的列表。

与其将List 传递给每个Fragment,不如让每个人创建自己的新ArrayList&lt;Category&gt;,并从每个构造函数中删除该参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 2013-09-06
    • 2012-09-06
    • 1970-01-01
    相关资源
    最近更新 更多