Android McClickListener未在listview上工作
我想从listview中打开新活动。在listview中打开新活动。我尝试过setOnclick listener,但无法正常工作。 适配器代码如下。thd代码中没有错误Android McClickListener未在listview上工作,android,sdk,Android,Sdk,我想从listview中打开新活动。在listview中打开新活动。我尝试过setOnclick listener,但无法正常工作。 适配器代码如下。thd代码中没有错误 公共类MyOffers扩展活动{ 静态最终字符串标记=MyOffers.class.getSimpleName(); 私有视图父视图; 公共字符串separatecheck; 私人回收站; 私有适配器列表基本mAdapter; ListView lv1; @凌驾 创建时受保护的void(Bundle savedInstance
公共类MyOffers扩展活动{
静态最终字符串标记=MyOffers.class.getSimpleName();
私有视图父视图;
公共字符串separatecheck;
私人回收站;
私有适配器列表基本mAdapter;
ListView lv1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u my\u dth\u提供);
ArrayList image_details=getListData();
lv1=(ListView)findViewById(R.id.dth_列表);
lv1.setAdapter(新的AdapterThlist(此,图像_详细信息));
Log.e(标签为“hfhfhhfhfhfofferrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr”);
//在单击时侦听单个列表项
lv1.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
Log.e(标签“HFHFHFHFHFRFRRRR”);
//对象o=lv1.getItemAtPosition(位置);
//NewsItem newsData=(NewsItem)o;
Intent Intent=新的Intent(getApplicationContext(),Dispoffer.class);
星触觉(意向);
//选定项目
}
});
}
适配器:
public class AdapterDthList extends BaseAdapter {
private ArrayList<NewsItemDth> listData;
private LayoutInflater layoutInflater;
public AdapterDthList(Context aContext, ArrayList<NewsItemDth> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView) convertView.findViewById(R.id.title);
holder.reporterNameView = (TextView) convertView.findViewById(R.id.reporter);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.headlineView.setText("Amount: "+listData.get(position).getReporterName()+"/-");
holder.reporterNameView.setText( listData.get(position).getHeadline());
holder.reportedDateView.setText(listData.get(position).getDate());
return convertView;
}
static class ViewHolder {
TextView headlineView;
TextView reporterNameView;
TextView reportedDateView;
}
}
公共类适配器列表扩展了BaseAdapter{
私有数组列表数据;
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
公共适配器列表(上下文、文本、数组列表数据){
this.listData=listData;
layoutInflater=layoutInflater.from(文本);
}
@凌驾
public int getCount(){
返回listData.size();
}
@凌驾
公共对象getItem(int位置){
返回listData.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
convertView=LayoutFlater.充气(R.layout.list\u row\u layout,null);
holder=新的ViewHolder();
holder.headlineView=(TextView)convertView.findViewById(R.id.title);
holder.reporterNameView=(TextView)convertView.findViewById(R.id.reporter);
holder.reportedDateView=(TextView)convertView.findViewById(R.id.date);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.headlineView.setText(“金额:+listData.get(position.getReporterName()+”/-”;
holder.reporterNameView.setText(listData.get(position.getHeadline());
holder.reportedDateView.setText(listData.get(position.getDate());
返回视图;
}
静态类视窗夹{
文本视图标题视图;
TextView报告erNameView;
TextView reportedDateView;
}
}
您似乎在使用getApplicationContext()
而不是活动
上下文..尝试更改
Intent-Intent=newintent(getApplicationContext(),Dispoffer.class);
到
如果列表中的行项包含可聚焦或可单击的视图,则
OnItemClickListener
将不起作用
将android:genderantfocusability=“blocksDescendants”添加到行项目的根布局中。下面是一个示例
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your widgets goes here
</LinearLayout>
//你的小部件在这里
我没有显示任何错误,只显示“lv1.setOnItemClickListener(新的AdapterView.OnItemClickListener()”发布您的项目Xml和AdapterThlist.java不起作用。我认为您的Xml文件中有错误,或者您可能在适配器中定义了某些内容,因此请使用这两个文件更新您的问题。@ChetanShanbhag告诉大家发布您的Xml和适配器文件。没有完整的代码,任何人都无法理解您的问题。请尽快更新@PratikButani已经发布了代码,如果这不能解决您的问题。请使用适配器和项目XML编辑您的问题,在其中的XML文件??列出latout或适配器布局??适配器的布局XML文件。@Chetan Shanbhag您需要添加
android:genderantFocusability=“blocksDescendants”
用于创建列表视图的每一行的xml文件根布局中的行。在本例中,它用于这一行。convertView=layoutInflater.inflate(R.layout.list\u row\u layout,null)
。您需要将它添加到您的列表行布局.xml
文件中。如果您正在使用,您将在Android Studio的res>布局目录下找到它。
Intent intent=new Intent(MyOffers.this,Dispoffer.class);
startActivity(intent);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your widgets goes here
</LinearLayout>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
tools:context="com.youtel.paisabox.activity.dth.MyDthOffers">
<ListView
android:id="@+id/dth_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:dividerHeight="1dp" >
</ListView>