Android-Android ListView的onItemClick回调方法中的position值不正确,怎么回事?

Android-Android ListView的onItemClick回调方法中的position值不正确,怎么回事?

灵芸 发布于 2017-01-24 字数 2637 浏览 1091 回复 2

今天遇到一个令人十分费解的问题,ListView的Adapter是继承自BaseAdapter类,代码如下:

package com.hwtt.oa.mobile.adapter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hwtt.oa.mobile.R;
import com.hwtt.oa.mobile.bean.HomeNewsBean;

public class HomeNewsAdapter extends BaseAdapter
{
private final Context context;
private final List<HomeNewsBean> homeNews

public HomeNewsAdapter(Context context, List<HomeNewsBean> homeNews)
{
this.context = context;
this.homeNews = homeNews;
}

@Override
public int getCount()
{
return homeNews.size();
}

@Override
public Object getItem(int position)
{
return homeNews.get(position);
}

@Override
public long getItemId(int position)
{
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder mViewHolder;
if(null == convertView)
{
convertView = LayoutInflater.from(context).inflatr(R.layout.home_news_item, null);
mViewHolder = new ViewHolder();
mViewHolder.mNewsTitle = (TextView) convertView.findViewById(R.id.HomeNews_Title);
mViewHolder.mNewsCreateTime = (TextView) convertView.findViewById(R.id.HomeNews_CreateTime);
mViewHolder.mNewsContent = (TextView) convertView.findViewById(R.id.HomeNews_Content);
convertView.setTag(mViewHolder);
} else
mViewHolder = (ViewHolder) convertView.getTag();
HomeNewsBean news = homeNews.get(position);
mViewHolder.mNewsTitle.setText(news.getNewsTitle());
mViewHolder.mNewsCreateTime.setText(mSimpleDateFormat.format(new Date()));
mViewHolder.mNewsContent.setText(news.getNewsContent());
return convertView;
}

private final class ViewHolder
{
private TextView mNewsTitle;
private TextView mNewsCreateTime;
private TextView mNewsContent;
}
}

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想挽留 2017-05-14 2 楼

你是不是添加了header?
我估计你是调用了ListView.addHeaderView(View)这个方法;这样ListView在计算position的时候会吧HeaderView当做第一行(下标为0);而真正的数据下标会从1开始;你数据既然封装在HomeNewsBean ;你要传递数据可用使用listview的item中的某一个TextView.setTag(HomeNewsBean);在OnItemClickListener中找到这个控件,然后TextView.getTag返回的就是HomeNewsBean对象,取出里面的数据该干嘛干嘛就行了。

灵芸 2017-05-08 1 楼

这个adapter是没有问题的.
你说的从1开始,是你在addHeader后,没有从header开始点击,而还是从原来的item开始点击。在listView的源码中,addHeader也是整合到listView中作为一个Item。

所以如果按照上面的方式,你添加两个header,你的开始位置是2了。我猜测你遇到的问题是无论添加几个Header,listiew中取的数据都是从0开始。这时你可以考虑下后面一个参数long id

附上一段测试代码,你可以执行看看:

 public class MainActivity extends ListActivity {
List<HomeNewsBean> homeNews = new ArrayList<HomeNewsBean>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
for(int i=0;i<10;i++){
HomeNewsBean hnb = new HomeNewsBean();
hnb.title="title="+i;
homeNews.add(hnb);
}
setContentView(R.layout.activity_main);
View vv = this.getLayoutInflater().inflate(R.layout.item, null);
this.getListView().addHeaderView(vv);
View vv2 = this.getLayoutInflater().inflate(R.layout.item, null);
this.getListView().addHeaderView(vv2);
HomeNewsAdapter hna = new HomeNewsAdapter(this,homeNews);
this.setListAdapter(hna);
this.getListView().setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.e("tag","position="+arg2+" "+arg3);
}

});
}

/* @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
}
*/

public class HomeNewsAdapter extends BaseAdapter
{
private final Context context;
private final List<HomeNewsBean> homeNews;

public HomeNewsAdapter(Context context, List<HomeNewsBean> homeNews)
{
this.context = context;
this.homeNews = homeNews;
}

@Override
public int getCount()
{
return homeNews.size();
}

@Override
public Object getItem(int position)
{
return homeNews.get(position);
}

@Override
public long getItemId(int position)
{
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder mViewHolder;
if(null == convertView)
{
convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
mViewHolder = new ViewHolder();
mViewHolder.mNewsTitle = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(mViewHolder);
} else
mViewHolder = (ViewHolder) convertView.getTag();
HomeNewsBean news = homeNews.get(position);
mViewHolder.mNewsTitle.setText("test");
return convertView;
}

private final class ViewHolder
{
private TextView mNewsTitle;
private TextView mNewsCreateTime;
private TextView mNewsContent;
}
}
}