如何从我的自定义基本适配器删除一个项目? [英] How do I delete an item from my custom base adapter?

查看:113
本文介绍了如何从我的自定义基本适配器删除一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我扩大BaseAdapter来创建自定义列表视图行。我打开每次用户持有该行上,并提示如果他想将其删除上下文菜单。但我怎么删除行? HashMap中只有测试数据。

 私人MyListAdapter myListAdapter;
私人的ArrayList< HashMap的<字符串,字符串>>项目;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    项目=新的ArrayList< HashMap的<字符串,字符串>>();
    HashMap的<字符串,字符串> MAP1 =新的HashMap<字符串,字符串>();
    map1.put(日,2011年10月9日);
    map1.put(距离,309公里);
    map1.put(时间,1吨45分钟);
    items.add(MAP1);

    myListAdapter =新MyListAdapter(这一点,项目);
    setListAdapter(myListAdapter);
    getListView()setOnCreateContextMenuListener(本)。
}


私有类MyListAdapter扩展了BaseAdapter {

    私人上下文的背景下;
    私人的ArrayList< HashMap的<字符串,字符串>>项目;

    公共MyListAdapter(上下文的背景下,ArrayList的< HashMap的<字符串,字符串>>项目){
        this.context =背景;
        this.items =项目;
    }

    @覆盖
    公众诠释getCount将(){
        返回items.size();
    }

    @覆盖
    公共对象的getItem(INT位置){
        返回items.get(位置);
    }

    @覆盖
    众长getItemId(INT位置){
        返回的位置;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){

        查看查看= convertView;

        如果(查看== NULL){
            LayoutInflater layoutInflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            鉴于= layoutInflater.inflate(R.layout.row_log,NULL);
        }

        TextView的rowLogOverview =(TextView中)view.findViewById(R.id.rowLogOverview);

        HashMap的<字符串,字符串>项目= items.get(位置);
        rowLogOverview.setText(item.get(日));

        返回查看;
    }
}
 

解决方案

您不会从适配器删除!您可以从项目中删除!和适配器是您的项目和视图之间。从视图中可以得到的位置,并根据您可以删除项目的位置。然后适配器将刷新你的看法。

这意味着你需要做这样的事情 //我不知道的语法,但我希望你有这个想法

  items.remove(位置);
 

I am extending BaseAdapter to make a custom listview row. I have context menu that opens everytime a user holds on the row and prompts if he wants to delete it. However how do I remove the row? The hashmap is only test data.

private MyListAdapter myListAdapter;
private ArrayList<HashMap<String, String>> items;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    items = new ArrayList<HashMap<String,String>>();
    HashMap<String, String> map1 = new HashMap<String, String>();
    map1.put("date", "10/09/2011");
    map1.put("distance", "309 km");
    map1.put("duration", "1t 45min");
    items.add(map1);

    myListAdapter = new MyListAdapter(this, items);
    setListAdapter(myListAdapter);
    getListView().setOnCreateContextMenuListener(this);
}


private class MyListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<HashMap<String, String>> items;

    public MyListAdapter(Context context, ArrayList<HashMap<String, String>> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;

        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.row_log, null);
        }

        TextView rowLogOverview = (TextView) view.findViewById(R.id.rowLogOverview);

        HashMap<String, String> item = items.get(position);
        rowLogOverview.setText(item.get("date"));

        return view;
    }
}

解决方案

You do not delete from the adapter ! You delete from the items ! and the adapter is between your items and the view. From the view you can get the position and according the position you can delete items. Then the adapter will refresh you views.

That means you need to do something like this //I am not sure for the syntax but I hope you got the idea

items.remove(position);

这篇关于如何从我的自定义基本适配器删除一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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