开发者

add a button in a listView

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");
开发者_StackOverflow
        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.


You must have a custom list adapter like the one given below.

public class CustomListAdapter extends BaseAdapter {
private  ArrayList<SingleElementDetails> allElementDetails;
private Context con; 
private LayoutInflater mInflater;

public CustomListAdapter(Context context, ArrayList<SingleElementDetails> results) {
    allElementDetails = results;
    mInflater = LayoutInflater.from(context);
    con=context;
       public View getView(int position, View convertView, ViewGroup parent) 
{
    convertView = mInflater.inflate(R.layout.listview1, null);

    Button bt=(Button)convertView.findViewById(R.id.bt);
    TextView textview1= (TextView) convertView.findViewById(R.id.dishname_entry);
    TextView textview2 = (TextView) convertView.findViewById(R.id.category_entry);
    TextView textview3=(TextView)convertView.findViewById(R.id.description_entry);
    textview1.setText(allElementDetails.get(position).getDishName());
    textview2.setText(allElementDetails.get(position).getCategory());
    textview3.setText(allElementDetails.get(position).getDescription());    


    bt.setOnClickListener(new OnClickListener(){


        public void onClick(View v) {
            Intent intent=new Intent(con,MainActivity.class);
            con.startActivity(intent);

        }

    });
    return convertView;
}    

}


you can use a Custom adapter like this:

ListView lv_ArchivePartylist;

ArrayList<Parties> select_archived_party;

lv_ArchivePartylist = (ListView)findViewById(R.id.archive_ListView01);
        lv_ArchivePartylist.setOnItemClickListener(new OnItemClickListener()
            {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
                // TODO Auto-generated method stub
                if(view.findViewById(R.id.img_chkbox_archive).getVisibility()==TextView.GONE)
                {
                    view.findViewById(R.id.img_chkbox_archive).setVisibility(TextView.VISIBLE);
                    Toast.makeText(ctx_archive, "Name="+archived_parties.get(position).getPartyTitle(), Toast.LENGTH_SHORT).show();
select_archived_party.add(archived_parties.get(position));
}
}

});

Then,instead of textview,use buttons and on that button's individual click event,you can write the code to make a call...Hope it helps:-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜