且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在ListView添加一个按钮

更新时间:2023-11-24 14:40:58

雅,你可以做。对于


  1. 您将创建包含的TextView和按钮列表行布局文件。

  2. 使用该布局定制ArrayAdapter内。

看到一个如在这网站

I want to add a button in every list item and when the user press it to make a phone call. But when the user press the text, nothing happens..is that possible? This is my code:

public class museum extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.museum); 


        ListView list = (ListView) findViewById(R.id.list);

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();


        HashMap<String, String> map = new HashMap<String, String>();
        map.put("name", "Archaeological Museum of Chania");
        map.put("address", "Chalidon 21 , Chania");

        mylist.add(map);
        map = new HashMap<String, String>();
        map.put("name", "Byzantine Museum");
        map.put("address", "Theotokopoulou 82 , Chania");

        mylist.add(map);
        // ...
        ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row_museum,
                    new String[] {"name", "address"}, new int[] {R.id.TextView01, R.id.TextView02});
        list.setAdapter(mSchedule);



        list.setOnItemClickListener(new OnItemClickListener() {
         public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
                switch( position )
                {
                   case 0: 
                            break;
                   case 1:  
                        break;
                   case 2:  
                        break;
                   case 3:  
                        break;


                }
            }
          });
    }
}

Ya you can do. For that

  1. You will have to create a layout file for the list row that contains the textview and the button.
  2. Use that layout inside a customized ArrayAdapter.

See an eg in this site.