长按删除项目

时间:2012-04-07 20:23:20

标签: android

我有ListView将所有数据保存到数据库。对于添加我有简单的按钮和textBox添加到数据库,并显示到listView。现在我想要长项目点击(按住项目)将从列表中删除所选项目。怎么可能做到这一点(实际上要求长按一下的方法)。

这是当前代码:

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

}

2 个答案:

答案 0 :(得分:8)

您希望上下文菜单基本上可以在此处看到:http://developer.android.com/guide/topics/ui/menus.html#context-menu

答案 1 :(得分:2)

您可以使用此结构:D

something.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) { ... } });
相关问题