Question

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 display the list:

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

    // List view
    private ListView lv;

    // Listview Adapter
    ArrayAdapter<String> adapter;

    // Search EditText
E ditText inputSearch;


// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Listview Data
    String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                            "iPhone 4S", "Samsung Galaxy Note 800",
                            "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};

    lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
    lv.setAdapter(adapter);       

    }

}
Was it helpful?

Solution

1-Use Listener on your list object

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

        }
    });

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

OTHER TIPS

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(new OnItemClickListener() 
        {

            @Override
            public void onItemClick(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(new OnItemClickListener() 
        {

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

Use setOnItemSelectedListener() for your listview

lv.setOnItemSelectedListener(new MyOnItemSelectedListener());
public class MyOnItemSelectedListener implements OnItemSelectedListener 
{
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) 
    {
        //toast message
        Toast.makeText(parent.getContext(), "Selected item: " +    parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
        String selected_item = parent.getItemAtPosition(pos).toString();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top