0

List.get(position)用来获取 a 的位置ListView。但这会返回当前滚动边界顶部的位置。我的 ListView 有 50 个项目,但您一次只能看到 10 个。因此,当您完全向下滚动并调用 List.get(position) 时,您会得到 10 的列表项,而不是 50。如果您单击位于中间的项目,ListView您会得到 5 而不是 25 等。

如何ListView从. _ _ ListView至少是最简单的方法。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;

if (convertView == null) {
  LayoutInflater inflator = context.getLayoutInflater();      
  view = inflator.inflate(R.layout.addtowatchlist, null);
  final ViewHolder viewHolder = new ViewHolder();
  viewHolder.text = (TextView) view.findViewById(R.id.label);      
  viewHolder.addbutton = (Button) view.findViewById(R.id.button);
  viewHolder.addbutton
      .setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View buttonview) {
            //I need the correct ACTUAL position here, not the VISABLE position                 
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(list.get(position).getUrl()));
            context.startActivity(browserIntent);              
            list.get(position).getUrl();                
            Model element = (Model) viewHolder.addbutton
                      .getTag();
                  element.setSelected(true);
        }
      });
  view.setTag(viewHolder);
  viewHolder.addbutton.setTag(list.get(position));
} else {
  view = convertView;
  ((ViewHolder) view.getTag()).addbutton.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());      
return view;

}

编辑:我认为这与使用错误的 viewHolder 调用 onClickListener 有关,但我无法弄清楚......

4

2 回答 2

1

该变量position已经是您的 ListView 中条目的位置。
我不知道你的列表里面有什么。
这是 BaseAdapter 如何工作以及如何正确构建 getView 的教程:http: //thinkandroid.wordpress.com/2010/01/13/custom-baseadapters/

于 2013-02-28T13:21:23.850 回答
0

经过多次痛苦的尝试,我自己修复了它。缺乏了解 ListView 优化的运作方式以及convertView参数的作用......

我将我onClickListener的块移到块外if (convertView == null)并在 getView 返回其视图之前将其放置。这样拍摄的位置不是回收的可见位置,而是从顶部一直到实际位置。

@Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    View view = null;
    final AiringModel am = list.get(position);      
    am.setRealPosition(position, list.get(position).getUrl());    
    LayoutInflater inflator = context.getLayoutInflater();      
    view = inflator.inflate(R.layout.addtowatchlist, null);
    final ViewHolder viewHolder = new ViewHolder();
    viewHolder.text = (TextView) view.findViewById(R.id.label);
    if (convertView == null) {
        view.setTag(viewHolder);     
    } else {
      view = convertView;
      ((ViewHolder)view.getTag()).addbutton.setTag(list.get(position));      
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());       
    viewHolder.addbutton = (Button) view.findViewById(R.id.check);    
    viewHolder.addbutton.setTag((Integer)position);    
    viewHolder.addbutton
    .setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View buttonview) {
            // TODO Auto-generated method stub                  
            int realPosition = am.getRealPosition();
            String realName = am.getRealName();
            Log.d("Pair created!", "Clicked button with position: "+ realPosition + " For item name: " + realName);
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(list.get(realPosition).getUrl()));
            context.startActivity(browserIntent);           
        }
    });
    return view;
  }
于 2013-02-28T17:09:44.640 回答