简体   繁体   中英

android-Button's clickevent of listView using BaseAdapter

I am using Listview With custom Adapter which contain imageview,textview and 3 button (insert,update,delete)requirement is that custom adapter is call every time inside BROADCAST receiver until intentfilter matched and i also set onclicklistener of button in getView method of base adapter.

The problem is that only the last row of listview button is only clickable..But i want all the button of all row must clickable.

Can anybody give me suggestion or any idea how I can proceed for that problem.

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

get the use of row view using inflater. this is working. let me know if u have any doubts.

set the tag for each view in get view method by 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
            }
        }); 

It will work

-考虑到您还没有OnClickListener界面注册所有Buttons

Try this, may be it will solve your problem.

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

You can use the button listener inside the the adapter but the only item to register it will be the last item drawn (ie the one immediately off the list). You need to tell the button listener which item it has been clicked on. In my case I just passed in the position and used that to load any associated info needed to manipulate the list item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM