Bhushan Patil
Bhushan Patil

Reputation: 125

data in the row is not deleted from table

when I click delete it doesn't delete or edit the row I created so my MainActivity is

    package com.frolicfreak.bhushan.memo;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

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

import static com.frolicfreak.bhushan.memo.DatabaseHelper.Table_Name;
import static com.frolicfreak.bhushan.memo.DatabaseHelper.col1;
import static com.frolicfreak.bhushan.memo.DatabaseHelper.col2;

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener{

FloatingActionButton fab;
DatabaseHelper mydb;

ListView listView;
ArrayAdapter<String> mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mydb =new DatabaseHelper(this);

    fab= (FloatingActionButton)findViewById(R.id.fab);
    fab.setOnClickListener(this);

    ListView listView = (ListView)findViewById(R.id.list);
    registerForContextMenu(listView);

}

@Override
public void onResume() {
    super.onResume();
    ArrayList<String> theList =new ArrayList<>();
    Cursor data= mydb.getAllData();
    listView = (ListView)findViewById(R.id.list);
    listView.setAdapter(null);

    if (data.getCount()==0){
        Toast.makeText(MainActivity.this,"data not inserted",Toast.LENGTH_LONG).show();
    }
    else
    {
        while(data.moveToNext()){
            theList.add(data.getString(1));
            ListAdapter listAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
            listView.setAdapter(listAdapter);
        }
    }
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    if (v.getId()==R.id.list) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch(item.getItemId()) {
        case R.id.edit:
           // int count= mydb.updaterow(getString(col2),EditText.)
            return true;
        case R.id.delete:
            int count=mydb.deleterow(getString(item.getItemId()));
            onResume();
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}


@Override
public void onClick(View v) {

    Intent intent= new Intent(this,Main2Activity.class);
    startActivity(intent);

}
}

My DataBasehelper class

package com.frolicfreak.bhushan.memo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.EditText;

import java.util.ArrayList;

/**
 * Created by BHUSHAN on 24-08-2017.
*/

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String Database_Name = "Memo.db";
public static final String Table_Name = "Memo";
public static final String col1 = "ID";
public static final String col2 = "data";

//DatabaseHelper mydb;

public DatabaseHelper(Context context) {

    super(context, Database_Name, null, 1);
    //mydb=new DatabaseHelper(context);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("create table " + Table_Name + "(ID integer primary key autoincrement, data text)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists " + Table_Name);
    onCreate(db);
}

public boolean insertData(String data, String Data) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(col2, Data);
    long result = db.insert(Table_Name, null, contentValues);
    if (result==-1)
        return false;
    else
        return true;
}

public ArrayList<String> getList(){
    ArrayList<String> taskList = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor= db.query(Table_Name,new String[]{col1},new String(col2),null,null,null,null);
    while (cursor.moveToNext()){
        int index= cursor.getColumnIndex(col1);
        taskList.add(cursor.getString(index));
    }
    cursor.close();
    db.close();
    return taskList;
}

public Cursor getAllData() {

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM " + Table_Name, null);
    return res;
}


public int deleterow(String col1) {
    String whereClause = "ID = ?";
    String[] whereArgs = {String.valueOf(col1)};

    SQLiteDatabase db = this.getWritableDatabase();

    int count = db.delete(Table_Name, whereClause, whereArgs);
    db.close();
    return count;
}

 /* public int updaterow(String oldname, String newdata){
    SQLiteDatabase db= mydb.getWritableDatabase();
    ContentValues cv=new ContentValues();
    cv.put(DatabaseHelper.col2,newdata);
    String[] whereargs={oldname};
    int count = db.update(Table_Name,cv, DatabaseHelper.col2+"=?" , whereargs);
    return count;
}*/
 }

my Main2Activity class where I edit and insert data

 package com.frolicfreak.bhushan.memo;

 import android.support.design.widget.FloatingActionButton;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.EditText;
 import android.widget.Toast;

 public class Main2Activity extends AppCompatActivity {
DatabaseHelper db;

EditText Data;
FloatingActionButton add;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    db = new DatabaseHelper(this);

    Data = (EditText)findViewById(R.id.edit);
    add =(FloatingActionButton)findViewById(R.id.add);

    AddData();
}

public void AddData(){
    add.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isInserted = 
 db.insertData(Data.getText().toString(),
                             Data.getText().toString() );
                    if (isInserted==true)
                        Toast.makeText(Main2Activity.this,"data 
 inserted",Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(Main2Activity.this,"data not 
 inserted",Toast.LENGTH_LONG).show();
                }
            }
    );
}

}

please help me I'm new to programming so I'm trying to learn here thankyou in advance and I actually need help with editing and saving the data too I wrote the code but I saved it in the comments

Upvotes: 1

Views: 64

Answers (3)

Ozgur
Ozgur

Reputation: 3766

When deleting, you are calling this

int count=mydb.deleterow(getString(item.getItemId()));
onResume();

You are not passing the id of selected item, but you are passing menu item's string fetched by getString(...) method, which is weird. There are so much unused code in your class.

I advice you to work on BaseAdapter and ListView.

Upvotes: 1

Michal Štefanec
Michal Štefanec

Reputation: 123

It's not a good programming pattern to call onResume() method. You should extract it to different method and call it in onResume().

Anyway, in this case notifyDataSetChanged() is a solution what you are looking for.

Upvotes: 0

Aniruddha Bera
Aniruddha Bera

Reputation: 417

Your delete function is working fine according to the code. Check the return is greater than 0 or not.

But you won't be able to see the changes in the ListView because you have to populate the list again.

Set an CursorAdapter since you're using a database to link to the listview and use CursorAdapter.notifyDataSetChanged()

Or create a method that will recreate the listview when a delete occurs.

ArrayList<String> COUNTRIES = new ArrayList<>();
public void refreshView() {

    MyDatabase database = new MyDatabase(ViewList.this);

    Cursor cursor = database.fetchAllData();

    COUNTRIES.clear();
    while (cursor.moveToNext()) {
        String temp = cursor.getString(cursor.getColumnIndex(database.COL1));
        COUNTRIES.add(temp);
    }
    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, COUNTRIES);
    ListView listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(arrayAdapter);

}

Upvotes: 1

Related Questions