我有自定义数组适配器的列表视图。我想从getView方法获取项目点击位置。我得到一些列表视图项位置,但当我在我的列表中添加超过7项时,我从getView方法得到错误的位置。我的意思是当我点击第9个列表项时它返回1.
这是我的代码
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
this.position = pos;
Log.v("View position", Integer.toString(pos));
lineView = convertView;
if(lineView==null)
{
adapterLine=new AdapterLine();
layoutInflater=context.getLayoutInflater();
lineView=layoutInflater.inflate(com.inomera.sanalmarket.main.R.layout.adapter, null,true);
adapterLine.sListText = (TextView) lineView.findViewById(R.id.sListText);
adapterLine.sListCheckbox = (CheckBox) lineView.findViewById(R.id.sListCheckbox);
adapterLine.sListImageView = (ImageView) lineView.findViewById(R.id.imageView1);
adapterLine.gestureOverlayView = (GestureOverlayView) lineView.findViewById(com.inomera.sanalmarket.main.R.id.gestureOverlayView1);
adapterLine.gestureOverlayView.setGestureVisible(false);
// To remember whitch tab is selected
adapterLine.sListImageView.setTag(pos);
adapterLine.sListCheckbox.setTag(pos);
adapterLine.sListText.setTag(pos);
Log.v("adapter", "position of adapter is " + Integer.toString(pos));
感谢您的帮助!
答案 0 :(得分:3)
数组适配器只会在列表视图中为每个可见行创建一个对象。在此之后,适配器将回收已创建的视图。这是:
的目的if(lineView==null)
在您的适配器中排队。
您需要放入一个使用循环视图设置行的else部分。这篇文章可能会有所帮助:
How can I make my ArrayAdapter follow the ViewHolder pattern?
答案 1 :(得分:0)
此实现是不完整的添加其他部分,它从数据中设置值。这是适合我的实施
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflator = reportViewContext.getLayoutInflater();
convertView = inflator.inflate(R.layout.tabitems, null);
viewHolder.name= (TextView) convertView
.findViewById(R.id.user_reportTab_userName);
//set other values needed in viewHolder if any
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
}
//data is list of Data model
if(data.size()<=0) {
//if data unavailable }
else {
/***** Get each Model object from Array list ********/
System.out.println("Position:"+position);
/************ Set Model values in Holder elements ***********/
viewHolder.name.setText(data.get(position).getName());
}
return convertView;
}
我知道这个问题已经过时了,如果有人偶然发现类似的问题,请回答