列表视图中的按钮 - 安卓

时间:2012-04-22 18:26:39

标签: android

我有一个使用ListActivity作为列表适配器扩展SimpleAdapter的类。我的代码如下所示:

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

我的问题是我如何为列表中的每个元素添加一个按钮,按钮应该向右浮动。我的列表包含类Firm的对象,当用户按下此按钮时,如何知道从列表中获取的对象?

3 个答案:

答案 0 :(得分:1)

您必须编写一个扩展BaseAdapter的CustomAdapter。

答案 1 :(得分:1)

here是自定义列表视图的示例,可以帮助您

使用自定义适配器.... 并设置

listview.setAdapter(adapter);

答案 2 :(得分:0)

如果您可以使用OnTouchListener和OnLongClickListener而不是按钮,则实现起来会更容易一些。此外,如果您只想选择项目,则可以更轻松地使用标准的内置Android机制。

只有当你真的需要每个列表项目上的按钮时,你必须像Gaurav Agarwal建议的那样... - 无论如何你迟早要做的事情: - )