Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

Button in a listview - android

I have a class that extends ListActivity with a SimpleAdapter as the list adapter. My code looks like this:

public class ListOfFirms extends ListActivity {

    Intent extras;
    int time;
    String km;
    ArrayList<String> firms = new ArrayList<String>(); 
    SimpleAdapter adapter;  
    static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
        
    @Override
    public void onCreate(Bundle savedInstanceState) {
            
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firms);
            
        extras = getIntent();
            
        time = extras.getIntExtra("time", 0); 
        km = extras.getStringExtra("km");
                  
        adapter = new SimpleAdapter(
            this, list, R.layout.taxi_custom,
            new String[] {"name","price"},
            new int[] {R.id.taxi_name,R.id.taxi_price});
          
            initializeFirm();
            setListAdapter(adapter);
     }
}

My question is how I can add a button to each element in the list, the button should be floating to right. My list contains object of the class Firm, how can I know which object that I grab out from the list, when a user presses this button?

Upvotes: 1

Views: 305

Answers (3)

MAC
MAC

Reputation: 15847

here is example of custom listview which may help you

use custom adapter.... and set

listview.setAdapter(adapter);

Upvotes: 1

user387184
user387184

Reputation: 11053

If you can use OnTouchListener and OnLongClickListener instead of a button it is a little easier to implement. Also if you just want to select the item, it ies easier tu use the standard built-in Android mechanisms.

Only if you REALLY need a button on each list item you have to do it like Gaurav Agarwal suggested... - which is something you might have to do sooner or later anyway :-)

Upvotes: 0

Gaurav Agarwal
Gaurav Agarwal

Reputation: 19102

You will have to write a CustomAdapter which extends BaseAdapter.

Upvotes: 1

Related Questions