Commit 45962dae authored by kirk's avatar kirk

add ability to delete rows from the database

parent fae15003
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/delete_rows_entry"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/row_padding"
android:showDividers="end" >
android:padding="@dimen/row_padding" >
<TableRow
android:id="@+id/tableRow1"
<TextView
android:id="@+id/ssid_row_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:layout_height="wrap_content"
android:text="@string/ssid_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/row_padding"
android:text="@string/ssid_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/ssid_row_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"
android:padding="@dimen/row_padding"
android:text="@string/ssid_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="@+id/TableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/row_padding"
android:text="@string/password_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/password_row_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"
android:padding="@dimen/row_padding"
android:text="@string/password_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="@+id/TableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/row_padding"
android:text="@string/security_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/security_row_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"
android:padding="@dimen/row_padding"
android:text="@string/security_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="@+id/TableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/row_padding"
android:text="@string/hidden_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/hidden_row_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"
android:padding="@dimen/row_padding"
android:text="@string/hidden_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="@+id/TableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/delete_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="@string/delete_label" />
</TableRow>
</TableLayout>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/delete_selected_item" android:title="@string/delete_selected_label"></item>
</menu>
......@@ -40,5 +40,6 @@
<string name="delete_selected_label">Delete Selected</string>
<string name="delete_rows_label">Database</string>
<string name="title_activity_delete_rows">Database</string>
<string name="delete_ssid_prompt">Delete %1$s?</string>
</resources>
......@@ -19,8 +19,6 @@ import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
/**
* {@link FragmentActivity} to display {@link DeleteRowsFragment}
......@@ -34,40 +32,6 @@ public class DeleteRowsActivity extends FragmentActivity {
*/
private DeleteRowsFragment deleteRowsFragment;
/**
* Inflate the options {@link Menu}
*
* @param menu
* the options {@link Menu}
*
* @see android.support.v4.app.Fragment#onCreateOptionsMenu(android.view.Menu,
* android.view.MenuInflater)
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.delete_rows, menu);
return true;
}
/**
* Handle options {@link MenuItem}
*
* @param item
* {@link MenuItem}
*
* @return <code>true</code> if and only if the event was consumed
*
* @see android.support.v4.app.Fragment#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return deleteRowsFragment.onOptionsItemSelected(item);
}
/**
* @param savedInstanceState
* saved state or <code>null</code>
......
......@@ -22,18 +22,22 @@ import java.util.Map;
import us.rader.wyfy.db.WiFiSettingsContract;
import us.rader.wyfy.db.WifiSettingsDatabaseHelper;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.TextView;
/**
* UI to delete rows from the database
......@@ -135,24 +139,34 @@ public class DeleteRowsFragment extends Fragment {
@Override
protected void onPostExecute(List<Map<String, String>> result) {
String[] from = {
WiFiSettingsContract.WifiSettingsEntry.COLUMN_NAME_HIDDEN,
WiFiSettingsContract.WifiSettingsEntry.COLUMN_NAME_PASSWORD,
WiFiSettingsContract.WifiSettingsEntry.COLUMN_NAME_SECURITY,
WiFiSettingsContract.WifiSettingsEntry.COLUMN_NAME_SSID };
int[] to = { R.id.hidden_row_text, R.id.password_row_text,
R.id.security_row_text, R.id.ssid_row_text };
SimpleAdapter adapter = new SimpleAdapter(getActivity(), result,
R.layout.row_layout, from, to);
ArrayList<String> strings = new ArrayList<String>();
for (Map<String, String> entry : result) {
String ssid = entry
.get(WiFiSettingsContract.WifiSettingsEntry.COLUMN_NAME_SSID);
strings.add(ssid);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), R.layout.row_layout, R.id.ssid_row_text,
strings);
allRowsList.setAdapter(adapter);
}
}
/**
* Database selection string to match by SSID
*/
private static final String SELECT_BY_SSID = WiFiSettingsContract.WifiSettingsEntry.COLUMN_NAME_SSID
+ " LIKE ?"; //$NON-NLS-1$
/**
* {@link ListView} to populate with data from all rows in the database
*/
private ListView allRowsList;
private ListView allRowsList;
/**
* Inflate the {@link View}
......@@ -178,46 +192,65 @@ public class DeleteRowsFragment extends Fragment {
View view = inflater.inflate(R.layout.delete_rows_fragment, container,
false);
allRowsList = (ListView) view.findViewById(R.id.rows_list);
allRowsList.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int postion, long id) {
TextView text = (TextView) view
.findViewById(R.id.ssid_row_text);
deleteRow(text.getText().toString());
return true;
}
});
new QueryTask().execute();
return view;
}
/**
* Handle options {@link MenuItem}
*
* @param item
* {@link MenuItem}
*
* @return <code>true</code> if and only if the event was consumed
* Offer the user the opportunity to delete the specified row from the
* database
*
* @see android.support.v4.app.Fragment#onOptionsItemSelected(android.view.MenuItem)
* @param ssid
* the SSID of the entry to delete
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
private void deleteRow(final String ssid) {
switch (item.getItemId()) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getString(R.string.delete_ssid_prompt, ssid));
case R.id.delete_selected_item:
builder.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
deleteRows();
return true;
@Override
public void onClick(DialogInterface dialog, int which) {
default:
FragmentActivity activity = getActivity();
new WifiSettingsDatabaseHelper(activity).delete(
SELECT_BY_SSID, ssid);
dialog.dismiss();
activity.finish();
return super.onOptionsItemSelected(item);
}
});
}
builder.setNegativeButton(android.R.string.no,
new DialogInterface.OnClickListener() {
}
@Override
public void onClick(DialogInterface dialog, int which) {
/**
* Delete rows corresponding to checked entries
*/
private void deleteRows() {
dialog.dismiss();
}
});
// TODO: not yet implemented
Toast.makeText(getActivity(), "Not yet implemented", Toast.LENGTH_SHORT).show(); //$NON-NLS-1$
builder.show();
}
......
......@@ -80,6 +80,32 @@ public final class WifiSettingsDatabaseHelper extends SQLiteOpenHelper {
}
/**
* Delete any row(s) matching the given criteria
*
* @param selection
* the selection string
*
* @param selectionArgs
* string with which to replace '?' variables in
* <code>selection</code>
*/
public void delete(String selection, String... selectionArgs) {
SQLiteDatabase db = getWritableDatabase();
try {
db.delete(WiFiSettingsContract.WifiSettingsEntry.TABLE_NAME,
selection, selectionArgs);
} finally {
db.close();
}
}
/**
* Return the password stored in the database for the current value of the
* {@link WifiSettings} singleton's SSID
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment