Reputation: 1564
My Listview gets information saved on a local database and displays the information. I am having altering what happens when my listview items are clicked. I want to delete them, can anybody assist me with this matter, while using my code? Thank you in Advance!
public class Notepad extends ListActivity {
public static final int INSERT_ID = Menu.FIRST;
EditText notes;
Button add;
ListView lv;
String currentDateTimeString = DateFormat.getDateInstance().format(
new Date());
private NotesDbAdapter mDbHelper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notepad_list);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
fillData();
Button add = (Button) findViewById(R.id.addNote);
// ListView lv = (ListView) findViewById(R.id.list);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
createNote();
}
});
// lv.setOnItemClickListener(new OnItemClickListener() {
//
// public void onItemClick(AdapterView<?> parent, View view,
// int position, long id) {
// // When clicked, show a toast with the TextView text
//
// try {
// Toast.makeText(getApplicationContext(),
// ((TextView) view).getText(), Toast.LENGTH_SHORT)
// .show();
//
// } catch (ClassCastException e) {
// Toast.makeText(getApplicationContext(), "Error",
// Toast.LENGTH_SHORT).show();
// }
//
// };
//
// });
}
private void createNote() {
EditText notes = (EditText) findViewById(R.id.note);
String noteName = notes.getText().toString();
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
int minutes = c.get(Calendar.MINUTE);
int hour = c.get(Calendar.HOUR);
mDbHelper.createNote(noteName + " Entered at " + hour + ":" + minutes
+ ":" + seconds, "");
fillData();
}
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
}
My ListView:
<ListView
android:id="@id/android:list"
android:layout_width="wrap_content"
android:layout_height="402dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/note" >
</ListView>
Upvotes: 1
Views: 383
Reputation: 5322
What I do in my Apps, is override the SimpleCursorAdapter.setViewBinder()
to set the Tag of Views inside the ListView with the ID (_ID) from the DB and delete this ID from the DB in the setOnItemClickListener()
and refresh the Adapter. Something like this:
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.notes_row, c, from, to);
notes.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int column)
{
TextView tv = (TextView) view;
view.setTag=cursor.getInt(cursor.getColumnIndex ("_id")); // You need to include the _id in the query
tv.setText(String.Valueof(cursor.getInt(cursor.getColumnIndex (NotesDbAdapter.KEY_TITLE ))));
return true;
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv=(TextView) view;
String ID=view.getTag();
// Delete ID from the DB
notes.notifyDataSetChanged();
};
});
setListAdapter(notes);
}
}
Upvotes: 0
Reputation: 40416
onListItemClick
you Got Position and Remove
it from Dynamic Array (Vector or List) Like vector.remove(position);
.if you want to remove it from Database ,Also remove from Database.After this You just write .....
adapter.notifyDataSetChanged();
Upvotes: 1
Reputation: 2612
You an add an onItemClickListener on your ListView just as you added on the button. If they click an item you delete that item in the local database using an sql query. I assume since you can select data you can delete it using SQL.
After that just reinitialize the list.
That's the easiest way:)
Although I think you could better use a longpress then a normal press. Just that items won't be deleted by accident
Upvotes: 0