RecyclerView不在ViewPager(Fragment)中更新

时间:2017-02-08 16:19:15

标签: java android-fragments android-viewpager android-recyclerview android-tablayout

美好的一天!

我很难在片段内刷新我的recyclerview值。 这是我的设置:

  1. 活动 - > MenuItem选项,用于在指定片段中添加值

    2. 片段 - >显示当前数据(RecyclerView); RecyclerViews / Adapters的通货膨胀

  2. 我的问题是,当我添加一个新项目时,片段没有更新recyclerview数据,我需要点击或滑动到其他片段并返回片段以刷新recyclerviews。

    这是我在活动类中添加新项目对话框:

     private void createPricePerSackDialog() {
    
        ArrayAdapter<CharSequence> ppsCoconutVarietyAdapter, ppsCoconutSizeAdapter, ppsProductTypeAdapter;
    
        AlertDialog.Builder builder= new AlertDialog.Builder(this);
        LayoutInflater inflater= this.getLayoutInflater();
        final  View customPricepersackView= inflater.inflate(R.layout.priceper_sack,null);
        builder.setView(customPricepersackView);
    
        ppsNumberOfTree= (EditText)customPricepersackView.findViewById(R.id.pricepersack_numberoftrees);
        numberOfFruits= (EditText)customPricepersackView.findViewById(R.id.pricepersack_numberoffruits);
        numberOfSack= (EditText)customPricepersackView.findViewById(R.id.pricepersack_numberofsac_editext);
        priceperSack= (EditText)customPricepersackView.findViewById(R.id.pricepersack_pricepersack_editext);
    
        ppsCoconutVariety= (Spinner)customPricepersackView.findViewById(R.id.pricepersack_coconutVariety_spinner);
        ppsFruitsSize= (Spinner)customPricepersackView.findViewById(R.id.pricepersack_fruitsize_spinner);
        ppsProductType= (Spinner)customPricepersackView.findViewById(R.id.pricepersack_productType_spinner);
    
    
        ppsCoconutVarietyAdapter= ArrayAdapter.createFromResource(this,R.array.coconut_varieties, android.R.layout.simple_spinner_item);
        ppsCoconutVarietyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ppsCoconutVariety.setAdapter(ppsCoconutVarietyAdapter);
    
        ppsCoconutSizeAdapter= ArrayAdapter.createFromResource(this, R.array.coconut_sizes,android.R.layout.simple_spinner_item);
        ppsCoconutSizeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ppsFruitsSize.setAdapter(ppsCoconutSizeAdapter);
    
        ppsProductTypeAdapter= ArrayAdapter.createFromResource(this, R.array.products_type,android.R.layout.simple_spinner_item);
        ppsProductTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ppsProductType.setAdapter(ppsProductTypeAdapter);
    
        builder.setTitle("Add Products per Sack");
        builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                int numberOfTreesValue= Integer.parseInt(ppsNumberOfTree.getText().toString());
                int numberOfFruitsValue= Integer.parseInt(numberOfFruits.getText().toString());
                int numberOfSackValue= Integer.parseInt(numberOfSack.getText().toString());
                int pricePersackValue= Integer.parseInt(priceperSack.getText().toString());
    
                String choosedVariety= ppsCoconutVariety.getSelectedItem().toString();
                String choosedFruitSize=ppsFruitsSize.getSelectedItem().toString();
                String choosedProductType= ppsProductType.getSelectedItem().toString();
                String dateCreated= getCurrentTimeStamp();
                int initialIncome= numberOfSackValue*pricePersackValue;
    
                ProductsType productsType= new ProductsType(PRICE_PER_SACK);
                PricePerSack pricePerSack= new PricePerSack(productsType,numberOfTreesValue,numberOfFruitsValue,choosedVariety,choosedFruitSize,choosedProductType,numberOfSackValue,pricePersackValue,initialIncome,dateCreated);
                try {
                    pricePerSackIntegerDao= getHelper().getPricePerSackIntegerDao();
                    pricePerSackIntegerDao.create(pricePerSack);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });
    
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
    
        AlertDialog alertDialog= builder.create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
    }
    

    这是片段类:

    public class Priceperkilo extends Fragment {
    
    final static int PRICE_PER_KILO = 4;
    Dao<PricePerKilo,Integer> pricePerKiloIntegerDao;
    List<PricePerKilo> pricePerKiloList=null;
    DatabaseHelper helper=null;
    RecyclerView recyclerView;
    PricePerKiloRecyclerViewAdapter pricePerKiloRecyclerViewAdapter= null;
    
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        if (pricePerKiloIntegerDao == null) {
            try {
                pricePerKiloIntegerDao=getHelper().getPricePerKiloIntegerDao();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
           pricePerKiloList= getAllDatas();
    
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v= inflater.inflate(R.layout.fragment_priceperkilo, container, false);
    
        pricePerKiloRecyclerViewAdapter= new PricePerKiloRecyclerViewAdapter(pricePerKiloList);
        recyclerView = (RecyclerView)v.findViewById(R.id.priceperkilo_recyclerView);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager= new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(pricePerKiloRecyclerViewAdapter);
    
        pricePerKiloRecyclerViewAdapter.notifyDataSetChanged();
    
        return v;
    }
    
    private DatabaseHelper getHelper() {
        if (helper == null) {
            helper= OpenHelperManager.getHelper(getActivity(),DatabaseHelper.class);
        }
        return helper;
    }
    
    // Getting All Datas in database
    public List<PricePerKilo> getAllDatas() {
    
        pricePerKiloList= new ArrayList<>();
        try {
            pricePerKiloList= pricePerKiloIntegerDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    
        return pricePerKiloList;
    }
    
    public void updateFragmentRecyclerView() {
        if (pricePerKiloRecyclerViewAdapter != null) {
            pricePerKiloRecyclerViewAdapter.notifyDataSetChanged();
        }
    }
    

    }

0 个答案:

没有答案