簡體   English   中英

使ListView項目可點擊麻煩

[英]Making ListView Items Clickable Trouble

我的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>

onListItemClick您獲得了位置並將其從動態數組(向量或列表)中Remove ,就像vector.remove(position);一樣vector.remove(position); 。如果要從數據庫中刪除它,也要從數據庫中刪除。之后,您只需編寫...

adapter.notifyDataSetChanged();

就像在按鈕上添加的一樣,在ListView上添加onItemClickListener。 如果他們單擊某個項目,則可以使用sql查詢在本地數據庫中刪除該項目。 我想因為您可以選擇數據,所以可以使用SQL刪除它。

之后,只需重新初始化列表即可。

那是最簡單的方法:)

盡管我認為您可以使用長按而不是普通按。 只是這些項目不會被意外刪除

我在我的Apps中所做的是重寫SimpleCursorAdapter.setViewBinder()以從數據庫中使用ID(_ID)設置ListView中的視圖標記,並在setOnItemClickListener()從數據庫中刪除此ID並刷新適配器。 像這樣:

        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); 


    } 
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM