我是android新手。我正在为我的应用程序使用内容提供程序,并想从我的sqlite数据库中删除一个项目。它将从数据库中删除该项目,但是recyclerview不会显示该项目已被删除,除非您返回并在删除的项目消失之前再次打开活动。我已经看过代码,但实际上我不知道问题出在哪里。
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selecte_problem_drawal);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(Color.parseColor("#FFFFFF"));
mRecyclerView = (RecyclerView) findViewById(R.id.selected_problem_recycle_view); // Instantiate Recyclerview
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setHasFixedSize(true);
selectedProblemsAdapter = new SelectedProblemsAdapter(this, null);
mRecyclerView.setAdapter(selectedProblemsAdapter);
getLoaderManager().initLoader(SELECTED_PROBLEMS_LOADER_ID, null,new CartLoader());
payForSelectedProblems = (Button)findViewById(R.id.payForSelectedProblems);
payForSelectedProblems.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (total > 0) {
Preferences.setDefaults("total", String.valueOf(total), getApplicationContext());
Intent intent = new Intent(SelectedProblems.this, SecondPayment.class);
startActivity(intent);
}else{
AlertDialog.Builder alert = new AlertDialog.Builder(SelectedProblems.this);
alert.setTitle("Alert");
alert.setMessage("Kindly Select a problem to continue...");
alert.setPositiveButton("OK",null);
alert.show();
}
}
});
}
public class CartLoader implements LoaderManager.LoaderCallbacks<Cursor> {
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return new CursorLoader(
SelectedProblems.this,
TranxavContract.CartEntries.CONTENT_URI,
SELECTED_PROBLEMS,
null,
null,
null
);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
selectedProblemsAdapter.update(cursor);
total = getSelectedProblemTotal(cursor);
problemSelectedTotal.setText(CurrencyFormatter.currencyFormat(String.valueOf(total)));
}
@Override
public void onLoaderReset(Loader<Cursor> loader){
selectedProblemsAdapter.update(null);
}
public double getSelectedProblemTotal(Cursor cursor){
while (cursor.moveToNext()){
total = total + Double.parseDouble(cursor.getString(SelectedProblems.PRICE));
}
return total;
}
}
适配器
public SelectedProblemsAdapter(Context context, Cursor cursor) {
this.context = context;
this.cursor = cursor;
}
@NonNull
@Override
public SelectedProblemsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.activity_selected_problems_content, null, false);
return new SelectedProblemsViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SelectedProblemsViewHolder holder, final int position) {
if (this.cursor != null){
this.cursor.moveToPosition(position);
priceIndex = cursor.getColumnIndex(TranxavContract.CartEntries.PRICE);
problemsIndex = cursor.getColumnIndex(TranxavContract.CartEntries.PROBLEMS);
id = cursor.getString(idIndex);
problems = cursor.getString(problemsIndex);
price = cursor.getString(priceIndex);
formatted_price = CurrencyFormatter.currencyFormat(price);
holder.selectedProblemPrice.setText(formatted_price);
holder.selectedProblems.setText(problems);
holder.remove_problem_from_cart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context.getContentResolver().delete(
TranxavContract.CartEntries.CONTENT_URI,
TranxavContract.CartEntries.PROBLEMS + " = ?",
new String[] { problems }
);
Toast.makeText(context, problems + " Item Removed from Problem Selected" + "with postion " + position, Toast.LENGTH_SHORT).show();
notifyItemRemoved(position);
notifyDataSetChanged();
}
});
}
}
@Override
public int getItemCount() {
return (null != cursor ? cursor.getCount(): 0);
}
public void update(Cursor cursor) {
this.cursor = cursor;
notifyDataSetChanged();
}
class SelectedProblemsViewHolder extends RecyclerView.ViewHolder{
TextView selectedProblems, selectedProblemPrice, selectedProblemTotal;
Button remove_problem_from_cart;
public SelectedProblemsViewHolder(View itemView) {
super(itemView);
selectedProblems = itemView.findViewById(R.id.selected_problems);
selectedProblemPrice = itemView.findViewById(R.id.selected_problem_price);
selectedProblemTotal = itemView.findViewById(R.id.selectedProblemTotal);
remove_problem_from_cart = itemView.findViewById(R.id.remove_problem_from_cart);
}
}
任何可以帮助我的人。
答案 0 :(得分:1)
对于每次删除,请像这样更新适配器
selectedProblemsAdapter.notifydatasetchanged();
答案 1 :(得分:0)
假设所有删除过程都是正确的,并且您希望在RecyclerView的整个列表中显示动画。
代替使用 notifyItemRemoved(位置) 您可以实现
notifyItemRangeChanged(position, getItemCount());
notifyItemRangeChanged()通知RecyclerView Adapter适配器中元素的位置已从位置更改。
Here是给您的参考。
希望有帮助!
答案 2 :(得分:0)
可能是问题所在,因为您删除了db中的记录后尚未重新加载列表。您可以尝试方法getLoaderManager().restartLoader(int id, Bundle args, LoaderCallbacks<D> callback)
,该方法应重新启动查询并从数据库更新它。但愿如此。在here
答案 3 :(得分:0)
删除后,调出您的显示方法
(如果selectedProblemsAdapter.notifydatasetchanged();
无法使用此方法)