Android-Android ListView的onItemClick回调方法中的position值不正确,怎么回事?
今天遇到一个令人十分费解的问题,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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是不是添加了header?
我估计你是调用了ListView.addHeaderView(View)这个方法;这样ListView在计算position的时候会吧HeaderView当做第一行(下标为0);而真正的数据下标会从1开始;你数据既然封装在HomeNewsBean ;你要传递数据可用使用listview的item中的某一个TextView.setTag(HomeNewsBean);在OnItemClickListener中找到这个控件,然后TextView.getTag返回的就是HomeNewsBean对象,取出里面的数据该干嘛干嘛就行了。
这个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;
}
}
}