Skip to content Skip to sidebar Skip to footer

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: pub

Solution 1:

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.

Solution 2:

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

publicclassCustomListAdapterextendsBaseAdapter {
private  ArrayList<SingleElementDetails> allElementDetails;
private Context con; 
private LayoutInflater mInflater;

publicCustomListAdapter(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(){


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

        }

    });
    return convertView;
}    

}

Solution 3:

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
    publicvoidonItemClick(AdapterView<?> parent, View view, int position,long id) {
                // TODO Auto-generated method stubif(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:-)

Post a Comment for "Add A Button In A Listview"