程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> listview-ListView Items 的點擊問題

listview-ListView Items 的點擊問題

編輯:編程綜合問答
ListView Items 的點擊問題

ListView獲取信息保存在本地數據庫然後再顯示信息。我想點擊listview的items時刪除它們,如何實現?

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>

最佳回答:


在程序中重寫SimpleCursorAdapter.setViewBinder(),使用數據庫中的 ID 來設置 ListView 裡 Views 的 Tag,刪除 setOnItemClickListener() 中數據庫的 ID, 並刷新 Adapter。

 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); 
    } 
} 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved