我正在使用 Listview 以及包含 imageview、textview 和 3 个按钮(插入、更新、删除)的自定义适配器,要求是每次在 BROADCAST 接收器内部调用自定义适配器,直到 Intentfilter 匹配,并且我也在基础适配器的 getView 方法中设置按钮的 onclicklistener。
问题是只有 ListView 按钮的最后一行只能单击。但我希望所有行的所有按钮都必须可单击。
任何人都可以给我建议或任何想法如何解决这个问题。
public View getView(final int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
pos=position;
if(view==null)
{
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.device_name, parent, false);
}
TextView text_view=(TextView)view.findViewById(R.id.textview_deviceName);
ImageView image_view=(ImageView)view.findViewById(R.id.imageView1);
text_view.setText(strDeviceName[position]);
if(strMajorDevice[position].equalsIgnoreCase("phone"))
{
image_view.setImageResource(int_image[0]);
}
else
{
image_view.setImageResource(int_image[1]);
}
btnAdd=(Button)view.findViewById(R.id.btn_add);
btnUpdate=(Button)view.findViewById(R.id.btn_update);
btnDelete=(Button)view.findViewById(R.id.btn_delete);
btnAdd.setOnClickListener(this);
btnUpdate.setOnClickListener(this);
btnDelete.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==btnAdd)
{
Toast.makeText(context, "ADD", Toast.LENGTH_LONG).show();
Intent intent = new Intent(context,MaterDeviceFormActivity.class);
intent.putExtra("button","add");
intent.putExtra("device_address", strDviceAddess[pos]);
context.startActivity(intent);
}
else if(v==btnUpdate)
{
Toast.makeText(context, "UPDATE", Toast.LENGTH_LONG).show();
Intent intent = new Intent(context,MaterDeviceFormActivity.class);
intent.putExtra("button","update");
intent.putExtra("device_address", strDviceAddess[pos]);
context.startActivity(intent);
}
else if(v==btnDelete)
{
Toast.makeText(context, "DELETE", Toast.LENGTH_LONG).show();
Intent intent = new Intent(context,MaterDeviceFormActivity.class);
intent.putExtra("button","delete");
intent.putExtra("device_address", strDviceAddess[pos]);
context.startActivity(intent);
}
}
最佳答案
private Context context;
public ListViewAdapter(Context context, String[] dateValues,String[] creditAmountValues,String[] closingBalanceValues,String[] currentAmountValues)
{
super(context, R.layout.transactionlayout, dateValues);
this.context = context;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.transactionlayout, parent, false);
((Button)rowView.findViewById(R.id.transactions_historyButtonID)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, ""+position, 4000).show();
}
});
return rowView;
}
使用 inflater 获取行 View 的使用。这是有效的。如果您有任何疑问,请告诉我。
关于java - 使用 BaseAdapter 的 listView 上的 android-Button 单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13081250/