Manoj Godara
Manoj Godara

Reputation: 169

Getting listview item index in android

My Custom ListView Adapter class like this. I want to get the index number of an item once an item has been pressed(onclick). How can I get the index number and pass it to a int.. The code where I'll get the index number is after the onCreate method. Please help me!

// 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;
}

Upvotes: 0

Views: 10214

Answers (3)

JMPergar
JMPergar

Reputation: 1906

If you want de id of element you can write in getView:

getItemId(position)

Upvotes: 1

nalaiqChughtai
nalaiqChughtai

Reputation: 1297

List View Item Position is already coming in argument list.

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

Upvotes: 1

Chintan Raghwani
Chintan Raghwani

Reputation: 3370

In getView() method you can get it

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

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

So, here position is your index

Upvotes: 0

Related Questions