永发信息网

android的listview中怎么加入button

答案:2  悬赏:70  手机版
解决时间 2021-02-01 12:50
  • 提问者网友:临风不自傲
  • 2021-02-01 07:42
android的listview中怎么加入button
最佳答案
  • 五星知识达人网友:撞了怀
  • 2021-02-01 09:04
//注意原本getView方法中的int position变量是非final的,现在改为final
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
 ViewHolder holder = null;
if (convertView == null) {

holder=new ViewHolder();  

//可以理解为从vlist获取view  之后把view返回给ListView
convertView = mInflater.inflate(R.layout.vlist, null);
holder.title = (TextView)convertView.findViewById(R.id.title);
holder.info = (TextView)convertView.findViewById(R.id.info);
holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}

holder.title.setText((String)mData.get(position).get("title"));
holder.info.setText((String)mData.get(position).get("info"));
holder.viewBtn.setTag(position);
//给Button添加单击事件  添加Button之后ListView将失去焦点  需要的直接把Button的焦点去掉
holder.viewBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
showInfo(position);
}
});

//holder.viewBtn.setOnClickListener(MyListener(position));

return convertView;
}
}

//提取出来方便点
public final class ViewHolder {
public TextView title;
public TextView info;
public Button viewBtn;
}
public void showInfo(int position){

ImageView img=new ImageView(ListViewActivity.this);
img.setImageResource(R.drawable.b);
new AlertDialog.Builder(this).setView(img)
.setTitle("详情"+position)
.setMessage("菜名:"+title[position]+"   价格:"+info[position])
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
给你发个列子
全部回答
  • 1楼网友:野味小生
  • 2021-02-01 09:13
需要自定义listview的适配器。在getview方法里面设置三个button的点击事件。 给个例子你: private class testadapter extends baseadapter { private layoutinflater minflater; private context con; public testadapter (context context) { // cache the layoutinflate to avoid asking for a new one each time. con = context; minflater = layoutinflater.from(context); } public int getcount() { return list.size(); } public object getitem(int position) { return position; } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { final viewholder holder; if (convertview == null) { convertview = minflater.inflate(r.layout.list, null); holder = new viewholder(); holder.bt01 = (button) convertview.findviewbyid(r.id.bt01); holder.bt02 = (button) convertview.findviewbyid(r.id.bt02); holder.bt03 = (button) convertview.findviewbyid(r.id.bt03); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } final int arg = position; //bt01点击 holder.bt01.setonclicklistener(new imageview.onclicklistener(){ public void onclick(view v) { } }); //bt02点击 holder.bt02.setonclicklistener(new imageview.onclicklistener(){ public void onclick(view v) { } }); //bt03点击 holder.bt03.setonclicklistener(new imageview.onclicklistener(){ public void onclick(view v) { } }); return convertview; } class viewholder { button bt01; button bt02; button bt03; } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯