How can I add 3 buttons to a listview on Android?
I'd like to customize each line of a List View and make them show up in the following way:
List View Buttons http://raphaeu.com/img_botoes.jpg
In the picture above, I will, of course, replace the numbers for images. But I need to make each one of those places "clickable", that is, each numbered place must have a different touch event attached to it.
How would I go on doing that?
Thank you开发者_如何学编程 in advance!!
You need to create a custom adapter class, extending from BaseAdapter. In it, you can inflate any layout for your rows you wish. There're tons of tutorials for this, search for 'listview custom adapter'.
Create a Custom List Adapter like Andro.. states here:
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;
}
}
Just have to tweak it a little bit for what you're trying to do. Everything you need though is pretty much in that class.
精彩评论