我的列表视图获取保存在本地数据库中的信息并显示信息。我正在修改点击列表视图项目时发生的情况。我想删除它们,有人可以帮我解决这个问题,而使用我的代码?先谢谢你!制作ListView项目可点击故障
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);
}
}
我的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>
我不知道我知道你的意思 – 2012-02-28 09:57:11
我从你的问题中了解到你想要删除在ListView中点击的项目。问题是你不能从ListView链接回数据库。这是我的答案。数据库中的每个项目都有_id,并且viewBinder(最初从DB填充LV)将LV中每个项目(R.id.text1)的标签设置为这个_id。当您单击该项目时,监听器使用该项目的标签(之前设置)来引用数据库,从数据库中删除该行,然后刷新LV以获取没有单击项目的新数据 – 2012-02-28 10:18:12
好吧,谢谢,我明白了那完全,对不起。但截至目前,我在点击时访问我的列表视图项目时遇到了困难。我不知道如何访问我的列表,在我的XML。 – 2012-02-28 10:21:51