在长按删除项 [英] On long click delete item

查看:280
本文介绍了在长按删除项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这是所有的数据保存到数据库的ListView。对于加入我有简单的按钮和文本框添加了数据库,并显示到ListView控件。现在我想thath长项点击(HOLD ON项目)将从列表中删除所选的项目。如何是可以做到的thath(actualy叫什么方法长按)。

下面是最新的code:

 进口的java.util.List;
进口了java.util.Random;进口android.app.ListActivity;
进口android.os.Bundle;
进口android.text.Editable;
进口android.view.View;
进口android.widget.ArrayAdapter;
进口android.widget.EditText;
进口android.widget.ListView;公共类扩展公布{ListActivity
    私人CommentsDataSource数据源;
    编辑的EditText;
    ListView控件列表;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.announce);        数据源=新CommentsDataSource(本);
        datasource.open();        清单<&评论GT;值= datasource.getAllComments();        ArrayAdapter<&评论GT;适配器=新ArrayAdapter<&评论GT;(这一点,
                android.R.layout.simple_list_item_1,价值观);
        setListAdapter(适配器);
    }
    公共无效的onClick(查看视图){
        @燮pressWarnings(未登记)
        ArrayAdapter<&评论GT;适配器=(ArrayAdapter<&评论GT;)getListAdapter();
        评论评论= NULL;
        开关(view.getId()){
        案例R.id.add:
            编辑=(EditText上)findViewById(R.id.editTxt);
            可编辑的txt =(编辑)edit.getText();
            字符串输入= txt.toString();
            注释= datasource.createComment(输入);
            adapter.add(注解);
            打破;
        }
        adapter.notifyDataSetChanged();
    }    @覆盖
    保护无效onResume(){
        datasource.open();
        super.onResume();
    }    @覆盖
    保护无效的onPause(){
        datasource.close();
        super.onPause();
    }}


解决方案

您想要的上下文菜单基本上是在这里看到:<一href=\"http://developer.android.com/guide/topics/ui/menus.html#context-menu\">http://developer.android.com/guide/topics/ui/menus.html#context-menu

I have ListView which is saving all data to database. For adding i have simple button and textBox which adds to database, and show to listView. Now i want thath on long item click (hold on item) will delete the selected item from list. How is possible to do thath (actualy what method to call for long click).

Here is current code:

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

}

解决方案

You want a context menu basically see here: http://developer.android.com/guide/topics/ui/menus.html#context-menu

这篇关于在长按删除项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆