Skip to content Skip to sidebar Skip to footer

How Can I Set Button Clicklistener In To List View To Work Like Onitemclicklistener

I have a listview that contains two buttons, a text view and... I want to set onclicklistener for one button and a textview to do whatever that I set in onitemclicklistener for th

Solution 1:

You can use an interface for handling click event of those Buttons and TextView. You have to set all focusable items'(like Button, RatingBar, EditText, Spinner etc.) focus to false.

I am giving you a code snippet

Adapter code

publicclassPropertyListAdapterextendsArrayAdapter<PropertyInfo> {

    private Context _context;
    privateint layoutId;
    private List<PropertyInfo> dataList;
    private ListItemBtnClickInterface _interface;

    publicPropertyListAdapter(Context context, int resource,
            List<PropertyInfo> objects, ListItemBtnClickInterface _interface) {
        super(context, resource, objects);
        // TODO Auto-generated constructor stub
        _context = context;
        layoutId = resource;
        dataList = objects;
        this._interface = _interface;
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stubfinal ViewHolder viewHolder;
        if(convertView == null) {
            LayoutInflaterinflater= (LayoutInflater) _context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(layoutId, parent, false);
            viewHolder = newViewHolder();
            viewHolder.propertyImgView = (ImageView) convertView.
                    findViewById(R.id.propertyImgView);
            viewHolder.editImgView = (ImageView) convertView.
                    findViewById(R.id.editImgView);
            viewHolder.deleteImgView = (ImageView) convertView.
                    findViewById(R.id.deleteImgView);
            viewHolder.propertyNameTxtView = (TextView) convertView.
                    findViewById(R.id.propertyNameTxtView);
            viewHolder.ownerTypeTxtView = (TextView) convertView.
                    findViewById(R.id.ownerTypeTxtView);
            viewHolder.checkBox1 = (CheckBox) convertView.
                    findViewById(R.id.checkBox1);
            viewHolder.propertyNameTxtView.setTypeface(Utils.getRegularTypeface(_context));
            viewHolder.ownerTypeTxtView.setTypeface(Utils.getRegularTypeface(_context));

            viewHolder.checkBox1.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {

                @OverridepublicvoidonCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                    PropertyInfoelement= (PropertyInfo) viewHolder.checkBox1
                      .getTag();
                  element.setSelected(buttonView.isChecked());

                }
              });

            convertView.setTag(viewHolder);
            viewHolder.checkBox1.setTag(dataList.get(position));
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
            ((ViewHolder) convertView.getTag()).checkBox1.setTag(dataList.get(position));
        }

        ViewHolderholder= (ViewHolder) convertView.getTag();
        holder.checkBox1.setChecked(dataList.get(position).isSelected());

        intheight= viewHolder.propertyImgView.getLayoutParams().height;
        intwidth= viewHolder.propertyImgView.getLayoutParams().width;

        viewHolder.deleteImgView.setTag(position);
        viewHolder.editImgView.setTag(position);

        viewHolder.deleteImgView.setOnClickListener(newOnClickListener() {

            @OverridepublicvoidonClick(View v) {
                // TODO Auto-generated method stubObjectdata= v.getTag();
                if(data != null) {
                    _interface.listItemBtnClickListener(data, v.getId());
                }
            }
        });

        viewHolder.editImgView.setOnClickListener(newOnClickListener() {

            @OverridepublicvoidonClick(View v) {
                // TODO Auto-generated method stubObjectdata= v.getTag();
                if(data != null) {
                    _interface.listItemBtnClickListener(data, v.getId());
                }
            }
        });

        PropertyInfoaddPropertyInfo= dataList.get(position);
        StringpropertyName="";
        StringownerType="";
        StringimgPath="";
        propertyName = addPropertyInfo.getPropertyName();
        ownerType = addPropertyInfo.getOwnerTypeName();
        imgPath = addPropertyInfo.getImagePath();

        viewHolder.propertyNameTxtView.setText(propertyName);
        viewHolder.ownerTypeTxtView.setText(ownerType);
        if(imgPath != null && !imgPath.equalsIgnoreCase("")) {
            Uriuri= Uri.parse(imgPath);
            Picasso.with(_context).load(uri)
            .resize(width, height).centerCrop().into(viewHolder.propertyImgView);
        } else {
            viewHolder.propertyImgView.setImageResource(R.drawable.no_img);
        }

        return convertView;
    }

    privateclassViewHolder {
        ImageView propertyImgView;
        ImageView editImgView;
        ImageView deleteImgView;
        TextView propertyNameTxtView;
        TextView ownerTypeTxtView;
        CheckBox checkBox1;
    }
}

Interface for handling click event

publicinterfaceListItemBtnClickInterface {

    publicvoidlistItemBtnClickListener(Object obj, int viewId);
}

setting adapter and click event of child views

propertyList = dbHelper.getAllProperties();
        dbHelper.closeDB();

        if(propertyList != null && propertyList.size() > 0) {
            noPropertyTxtView.setVisibility(View.GONE);
            adapter = new PropertyListAdapter(
                    PropertyListActivity.this, 
                    R.layout.row_property, propertyList, new ListItemBtnClickInterface() {

                        privateint clickedPosition;

                        @Override
                        publicvoidlistItemBtnClickListener(Object obj, int viewId) {
                            // TODO Auto-generated method stub
                            clickedPosition = Integer.parseInt(obj.toString());
                            PropertyInfo addPropertyInfo = propertyList.get(clickedPosition);
                            final long propertyId = addPropertyInfo.getId();
                            switch (viewId) {
                            case R.id.editImgView:
                                Intent intent = new Intent(PropertyListActivity.this,
                                        AddPropertyActivity.class);
                                intent.putExtra(PortfolioManagementApplication.KEY_PROPERTY_ID, 
                                        propertyId);
                                startActivity(intent);
                                break;

                            case R.id.deleteImgView:
                                new AlertDialog.Builder(PropertyListActivity.this)
                                .setTitle(getResources().getString(R.string.delete_property_title))
                                .setIcon(R.drawable.delete_icon)
                                .setMessage(R.string.delete_property_msg).setCancelable(true)
                                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                                    @Override
                                    publicvoidonClick(DialogInterface dialog, int which) {
                                        // TODO Auto-generated method stub
                                        dbHelper.deleteProperty(propertyId);
                                        dbHelper.closeDB();
                                        propertyList.remove(clickedPosition);
                                        adapter.notifyDataSetChanged();
                                        if (propertyList.size() == 0)
                                            noPropertyTxtView.setVisibility(View.VISIBLE);
                                        else
                                            noPropertyTxtView.setVisibility(View.GONE);
                                    }
                                }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                                    @Override
                                    publicvoidonClick(DialogInterface dialog, int which) {
                                        // TODO Auto-generated method stub

                                    }
                                }).show();
                                break;

                            default:
                                break;
                            }
                        }
                    });
            propertyListView.setAdapter(adapter);
            propertyListView.setOnItemClickListener(this);
        } else {
            noPropertyTxtView.setVisibility(View.VISIBLE);
        }

OnItemClick of ListView

@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stubPropertyInfoaddPropertyInfo= propertyList.get(position);
        longpropertyId= addPropertyInfo.getId();
        Intentintent=newIntent(PropertyListActivity.this,
                AddPropertyActivity.class);
        intent.putExtra(PortfolioManagementApplication.KEY_PROPERTY_ID, 
                propertyId);
        startActivity(intent);
    }

add these attributes in focusable item

android:clickable="false"android:focusable="false"android:focusableInTouchMode="false"

Post a Comment for "How Can I Set Button Clicklistener In To List View To Work Like Onitemclicklistener"