0

我的自定义 ListView 适配器类是这样的。一旦按下项目(onclick),我想获取项目的索引号。如何获取索引号并将其传递给 int.. 我将获取索引号的代码位于 onCreate 方法之后。请帮我!

// On holder.txtstormName_Nice button click i want to get selected item index.
public class ListViewCustomAlerts extends BaseAdapter {

    ArrayList<HurricaneBeanClass> itemList;

    public Activity context;
    public LayoutInflater inflater;
    public ListViewCustomAlerts(Activity context,ArrayList<HurricaneBeanClass> arraylist_List) 
    {
        super();
        this.context = context;
        this.itemList = arraylist_List;
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() 
    {
        return itemList.size();
    }

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

    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
         p =position;
       final ViewHolder holder;
        if(convertView==null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.hurricane_row, null);  
            holder.txtstormName_Nice=(TextView)convertView.findViewById(R.id.hurricaneNameRowTextView);
            convertView.setTag(holder);  

        }
        else
            holder=(ViewHolder)convertView.getTag();
        HurricaneBeanClass bean = (HurricaneBeanClass) itemList.get(position);
        holder.txtstormName_Nice.setText(bean.getStormName_Nice());

        holder.txtstormName_Nice.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
        //here i want item index.................
                HurricaneBeanClass bean = (HurricaneBeanClass) itemList.get(position);
                HurricaneActivity.curlat=bean.getCurentlat();
                HurricaneActivity.curlon=bean.getCurentlon();

...

            }
        });

        return convertView;

    }                  

}

public static class ViewHolder
{
    TextView txtstormName_Nice;
}
4

3 回答 3

1

列表视图项目位置已经出现在参数列表中。

public View getView(int position, View convertView, ViewGroup parent) {
于 2012-09-04T11:21:25.887 回答
1

如果你想要元素的 id,你可以在 getView 中写:

getItemId(position)
于 2012-09-04T11:22:35.683 回答
0

在 getView() 方法中你可以得到它

 public View getView(final int position, View convertView, ViewGroup parent) {
 .........
 .....

  System.out.println("Index:"+position);
 }

所以,这里的位置是你的索引

于 2012-09-04T11:14:26.263 回答