android-Button的list点击事件使用BaseAdapter查看



我正在使用 listView 带有自定义适配器,其中包含imageView,textView和3按钮(插入,更新,删除)要求是每次都会在内部调用自定义适配器广播接收器,直到IntentFilter匹配为止,我还将基本适配器GetView方法的OnClickListener设置为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;
  }

使用Afflater获取行视图。这在起作用。让我知道您是否有疑问。

通过setTag()

设置Get View方法中每个视图的标签
@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {                            
            LayoutInflater inflater = context.getLayoutInflater();
            convertView = inflater.inflate(.....);
        } 
        ur_view= (views) convertView.findViewById(R.id.....);
                ur_view.setTag(position);
        ur_view.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //do something
            }
        }); 

它将起作用

- 在以为您有未注册的所有OnClickListener接口的Buttons

尝试一下,可能会解决您的问题。

btnAdd.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Integer index = (Integer) arg0.getTag();
        Toast.makeText(context, "ADD", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(context,MaterDeviceFormActivity.class);
        intent.putExtra("button","add");
        intent.putExtra("device_address", strDviceAddess[index]);
        context.startActivity(intent);
    }
});
btnUpdate.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Integer index = (Integer) arg0.getTag();
        Toast.makeText(context, "UPDATE", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(context,MaterDeviceFormActivity.class);
        intent.putExtra("button","update");
        intent.putExtra("device_address", strDviceAddess[index]);
        context.startActivity(intent);
    }
});
btnDelete.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Integer index = (Integer) arg0.getTag();
        Toast.makeText(context, "DELETE", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(context,MaterDeviceFormActivity.class);
        intent.putExtra("button","delete");
        intent.putExtra("device_address", strDviceAddess[index]);
        context.startActivity(intent);
    }
});

您可以在适配器内使用按钮侦听器,但是唯一要注册的项目将是最后一个项目(即立即从列表中删除的项目)。您需要告诉按钮侦听器单击哪个项目。就我而言,我刚刚通过了该位置,并用它加载了操纵列表项目所需的任何相关信息。

最新更新