首页 \ 问答 \ 使用OnItemLongClickListener删除项目后,使用自定义adater刷新列表(Refresh list with custom adater after delete an item with OnItemLongClickListener)

使用OnItemLongClickListener删除项目后,使用自定义adater刷新列表(Refresh list with custom adater after delete an item with OnItemLongClickListener)

我想在用户长按一下并刷新列表时从列表中删除项目。

当我使用此代码时,它会删除该项但不刷新列表:

codeListView.setOnItemLongClickListener (new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        String sqlite_id = ((TextView) view.findViewById(R.id.code_id)).getText().toString();
        int sql_id = Integer.valueOf(sqlite_id.toString());
        if(fav == false){
            db.saveFav(sql_id);
            text = "Zu den Favoriten hinzugefügt!";
        }else{
            db.delFav(sql_id);
            text = "Aus den Favoriten gelöscht!";
            CustomAdapterCode adapter_code = (CustomAdapterCode)codeListView.getAdapter();
            adapter_code.notifyDataSetChanged();
        }
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        return true;
    }
});

现在我发现这个Android如何在stackoverflow上使用longclicklistener从列表中删除项目 ,但我的代码不起作用:

codeListView.setOnItemLongClickListener (new OnItemLongClickListener() {
    @Override
    public void onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        String sqlite_id = ((TextView) view.findViewById(R.id.code_id)).getText().toString();
        int sql_id = Integer.valueOf(sqlite_id.toString());
        if(fav == false){
            db.saveFav(sql_id);
            text = "Zu den Favoriten hinzugefügt!";
        }else{
            db.delFav(sql_id);
            text = "Aus den Favoriten gelöscht!";
            CustomAdapterCode adapter_code = (CustomAdapterCode)codeListView.getAdapter();
            adapter_code.notifyDataSetChanged();
        }
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();               
    }
});

CustomAdapter:

private class CustomAdapterCode extends ArrayAdapter<HashMap<String, Object>>{
    public CustomAdapterCode(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
        super(context, textViewResourceId, Strings);
    }
    private class ViewHolder{
        TextView code_id, code_layout, name_layout, prio1, prio2, prio3;
    }

    ViewHolder viewHolder;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){        
            convertView = inflater.inflate(R.layout.code_list_item, parent, false);
            viewHolder=new ViewHolder();

            viewHolder.code_id=(TextView) convertView.findViewById(R.id.code_id);
            viewHolder.code_layout=(TextView) convertView.findViewById(R.id.code_layout);
            viewHolder.name_layout=(TextView) convertView.findViewById(R.id.name_layout);
            viewHolder.prio1=(TextView) convertView.findViewById(R.id.prio1);
            viewHolder.prio2=(TextView) convertView.findViewById(R.id.prio2);
            viewHolder.prio3=(TextView) convertView.findViewById(R.id.prio3);

            convertView.setTag(viewHolder);

        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }

        viewHolder.code_id.setText(codeList.get(position).get("_id").toString());
        viewHolder.code_layout.setText(codeList.get(position).get("code").toString());
        viewHolder.name_layout.setText(codeList.get(position).get("name").toString());
        viewHolder.prio1.setText(codeList.get(position).get("prio1").toString());
        viewHolder.prio2.setText(codeList.get(position).get("prio2").toString());
        viewHolder.prio3.setText(codeList.get(position).get("prio3").toString());


        return convertView;
    }
}   

Eclipse说:

此行有多个标记

  • 实现android.widget.AdapterView.OnItemLongClickListener.onItemLongClick
  • 返回类型与AdapterView.OnItemLongClickListener.onItemLongClick(AdapterView,View,int,long)不兼容

I want to delete items from a list when the user makes a long click on it and refresh the list.

When I use this code, it delete the item but don't refresh the list:

codeListView.setOnItemLongClickListener (new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        String sqlite_id = ((TextView) view.findViewById(R.id.code_id)).getText().toString();
        int sql_id = Integer.valueOf(sqlite_id.toString());
        if(fav == false){
            db.saveFav(sql_id);
            text = "Zu den Favoriten hinzugefügt!";
        }else{
            db.delFav(sql_id);
            text = "Aus den Favoriten gelöscht!";
            CustomAdapterCode adapter_code = (CustomAdapterCode)codeListView.getAdapter();
            adapter_code.notifyDataSetChanged();
        }
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        return true;
    }
});

Now I found this Android how to delete items from list with longclicklistener on stackoverflow, but my code doesn't work:

codeListView.setOnItemLongClickListener (new OnItemLongClickListener() {
    @Override
    public void onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        String sqlite_id = ((TextView) view.findViewById(R.id.code_id)).getText().toString();
        int sql_id = Integer.valueOf(sqlite_id.toString());
        if(fav == false){
            db.saveFav(sql_id);
            text = "Zu den Favoriten hinzugefügt!";
        }else{
            db.delFav(sql_id);
            text = "Aus den Favoriten gelöscht!";
            CustomAdapterCode adapter_code = (CustomAdapterCode)codeListView.getAdapter();
            adapter_code.notifyDataSetChanged();
        }
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();               
    }
});

CustomAdapter:

private class CustomAdapterCode extends ArrayAdapter<HashMap<String, Object>>{
    public CustomAdapterCode(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
        super(context, textViewResourceId, Strings);
    }
    private class ViewHolder{
        TextView code_id, code_layout, name_layout, prio1, prio2, prio3;
    }

    ViewHolder viewHolder;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){        
            convertView = inflater.inflate(R.layout.code_list_item, parent, false);
            viewHolder=new ViewHolder();

            viewHolder.code_id=(TextView) convertView.findViewById(R.id.code_id);
            viewHolder.code_layout=(TextView) convertView.findViewById(R.id.code_layout);
            viewHolder.name_layout=(TextView) convertView.findViewById(R.id.name_layout);
            viewHolder.prio1=(TextView) convertView.findViewById(R.id.prio1);
            viewHolder.prio2=(TextView) convertView.findViewById(R.id.prio2);
            viewHolder.prio3=(TextView) convertView.findViewById(R.id.prio3);

            convertView.setTag(viewHolder);

        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }

        viewHolder.code_id.setText(codeList.get(position).get("_id").toString());
        viewHolder.code_layout.setText(codeList.get(position).get("code").toString());
        viewHolder.name_layout.setText(codeList.get(position).get("name").toString());
        viewHolder.prio1.setText(codeList.get(position).get("prio1").toString());
        viewHolder.prio2.setText(codeList.get(position).get("prio2").toString());
        viewHolder.prio3.setText(codeList.get(position).get("prio3").toString());


        return convertView;
    }
}   

Eclipse says:

Multiple markers at this line

  • implements android.widget.AdapterView.OnItemLongClickListener.onItemLongClick
  • The return type is incompatible with AdapterView.OnItemLongClickListener.onItemLongClick(AdapterView, View, int, long)

原文:https://stackoverflow.com/questions/19697198
更新时间:2022-05-23 21:05

最满意答案

只需添加一个Where

var ilceNufus = doc.DocumentNode.SelectNodes("//*[@id='Table_01']/tr[2]/td[1]/table/tr/td/table[5]/tr/td[2]/table[1]/tr/td[3]/table[2]/tr")
                .Skip(1)
                .Select(td => td.Elements("td").Select(row => row.InnerText).ToList())
                .Where(tds => tds.Count == 4)
                .Select(td => new { Yil = td[0], Toplam = td[1], Sehir = td[2], Koy = td[3] }).ToList();

Just add a Where

var ilceNufus = doc.DocumentNode.SelectNodes("//*[@id='Table_01']/tr[2]/td[1]/table/tr/td/table[5]/tr/td[2]/table[1]/tr/td[3]/table[2]/tr")
                .Skip(1)
                .Select(td => td.Elements("td").Select(row => row.InnerText).ToList())
                .Where(tds => tds.Count == 4)
                .Select(td => new { Yil = td[0], Toplam = td[1], Sehir = td[2], Koy = td[3] }).ToList();

相关问答

更多
  • 第二个参数是可选的原因是文本隐含某些 SyntaxKind值。 例如,如果您为第一个参数传递SyntaxKind.TrueLiteral ,则可以省略第二个参数。 但是,当根据第一个参数没有合理的第二个参数默认值时,我们抛出ArgumentOutOfRangeException 。 在你的例子中,你可以创建表达式: Syntax.LiteralExpression(SyntaxKind.CharacterLiteralExpression, Syntax.Literal('a')) The reason t ...
  • 你的问题接缝是由Table对象造成的,我认为它是一个DataTable,所以如果你添加Table = new DataTable; 行Table.Load(Data); 这是一种做好清理工作的方法 string sql = String.Format("SELECT ({0}) FROM {1};", String.Join(", ", columnNames), TableName); Your problem seams to be caused by the Table object, which i ...
  • 我认为问题在于[索引]。 如果要选择特定行的值,可以执行以下操作。 protected void Btn1_Click(object sender, EventArgs e) { Button btn = (Button)sender; GridViewRow gvRow = (GridViewRow)btn.NamingContainer; string rute = gvRow.Cells[0].Text; string rute1 = gvRow.Cells[1].Te ...
  • 如果符号不正确,调试器可能会显示错误的源代码行。 在异常中验证堆栈跟踪中的异常源。 Debugger can show you wrong line of source code if symbols are incorrect. Verify your exception source from stack trace in the exception.
  • 您使用的XPath始终始于文档的根目录。 SelectSingleNode("//a")表示从文档的根目录开始,在文档中a任意位置找到第a ; 这就是为什么它抓住首页链接。 如果你想从当前节点开始,你应该使用. 选择。 SelectSingleNode(".//a")意味着找到当前节点下方的第a 。 所以你的代码看起来像这样: string url = contentDiv.SelectSingleNode(".//tr[@class='blueRow']") .Sel ...
  • 只需添加一个Where var ilceNufus = doc.DocumentNode.SelectNodes("//*[@id='Table_01']/tr[2]/td[1]/table/tr/td/table[5]/tr/td[2]/table[1]/tr/td[3]/table[2]/tr") .Skip(1) .Select(td => td.Elements("td").Select(row => row.InnerText).To ...
  • 发生这种情况是因为您正在向StartNew传递Lambda表达式 ,该表达式隐式捕获您的i变量。 这种效果被称为关闭 。 为了获得正确的行为,您必须制作索引的本地副本: for (int i = 0; i < music.Count; i++) { var currentIndex = i; downloadTasks[i] = Task.Factory.StartNew(() => Download ...
  • 你必须使用//不是/ : //table[@class='borders']//tbody//tr//td 它为您提供所有行。 You have to use // not /: //table[@class='borders']//tbody//tr//td It gives you all rows.
  • 我的答案不是我希望学习的......但它解决了它。 我仍然想知道如何使用HTMLnode的搜索参数内选择... 我暂时使用LINQ ...不知道哪个更好或什么... var querylist3 = from list in doc.DocumentNode.SelectNodes("//ul[@class='address label-value']").Cast() from row in list.SelectNodes("li").Wh ...
  • 不要这样做。 例外应该是例外 。 你有一切手段来防止这种特殊情况,你绝对应该这样做。 invaderA = invadersShooting[InvadorNumberA]; invaderA = invadersShooting[0]; 在第一种情况下, InvadorNumberA可以是0到4之间的任何值。检查列表中是否至少有InvadorNumberA + 1元素, 然后尝试从中获取元素。 不要依赖例外来纠正你的课程。 InvadorNumberA ,也许InvadorNumberA实际上应该被限 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。