Skip to content Skip to sidebar Skip to footer

Java: Arrayadapter Geting Clicked Row Textview Value

I am trying to get value of TextView from clicked row of ArrayAdapter. I have two problems: 1. How do i detect row clicked? 2. How to get vaule of that row? I am using this code to

Solution 1:

1-Use Listener on your list object

lv.setOnItemClickListener(newAdapterView.OnItemClickListener() {
        @OverridepublicvoidonItemClick(AdapterView<?> parent, View view,int position, long id) {

        }
    });

2-position will give you the the item position clicked in your list_view

Solution 2:

Use itemclicklistener on your list. If you are passing string to the arrayadapter as you mention in your code then it will be.

mListView.setOnItemClickListener(newOnItemClickListener() 
        {

            @OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                // TODO Auto-generated method stub
                String item=arg0.getItemAtPosition(arg2).toString();
            }
        });

if you are passing array list in arrayadapter like

adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);

then you can get the item at that particular position like following:

mListView.setOnItemClickListener(newOnItemClickListener() 
        {

            @OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                // TODO Auto-generated method stub
                String item=arg0.getItemAtPosition(arg2).toString();
            }
        });

Solution 3:

Use setOnItemSelectedListener() for your listview

lv.setOnItemSelectedListener(newMyOnItemSelectedListener());
publicclassMyOnItemSelectedListenerimplementsOnItemSelectedListener 
{
    @OverridepublicvoidonItemSelected(AdapterView<?> parent, View view, int pos,
        long id) 
    {
        //toast message
        Toast.makeText(parent.getContext(), "Selected item: " +    parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
        Stringselected_item= parent.getItemAtPosition(pos).toString();
    }

    @OverridepublicvoidonNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub
    }
}

Post a Comment for "Java: Arrayadapter Geting Clicked Row Textview Value"