从类中获取方法来刷新我的ListView时,出现NullPointer异常。我正在调用适配器的onclick事件中的片段的方法。大多数情况下,每件事情都很好,我只是在输入新项目/行后不能刷新列表视图。Android:notifyDataSetChanged()刷新我的ListView
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)
我用我在其他情况下ProductAdapter,会有一个第三个参数,然后... – Maki
公众ProductAdapter(上下文的背景下,名单<产品型号> productModelList,Cashier出纳员)this.context = context; this.productModelList = productModelList; this.cashier = cashier; } – Maki
我不是在我的SaleAdapter @ _ @ – Maki