0

So when I delete a row in the SQLITE Database and the actual activity is closing, I see the CardView which is delete.

I really tried to google but nothing changed, so maybe you could help me out guys.

This is the SQLITE Delete function:

 public void deleteEntry(String ID){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "DELETE FROM " + DB_NAME + " WHERE ID = '" + ID + "'";
        Log.d("QUERY",query);
        db.execSQL(query);
        db.close();
 }

MyAdapter.java

package simplylabs.com.simplynote;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import static android.content.ContentValues.TAG;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> 
{

    public MyAdapter(List<ListItem> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
    }

    private List<ListItem> listItems;
    private Context context;




    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_design,parent,false);
        return new MyViewHolder(v);
    }





    @Override
    public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) 
{

        final ListItem list = listItems.get(position);
        holder.insert_NOTIZENNAME.setText(list.getNOTIZENNAME());
        holder.insert_NOTIZ.setText(list.getNOTIZ());
        holder.insert_TIME.setText(list.getTIME());

        if(list.getPRIORITAET().equals("Sehr wichtig")){
            holder.prioColor.setBackgroundResource(R.color.rot);
        }
        if(list.getPRIORITAET().equals("Wichtig")){
            holder.prioColor.setBackgroundResource(R.color.orange);
        }
        if(list.getPRIORITAET().equals("Nicht wichtig")){
            holder.prioColor.setBackgroundResource(R.color.gruen);
        }



    }



    @Override
    public int getItemCount() {
        return listItems.size();
    }




    public class MyViewHolder extends RecyclerView.ViewHolder{

        TextView insert_NOTIZENNAME;
        TextView insert_NOTIZ;
        TextView insert_TIME;
        View prioColor;

        public MyViewHolder(View itemView) {
            super(itemView);
            insert_NOTIZENNAME = itemView.findViewById(R.id.showNOTIZENNAME);
            insert_NOTIZ = itemView.findViewById(R.id.show_NOTIZ);
            insert_TIME = itemView.findViewById(R.id.show_DATUM);
            prioColor = itemView.findViewById(R.id.view_prioritaet);


        }

    }

}

This is when I click on the menu Item:

@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);
 }

MainActivity.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);
                        }

                    }
                })
  );

Could you please help me out please?

I've changed this, it is still only remove the cardview when i move to the last tab ...

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();
    }
}
  • I'm guessing you are saying you have ActivityA with a list of items, click on an item to go to ActivityB then delete that item on ActivityB and ActivityA still has the item? If that is the case, move your code that populates the RecyclerView to be in onResume instead of onCreate – tim.paetz Nov 09 '17 at 15:28
  • @tim.paetz I've changed the Activity A, but it is still removing when I go to the last tab and back. –  Nov 09 '17 at 15:45

2 Answers2

2

As you are deleting data from your local storage you need to notify the adapter that data set has changed.To do that you could try this.

After deleting data from SQLite you need to read data again and store it in a list/arraylist variable.

create a function inside your adapter like this,

public void loadWithNewData(List<items> newCollectedData) {
    this.listItems.clear();
    this.listItems = newCollectedData;
    this.notifyDataSetChanged();
}

And then call that function with adapter variable like this.

adapter.loadWithNewData(newDataCollectedFromDatabase);

hope it will help.

That's Enam
  • 286
  • 2
  • 3
  • 16
  • It is not working ._. I've put the function in the AdapterClass and called the function in the first Fragment. But nothing changed –  Nov 09 '17 at 15:53
  • I have called the function in the fragment cardview on click, but the problem is when i click on it it dissapears... –  Nov 09 '17 at 16:01
  • Maybe you can help with another problem. I close the Activity with finish but when I delete 2 times I see the last activity which should be finished. Any idea? –  Nov 09 '17 at 16:19
  • https://stackoverflow.com/questions/4924071/calling-finish-on-an-android-activity-doesnt-actually-finish please check this. – That's Enam Nov 09 '17 at 16:22
  • I understand now, but don't know how to fix my problem –  Nov 09 '17 at 16:27
  • Try to modify your activity onBackpress() – That's Enam Nov 09 '17 at 16:30
  • Fixed yeah :D I fixed it with Flag Clear Top –  Nov 09 '17 at 16:52
0

You are deleting the row from the database, but you are not updating your listItems array. You should update listItems and then call adapter.notifyDataSetChanged();

Daniele
  • 4,163
  • 7
  • 44
  • 95
  • I dont really know how this is working. I tried with codes from other posts, but it is not working –  Nov 09 '17 at 15:36