Android:notifyDataSetChanged()刷新我的ListView

时间:2017-06-02 05:07:47

标签: android listview

从类中获取方法来刷新ListView时,我得到一个NullPointer异常。我从适配器的onclick事件中的片段调用方法。大多数情况下,每件事情都运作良好,我只是在输入新项目/行后才能刷新列表视图。

public class Cashier extends Fragment {
    public SaleAdapter saleAdapter;
    List<SaleModel> saleModelList = new ArrayList<>()

    public view onCreateView(...) {
        load_sales();
    }

    public void load_sales() {
        saleModelList.clear();
        SQLiteHelper db = new SQLiteHelper(getActivity());
        Cursor cursor = db.showSales();
        if(cursor.moveToFirst()) {
            do {
                SaleModel saleModel = new SaleModel(            
cursor.getString(cursor.getColumnIndex(SQLiteHelper.COL_PCODE)),
cursor.getString(cursor.getColumnIndex(SQLiteHelper.COL_QUANTITY)),
cursor.getString(cursor.getColumnIndex(SQLiteHelper.COL_SRP)),
cursor.getString(cursor.getColumnIndex(SQLiteHelper.COL_TOTALPRICE))
                );
                saleModelList.add(saleModel);
            } while(cursor.moveToNext());
        }
    saleAdapter = new SaleAdapter(getActivity(), R.layout.list_sale, saleModelList);
    lvSales.setAdapter(saleAdapter);
    saleAdapter.notifyDataSetChanged();
    }

    public void refreshList() {
        saleAdapter.notifyDataSetChanged();
    }
}

从适配器我必须调用load_sales()方法。

public class ProductAdapter extends RecycleView.Adapter .... {
    private List<ProductModel> productModelList = new ArrayList<>();
    private Context context;

    public ProductAdapter (Context context, List<ProductModel> productModelList) {
        this.context = context;
        this.productModelList = productModelList;
    }

    class HolderData extends RecycleView.ViewHolder {
        ...
        view.OnClickListener(....) {
            AlertDialog.Builder builder = ....
            builder.setPositiveButton() {
                Cashier cashier = new Cashier();  //Declare cashier as new Cashier class
                cashier.refreshList();  //Uses the refreshList() to reload the ListView
            }
        }
    }
}

这是我的错误

java.lang.NullPointerException
    at xxxx.fragment.Cashier.refershList(Cashier.java:112)
    at xxxx.utility.adapter.ProductAdapter$HolderData$1$1.onClick(ProductAdapter.java:121)
    at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)

3 个答案:

答案 0 :(得分:0)

它抛出空指针,因为SaleAdapter未初始化。

当您调用新的Cashier()时,收银台对象将被初始化,但Cashier类中的其他对象(例如SaleAdapter将在执行load_sales()方法后初始化)

所以最大的错误是你不应该在你的ProductAdapter中使用新的Cashier()。将SaleAdapter片段传递给ProductAdapter

public class ProductAdapter extends RecycleView.Adapter .... {
    Cashier mCashier;
    public ProductAdapter(Cashier cashier){mCashier = cashier}
    ....
    class HolderData extends RecycleView.ViewHolder {
        ...
        view.OnClickListener(....) {
            AlertDialog.Builder builder = ....
            builder.setPositiveButton() {

                mCashier.refreshList();  //Uses the refreshList() to reload the ListView
            }
        }
    }
}

并通过电话从您的片段收银台传递收银台对象     来自Cashier类的新ProductAdapter(this)

答案 1 :(得分:0)

您已在load_sales()方法中初始化适配器,因此首次必须在调用refreshlist方法之后调用load_sales方法

答案 2 :(得分:0)

  

你在代码中做错了收银员出纳=新   收银员(); 这一行

当你使用 new 关键字意味着你创建新对象所以在这里你创建了一个新的Cashier Fragment对象,当你在那个时候调用refresh方法时没有查看(当你提交事务时查看加载)适配器为空

  

你需要做什么

Prasanna Anbu 给出完美的答案我只是添加你忘了

public class ProductAdapter extends RecycleView.Adapter .... {
    Cashier mCashier;
    ArrayList<ProductDetails> mProductList 
    public ProductAdapter(Cashier cashier,ArrayList<ProductDetails> productList ){
        mCashier = cashier;
        mProductList = productList ;
     } 
    ....
    class HolderData extends RecycleView.ViewHolder {
        ...
        view.OnClickListener(....) {
            AlertDialog.Builder builder = ....
            builder.setPositiveButton() {

               methodToAddDataInDatabase(); 
               cashier.load_sales();   // this method call when you want to load new instance of adapter
              //ArrayList<ProductDetails> myObject = new ArrayList<ProductDetails>(mProductList);
             // Refresh(myObject);
            }
        }
    }

public void Refresh(ArrayList<ProductDetails> productList) {
        mProductList .clear();
        mProductList .addAll(productList);
        notifyDataSetChanged();
    }
}

这是如何加载适配器

ProductAdapter mProductAdapter = new ProductAdapter (this, mProductList );
recyclerView.setAdapter(mProductAdapter );