android – 检测长按两个项目列表视图

android – 检测长按两个项目列表视图,第1张

概述首先让我说我是 Android开发的新手,但在C#,VB方面有很强的背景. 我已经阅读了大量关于类似问题的帖子并尝试了解决方案,但它似乎永远不会奏效.我确信这是我正在做的事情,这是基于我对编程语言的无知. 所以,我得到了ListAdapter的一些代码来填充Two Item List View. import java.util.ArrayList;...public class List 首先让我说我是 Android开发的新手,但在C#,VB方面有很强的背景.

我已经阅读了大量关于类似问题的帖子并尝试了解决方案,但它似乎永远不会奏效.我确信这是我正在做的事情,这是基于我对编程语言的无知.

所以,我得到了listadapter的一些代码来填充Two Item List VIEw.

import java.util.ArrayList;...public class listadapter extends BaseAdapter {private Activity activity;  private ArrayList<Integer> image;  private ArrayList<String> List1;  private ArrayList<String> List2;  private static LayoutInflater inflater=null;  public listadapter(Activity a,ArrayList<Integer> image,ArrayList<String> List1,ArrayList<String> List2) {      this.activity = a;               this.image = image;      this.List1 = List1;      this.List2 = List2;      listadapter.inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        }  public int getCount() {    return image.size();          }  public Object getItem(int position) {      return position;  }  public long getItemID(int position) {      return position;  }  public static class VIEwHolder{      public TextVIEw text1;      public TextVIEw text2;      public ImageVIEw image;  }  public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {      VIEw vi=convertVIEw;      VIEwHolder holder;      if(convertVIEw==null){          vi = inflater.inflate(R.layout.grID_List_layout,null);          holder=new VIEwHolder();          holder.text1=(TextVIEw)vi.findVIEwByID(R.ID.item1);          holder.text2=(TextVIEw)vi.findVIEwByID(R.ID.item2);          holder.image = (ImageVIEw)vi.findVIEwByID(R.ID.icon);          vi.setTag(holder);      }      else          holder=(VIEwHolder)vi.getTag();          holder.text1.setText(this.List1.get(position));          holder.text2.setText(this.List2.get(position));          holder.image.setimageResource(this.image.get(position));          return vi;  }

}

我已经修改了上面的内容,在原始文章中使用ArrayList而不是Array.它工作正常.我想要做的是检测长按并提示用户删除.

main.xml具有此部分代码,即列表视图

<ListVIEw    androID:ID="@+ID/lvResult"    androID:layout_wIDth="fill_parent"    androID:layout_height="296dp"    androID:paddingleft="10px"    androID:paddingRight="10px" ></ListVIEw>

还有另一个grID_List_layout.xml文件,其中包含

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="fill_parent"androID:layout_height="wrap_content" ><ImageVIEw    androID:ID="@+ID/icon"    androID:layout_wIDth="72px"    androID:layout_height="wrap_content"    androID:layout_margintop="5px" /><TwolineListItem    xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:ID="@+ID/twolineList"    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"    androID:onClick="onClick"    androID:orIEntation="vertical"    androID:paddingBottom="5px"    androID:paddingtop="5px" >    <TextVIEw        androID:ID="@+ID/item1"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:textAppearance="?androID:attr/textAppearanceMedium" />    <TextVIEw        androID:ID="@+ID/item2"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:paddingtop="30px"        androID:textAppearance="?androID:attr/textAppearanceSmall" /></TwolineListItem>

在主要活动中,它被实例化(?)

ListVIEw lv = (ListVIEw) findVIEwByID(R.ID.lvResult);//listadapter is declared further uplistadapter = new listadapter(Homesteractivity.this,arrIcon,arRSSID,arrMAC);    lv.setAdapter(listadapter);

那么,我想知道的是如何拦截长按?我在哪里放各种各样的东西?即XML文件中必定存在某些内容,但哪一个?我试过了

lv.setonItemClickListener(new AdapterVIEw.OnItemClickListener() {        public voID onListItemClick(ListVIEw l,VIEw v,int position,long ID)        {            //super.onListItemClick( l,v,position,ID);            Toast.makeText(this,Toast.LENGTH_LONG).show();        }    }

和各种版本,但似乎没有什么可以取悦编译器!

如果你有时间并且感到如此倾向,我会感激一些教育,所以我可以全神贯注.

解决方法 要获得长时间点击,请使用 setOnItemLongClickListener.像这样:
lv.setonItemLongClickListener(new AdapterVIEw.OnItemLongClickListener() {    public boolean onItemLongClick(AdapterVIEw<?> parent,long ID)    {      //do stuff in here.    }});
总结

以上是内存溢出为你收集整理的android – 检测长按两个项目列表视图全部内容,希望文章能够帮你解决android – 检测长按两个项目列表视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1130773.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存