如何在view-pager片段中添加和删除hashmap键/值对

时间:2016-12-07 05:48:00

标签: android

我在我的应用程序中使用带分段的view-pager。我想添加和删除哈希映射中的任何键/值,但它给我空指针异常。如果我从地图中删除最后一个键然后它工作正常,哈希映射中有5个元素。如何解决它。 以下是我的问题代码:

public class MainActivity extends AppCompatActivity {
    ViewPager viewPager;
    public PagerAdapter adapter1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.pager1);
        final HashMap<String,Integer> sliderDataList = new HashMap<String,Integer>();
        sliderDataList.put("0",R.drawable.first);
        sliderDataList.put("1",R.drawable.second);
        sliderDataList.put("2",R.drawable.three);
        sliderDataList.put("3",R.drawable.four);
        sliderDataList.put("4",R.drawable.xiaomi);
         adapter1 = new PagerAdapter(getSupportFragmentManager(), sliderDataList, MainActivity.this);
        viewPager.setAdapter(adapter1);
        viewPager.addOnPageChangeListener(new   ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
              remove("3");
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void remove(String s){
        adapter1.remove(s);
    }

    public class PagerAdapter extends FragmentStatePagerAdapter {
        private HashMap<String, Integer> hMap;
        private final Context context;

        public PagerAdapter(FragmentManager fm, HashMap<String,Integer> hMap, Context context) {
            super(fm);
            this.hMap=hMap;
            this.context=context;
        }
        @Override
        public int getItemPosition (Object object)
        {
            return POSITION_NONE;
        }

        @Override
        public Fragment getItem(int position) {
            return new SliderFragment(hMap,context,position);
        }

        @Override
        public int getCount() {
            return hMap.size();
        }

        public void remove(String s){
          hMap.remove(s);
          adapter1.notifyDataSetChanged();
                }
            }


    @SuppressLint("ValidFragment")
    public static class SliderFragment extends Fragment {

        private HashMap<String, Integer> urls;
        private int imageResourceId;
        ImageView imageView;
        private Context ctx;

        public SliderFragment(HashMap<String,Integer> urls, Context c, int pos) {
            this.urls = urls;
            this.imageResourceId = pos;
            this.ctx = c;
        }

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

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.content_main, container, false);
           imageView = (ImageView) view.findViewById(R.id.img);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(urls.get(imageResourceId +""));
            return view;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个:

Iterator<Map.Entry<String,String>> iter = TestMap.entrySet().iterator();
while (iter.hasNext()) {
    Map.Entry<String,String> entry = iter.next();
     if("Sample".equalsIgnoreCase(entry.getValue())){
        iter.remove();
   }
}

如果您只想删除空指针异常,请添加:

if (urls.get(imageResourceId + "") != null)
            imageView.setImageResource(urls.get(imageResourceId + ""));

答案 1 :(得分:0)

 public void remove(String s){

     if(sliderDataList.containskey("3"))
       {
         containskey.remove("3");
         adapter1.notifydataSetChanged();
        }

}