0

我正在使用带有自定义适配器的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);
    }
}
4

5 回答 5

3
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;
  }

使用充气机来使用行视图。这是有效的。如果你有任何疑问,请告诉我。

于 2012-10-26T05:55:38.123 回答
1

在 get view 方法中为每个视图设置标签setTag()

@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
            }
        }); 

它会工作

于 2012-10-26T06:19:42.330 回答
0

-认为你还没有注册所有ButtonsOnClickListener接口。

于 2012-10-26T05:31:53.607 回答
0

试试这个,也许它会解决你的问题。

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);
    }
});
于 2012-10-27T10:00:07.847 回答
0

您可以使用适配器内的按钮侦听器,但唯一注册它的项目将是最后绘制的项目(即立即从列表中删除的项目)。您需要告诉按钮侦听器它被点击了哪个项目。在我的例子中,我只是传入了该位置并使用它来加载操作列表项所需的任何相关信息。

于 2015-12-02T00:51:58.340 回答