2012-04-07 36 views
6

Tôi có ListView đang lưu tất cả dữ liệu vào cơ sở dữ liệu. Để thêm tôi có nút đơn giản và textBox mà thêm vào cơ sở dữ liệu, và hiển thị cho listView. Bây giờ tôi muốn thath trên nhấp chuột dài mục (giữ trên mục) sẽ xóa các mục đã chọn từ danh sách. Làm thế nào là có thể làm điều đó (thực tế những gì phương pháp để gọi cho nhấp chuột dài).Khi nhấp chuột vào mục xóa dài

Đây là mã hiện tại:

import java.util.List; 
import java.util.Random; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ListView; 

public class Announce extends ListActivity{ 
    private CommentsDataSource datasource; 
    EditText edit; 
    ListView list; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.announce); 

     datasource = new CommentsDataSource(this); 
     datasource.open(); 

     List<Comment> values = datasource.getAllComments(); 

     ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, 
       android.R.layout.simple_list_item_1, values); 
     setListAdapter(adapter); 
    } 


    public void onClick(View view) { 
     @SuppressWarnings("unchecked") 
     ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter(); 
     Comment comment = null; 
     switch (view.getId()) { 
     case R.id.add: 
      edit = (EditText)findViewById(R.id.editTxt); 
      Editable txt=(Editable)edit.getText(); 
      String input = txt.toString();   
      comment = datasource.createComment(input); 
      adapter.add(comment); 
      break; 
     } 
     adapter.notifyDataSetChanged(); 
    } 



    @Override 
    protected void onResume() { 
     datasource.open(); 
     super.onResume(); 
    } 

    @Override 
    protected void onPause() { 
     datasource.close(); 
     super.onPause(); 
    } 

} 

Trả lời

2

Bạn có thể sử dụng contruct này: D

something.setOnLongClickListener(new OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { ... } }); 
Các vấn đề liên quan