首先让我说我是
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 up
- listAdapter = 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.
- }
- });