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

前端之家收集整理的这篇文章主要介绍了android – 检测长按两个项目列表视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先让我说我是 Android开发的新手,但在C#,VB方面有很强的背景.

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

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

  1. import java.util.ArrayList;
  2. ...
  3.  
  4.  
  5. public class ListAdapter extends BaseAdapter {
  6.  
  7. private Activity activity;
  8. private ArrayList<Integer> image;
  9. private ArrayList<String> list1;
  10. private ArrayList<String> list2;
  11. private static LayoutInflater inflater=null;
  12.  
  13. public ListAdapter(Activity a,ArrayList<Integer> image,ArrayList<String> list1,ArrayList<String> list2) {
  14. this.activity = a;
  15. this.image = image;
  16. this.list1 = list1;
  17. this.list2 = list2;
  18. ListAdapter.inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  19. }
  20.  
  21. public int getCount() {
  22. return image.size();
  23. }
  24.  
  25. public Object getItem(int position) {
  26. return position;
  27. }
  28.  
  29. public long getItemId(int position) {
  30. return position;
  31. }
  32.  
  33. public static class ViewHolder{
  34. public TextView text1;
  35. public TextView text2;
  36. public ImageView image;
  37. }
  38.  
  39. public View getView(int position,View convertView,ViewGroup parent) {
  40. View vi=convertView;
  41. ViewHolder holder;
  42. if(convertView==null){
  43.  
  44. vi = inflater.inflate(R.layout.grid_list_layout,null);
  45.  
  46. holder=new ViewHolder();
  47. holder.text1=(TextView)vi.findViewById(R.id.item1);
  48. holder.text2=(TextView)vi.findViewById(R.id.item2);
  49. holder.image = (ImageView)vi.findViewById(R.id.icon);
  50.  
  51. vi.setTag(holder);
  52. }
  53. else
  54. holder=(ViewHolder)vi.getTag();
  55.  
  56. holder.text1.setText(this.list1.get(position));
  57. holder.text2.setText(this.list2.get(position));
  58. holder.image.setImageResource(this.image.get(position));
  59.  
  60. return vi;
  61. }

}

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

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

  1. <ListView
  2. android:id="@+id/lvResult"
  3. android:layout_width="fill_parent"
  4. android:layout_height="296dp"
  5. android:paddingLeft="10px"
  6. android:paddingRight="10px" >
  7. </ListView>

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

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content" >
  4.  
  5. <ImageView
  6. android:id="@+id/icon"
  7. android:layout_width="72px"
  8. android:layout_height="wrap_content"
  9. android:layout_marginTop="5px" />
  10.  
  11. <TwoLineListItem
  12. xmlns:android="http://schemas.android.com/apk/res/android"
  13. android:id="@+id/twolinelist"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:onClick="onClick"
  17. android:orientation="vertical"
  18. android:paddingBottom="5px"
  19. android:paddingTop="5px" >
  20.  
  21. <TextView
  22. android:id="@+id/item1"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:textAppearance="?android:attr/textAppearanceMedium" />
  26.  
  27. <TextView
  28. android:id="@+id/item2"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:paddingTop="30px"
  32. android:textAppearance="?android:attr/textAppearanceSmall" />
  33. </TwoLineListItem>

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

  1. ListView lv = (ListView) findViewById(R.id.lvResult);
  2. //listAdapter is declared further up
  3. listAdapter = new ListAdapter(HomesterActivity.this,arrIcon,arRSSID,arrMAC);
  4. lv.setAdapter(listAdapter);

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

  1. lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  2. public void onListItemClick(ListView l,View v,int position,long id)
  3. {
  4. //super.onListItemClick( l,v,position,id);
  5. Toast.makeText(this,Toast.LENGTH_LONG).show();
  6. }
  7. }

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

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

解决方法

要获得长时间点击,请使用 setOnItemLongClickListener.像这样:
  1. lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  2. public boolean onItemLongClick(AdapterView<?> parent,long id)
  3. {
  4. //do stuff in here.
  5. }
  6. });

猜你在找的Android相关文章