从表中删除一些行后重新加载RecyclerView

时间:2017-11-09 15:21:46

标签: android android-sqlite

因此,当我删除SQLITE数据库中的一行并且实际活动正在关闭时,我看到CardView是删除的。

我真的试图谷歌但没有改变,所以也许你可以帮助我的人。

这是SQLITE删除功能:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

        DBHandler dbHandler = new DBHandler(this);
        final Bundle extras = getIntent().getExtras();
        switch(item.getItemId()){
            case R.id.notiz_loeschen:

                dbHandler.deleteEntry(extras.getString("ID"));
                Toast.makeText(this, extras.getString("ID"), Toast.LENGTH_SHORT).show();

                finish();
                break;
            case R.id.notiz_aendern:

                break;
        }

        return super.onOptionsItemSelected(item);
 }

MyAdapter.java

 recyclerView = rootView.findViewById(R.id.recyclerview_alle_notizen);
 recyclerView.setHasFixedSize(true);
 recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
 recyclerView.setItemAnimator(new DefaultItemAnimator());


 adapter = new MyAdapter(listItems,getActivity());
 adapter.notifyDataSetChanged();
 recyclerView.setAdapter(adapter);

 recyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
                    @Override public void onItemClick(View view, int position) {
                        if (data.moveToPosition(position))
                        {
                            Intent i = new Intent(getActivity(),NotizAnzeigen.class);
                            i.putExtra("NotizName",data.getString(1));
                            i.putExtra("Notiz",data.getString(2));

                            startActivity(i);
                        }

                    }
                })
  );

这是当我点击菜单项目:

package simplylabs.com.simplynote;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

public class AlleNotizen extends Fragment {
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    private List<ListItem> listItems;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.alle_notizen, container, false);


        recyclerView = rootView.findViewById(R.id.recyclerview_alle_notizen);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());





        return rootView;
    }

    @Override
    public void onResume() {

        listItems = new ArrayList<>();
        ListItem listItem = new ListItem();


        final DBHandler db = new DBHandler(getActivity());
        final Cursor data = db.getData();



        while(data.moveToNext()){

            ListItem li = new ListItem(data.getString(0), data.getString(1),data.getString(2),data.getString(4),data.getString(3));
            listItems.add(li);

        }




        adapter = new MyAdapter(listItems,getActivity());
        adapter.notifyDataSetChanged();
        recyclerView.setAdapter(adapter);

        recyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
                    @Override public void onItemClick(View view, int position) {
                        if (data.moveToPosition(position))
                        {
                            Intent i = new Intent(getActivity(),NotizAnzeigen.class);
                            i.putExtra("NotizName",data.getString(1));
                            i.putExtra("Notiz",data.getString(2));

                            startActivity(i);
                        }

                    }
                })
        );



        super.onResume();
    }
}

MainActivity.java

before_save

请你帮帮我吗?

我已经改变了这一点,当我移动到最后一个标签时,它仍然只删除了cardview ...

ClassicRock

2 个答案:

答案 0 :(得分:3)

当您从本地存储中删除数据时,您需要通知适配器数据集已更改。为此,您可以尝试此操作。

  

从SQLite删除数据后,您需要再次读取数据并存储   它在list / arraylist变量中。

在你的适配器中创建一个像这样的函数,

tck = interpolate.bisplrep(x, y, z, s=0)
def givemeZ(x,y):
    return interpolate.bisplev(x,y,tck)

然后使用适配器变量调用该函数。

public void loadWithNewData(List<items> newCollectedData) {
    this.listItems.clear();
    this.listItems = newCollectedData;
    this.notifyDataSetChanged();
}
希望它会有所帮助。

答案 1 :(得分:0)

您正在从数据库中删除该行,但您没有更新listItems数组。您应该更新listItems,然后致电adapter.notifyDataSetChanged();