Skip to content

Commit

Permalink
Allow user to delete single entry from database
Browse files Browse the repository at this point in the history
* Also display a drawable on empty database

Signed-off-by: Adam Myczkowski <mycaxd511@gmail.com>
  • Loading branch information
Adam Myczkowski committed Nov 23, 2017
1 parent 6dee3dc commit 4cec554
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public void ReadDB(Context context, ListView listView) {
}

SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, R.layout.row, cursor, from, to, 0);
adapter.notifyDataSetChanged();

listView.setAdapter(adapter);
database.close();
Expand All @@ -74,4 +73,9 @@ public void clearDatabase() {
String clearDBQuery = "DELETE FROM "+RESULTS_TABLE_NAME;
sqLiteDatabase.execSQL(clearDBQuery);
}

public void deleteEntry(long row) {
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
sqLiteDatabase.delete(RESULTS_TABLE_NAME, RESULTS_COLUMN_ID + "=" + row, null);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.luckynumbers.mycax.luckynumbers;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

public class ResultsFragment extends Fragment {
public class ResultsFragment extends Fragment implements ListView.OnItemLongClickListener {

private ListView listView;

Expand All @@ -19,7 +23,31 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
listView = (ListView) view.findViewById(R.id.contentlist);
DataB database = new DataB(getActivity());
database.ReadDB(getActivity(), listView);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
listView.setEmptyView(imageView);
listView.setOnItemLongClickListener(this);
return view;
}

@Override
public boolean onItemLongClick(AdapterView<?> adapter, View v, int position, final long id) {
new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.ic_warning_black_24dp)
.setTitle("Delete result")
.setMessage("Are you sure you want delete this result?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
DataB database = new DataB(getActivity());
database.deleteEntry(id);
database.ReadDB(getActivity(), listView);
}

})
.setNegativeButton("No", null)
.show();
return true;
}

}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_sort_light_grey_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#bdbdbd"
android:pathData="M3,18h6v-2L3,16v2zM3,6v2h18L21,6L3,6zM3,13h12v-2L3,11v2z"/>
</vector>
8 changes: 8 additions & 0 deletions app/src/main/res/layout/fragment_results.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/contentlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:srcCompat="@drawable/ic_sort_light_grey_24dp" />
</LinearLayout>
6 changes: 3 additions & 3 deletions app/src/main/res/layout/row.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
Expand Down

0 comments on commit 4cec554

Please sign in to comment.