就像标题所说,我的setonItemClickListener不起作用.我查看了迄今为止我所看到的所有内容,并且无法找到我的错误.
这是代码:
这是有问题的课程.它不是主类,而是从意图调用:
package...;import androID.os.Bundle;import androID.app.Activity;import androID.content.Context;import androID.content.Intent;import androID.util.Log;import androID.vIEw.Menu;import androID.vIEw.VIEw;import androID.@R_419_5537@.AdapterVIEw;import androID.@R_419_5537@.listadapter;import androID.@R_419_5537@.ListVIEw;public class GroupActivity extends Activity { String group_ID = ""; Context context; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_group); context = this; Intent intent = getIntent(); group_ID = intent.getStringExtra("group_ID"); Log.i("bla", "Launched GroupActivity for group_ID " + group_ID); final SubAdapter adapter = new SubAdapter(this, group_ID); ListVIEw lv = (ListVIEw) findVIEwByID(R.ID.ListVIEw1); lv.setClickable(true); lv.setAdapter(adapter); lv.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> av, VIEw v, int IDx, long lIDx) { Log.i("print here", "blaa " + IDx); } }); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}
这是SubAdapter类:
package...;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import androID.app.Activity;import androID.content.Context;import androID.content.Intent;import androID.graphics.Bitmap;import androID.graphics.BitmapFactory;import androID.graphics.drawable.BitmapDrawable;import androID.graphics.drawable.Drawable;import androID.text.format.DateUtils;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.@R_419_5537@.BaseAdapter;import androID.@R_419_5537@.ImageVIEw;import androID.@R_419_5537@.TextVIEw;public class SubAdapter extends BaseAdapter { private static final int thumb_wIDth = 128; private static final int thumb_hight = 128; private static Bitmap default_thumb = null; private static LayoutInflater inflater = null; private final String group_ID_; private final Activity activity; private final SubAdapter t = this; // used to keep selected position in ListVIEw private int selectedPos = -1; // init value for not-selected public SubAdapter(Activity a, final String group_ID) { Drawable d = a.getResources().getDrawable(R.drawable.ic_contact_picture); default_thumb = ((BitmapDrawable) d).getBitmap(); activity = a; group_ID_ = group_ID; inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @OverrIDe public VIEw getVIEw(int IDx, VIEw convertVIEw, VIEwGroup parent) { Log.i("bla2", "getting vIEw with index " + IDx); VIEw vi = convertVIEw; if (convertVIEw == null) { vi = inflater.inflate(R.layout.sub_List_item, null); vi.setClickable(true); } TextVIEw tv1 = (TextVIEw)vi.findVIEwByID(R.ID.textVIEw1); TextVIEw tv2 = (TextVIEw)vi.findVIEwByID(R.ID.textVIEw2); ImageVIEw img = (ImageVIEw)vi.findVIEwByID(R.ID.imageVIEw1); tv1.setText("first name" + " " + "last name"); tv2.setText("some date"); // put thumbnail Bitmap thumb = default_thumb; img.setimageBitmap(thumb); Log.i("bla3", "index is " + IDx + "selected index is " + selectedPos); if(selectedPos == IDx){ Log.i("bla4", "insIDe if"); } return vi; } public voID setSelectedposition(int pos){ selectedPos = pos; // inform the vIEw of this change notifyDataSetChanged(); } @OverrIDe public voID notifyDataSetChanged() { super.notifyDataSetChanged(); }}
这是activity_group xml:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context=".GroupActivity" > <ListVIEw androID:ID="@+ID/ListVIEw1" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:clickable="true" androID:focusableIntouchMode="false" androID:focusable="false" > </ListVIEw></relativeLayout>
这是sub_List_item xml:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:clickable="true" androID:focusable="false" androID:focusableIntouchMode="false"> <ImageVIEw androID:ID="@+ID/imageVIEw1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentleft="true" androID:layout_alignParenttop="true" androID:layout_margin="6dp" androID:src="@drawable/ic_launcher" /> <TextVIEw androID:ID="@+ID/textVIEw1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParenttop="true" androID:layout_margin="6dp" androID:layout_toleftOf="@+ID/togglebutton1" androID:layout_toRightOf="@+ID/imageVIEw1" androID:text="some text" androID:textAppearance="?androID:attr/textAppearance" /> <TextVIEw androID:ID="@+ID/textVIEw2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignleft="@+ID/textVIEw1" androID:layout_below="@+ID/textVIEw1" androID:layout_toleftOf="@+ID/togglebutton1" androID:text="more text" androID:textAppearance="?androID:attr/textAppearance" /> <Togglebutton androID:ID="@+ID/togglebutton1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParenttop="true" androID:layout_margin="6dp" androID:text="present" /></relativeLayout>
除了点击监听器之外,其他一切似乎都有效. SubAdapter正确打开并填充列表.
基本上我正在寻找的是获取消息“Log.i(”print here“,”blaa“IDx);”打印 – 它是ListVIEw的setonItemClickListener中的日志(见上图)
如果缺少任何其他相关代码,请与我们联系
谢谢!!!
解决方法:
你可能应该删除这个电话
vi.setClickable(真);在您的SubAdapter类中,因为您的convertVIEw将在onItemClick侦听器之前使用单击.
以上是内存溢出为你收集整理的android – ListView setOnItemClickListener无法正常工作全部内容,希望文章能够帮你解决android – ListView setOnItemClickListener无法正常工作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)