-2

I have search a lot of questions here and already implemented every answer i get from here, but no one has solved this problem. I have 2 buttons in a row of my custom listview and i want to perform on click on them. Here is my custom adapter and xml code.

EDIT

CartAadpter.java

        public class CartAdapter extends ArrayAdapter<CartItemListData> {

    Context context;

    int layoutResourceId;

    ArrayList<CartItemListData> data = new ArrayList<CartItemListData>();

    LayoutInflater mInflater;

    CartItemListData listData;

    CartDatabaseHelper db;

    int i;

    public CartAdapter(Context context, ArrayList<CartItemListData> data) {

        super(context, R.layout.cart_listitems, data);

        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if (convertView == null) {

            mInflater = LayoutInflater.from(context);

            convertView = mInflater.inflate(R.layout.cart_listitems, parent,
                    false);

            holder = new ViewHolder();

            holder.text = (TextView) convertView
                    .findViewById(R.id.cart_item_id);

            holder.text1 = (TextView) convertView
                    .findViewById(R.id.cart_item_boxes);

            holder.edit = (Button) convertView.findViewById(R.id.editdata);

            holder.delete = (Button) convertView.findViewById(R.id.deletedata);

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();
        }

        listData = data.get(position);

        // Button's Tag
        holder.edit.setTag(data.get(position).getId());
        holder.delete.setTag(data.get(position).getId());

        // SetText to Textview
        holder.text.setText(data.get(position).getTilesId());
        holder.text1.setText(String.valueOf(listData.getNumberOfBox()));

        Log.e("Database Data ID:  ", listData.getId() + "  " + position);

        holder.edit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(final View v) {

                i = data.get(position).getId();

                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("Edit Box");
                builder.setMessage("Please Enter Number of Box.");

                final EditText editText = new EditText(context);
                editText.setInputType(InputType.TYPE_CLASS_NUMBER);

                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT);

                editText.setLayoutParams(lp);

                builder.setView(editText);

                builder.setNegativeButton("Cancel", null);

                builder.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {

                                if (editText.getText().toString() == null
                                        && editText.getText().length() == 0) {

                                    Toast.makeText(
                                            context,
                                            "To delete Data use \"delete button\"",
                                            Toast.LENGTH_LONG).show();
                                } else {

                                    int data = Integer.parseInt(editText
                                            .getText().toString());

                                    if (data <= 0) {

                                        Toast.makeText(
                                                context,
                                                "To delete Item Please use delete button",
                                                Toast.LENGTH_LONG).show();
                                    } else {

                                        Log.e("Edited", "Yes");

                                        CartDatabaseHelper cartDatabaseHelper = new CartDatabaseHelper(
                                                context);

                                        cartDatabaseHelper
                                                .updateCart(new CartItemListData(
                                                        i, holder.text
                                                                .getText()
                                                                .toString(),
                                                        data));
                                        CartAdapter.this.data.clear();
                                        CartAdapter.this.data.add(listData);
                                        notifyDataSetChanged();
                                    }
                                }
                            }

                        });
                builder.create().show();
            }
        });

        holder.delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.e("Deleted", "Yes");

                AlertDialog.Builder builder = new AlertDialog.Builder(context);

                builder.setTitle("Delete!!");
                builder.setMessage("Deleting Item from List!!!");
                builder.setIcon(R.drawable.ic_alerts_and_states_warning);

                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {

                                db = new CartDatabaseHelper(context);

                                db.delete_cartitem(data.get(position).getId());

                                data.remove(position);

                                notifyDataSetChanged();

                            }
                        });
                builder.setNegativeButton("No", null);
                builder.create().show();

            }
        });

        return convertView;
    }

    static class ViewHolder {

        TextView text, text1;
        Button edit, delete;
    }
}

cartlist_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<TextView
    android:id="@+id/cart_item_id"
    android:layout_width="0sp"
    android:layout_height="fill_parent"
    android:layout_marginLeft="2dp"
    android:layout_weight="2"
    android:background="@drawable/cart_list_text_background"
    android:gravity="center_vertical|center_horizontal"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="22sp" />

<TextView
    android:id="@+id/cart_item_boxes"
    android:layout_width="0sp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="@drawable/cart_list_text_background"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="22sp" />

<Button
    android:id="@+id/editdata"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp"
    android:background="@drawable/edit"
    android:focusable="false"                    // Here I have checked it with true value too
    android:focusableInTouchMode="false" />

<Button
    android:id="@+id/deletedata"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:background="@drawable/ic_delete"
    android:focusable="false"
    android:focusableInTouchMode="false" />

MylistView:

<ListView
        android:id="@+id/lvExp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:cacheColorHint="#FFFFFF"
        android:childDivider="#f8dfdb"
        android:choiceMode="singleChoice"
        android:clipChildren="true"
        android:clipToPadding="true"
        android:divider="#ff2200"
        android:dividerHeight="3dp"
        android:drawSelectorOnTop="true"
        android:fastScrollEnabled="true"
        android:focusable="false"
        android:footerDividersEnabled="true"
        android:hapticFeedbackEnabled="true"
        android:headerDividersEnabled="true"
        android:scrollingCache="true"
        android:soundEffectsEnabled="true"
        android:textFilterEnabled="false"
        android:transcriptMode="normal"
        android:translationX="10dp" />

Can anyone tell me where did i make mistake? My work is about to finish and app is almost ready but i am stuck in this problem. I will be thankfull to any type of suggestion or help.

Shvet
  • 1,159
  • 1
  • 18
  • 30
  • have you tried `clickable=true` property.. – Pragnesh Ghoda シ Sep 04 '14 at 09:33
  • 1
    Yes, But still not working. – Shvet Sep 04 '14 at 09:37
  • Hi please have a look at my answer [here](http://stackoverflow.com/questions/23623060/can-we-have-both-of-button-and-onitemclick-listener-in-listview-in-android/23623096#23623096). Hope will help you... – Neha Shukla Sep 04 '14 at 10:24
  • Hi have a look at my answer [here](http://stackoverflow.com/questions/23623060/can-we-have-both-of-button-and-onitemclick-listener-in-listview-in-android/23623096#23623096). Hope will help you – Neha Shukla Sep 04 '14 at 10:25

2 Answers2

3

I am not sure,but Try holder.edit.setOnClickListener(new View.OnClickListener()) instead of holder.edit.setOnClickListener(new OnClickListener()).

Aditi Parikh
  • 1,522
  • 3
  • 13
  • 34
  • Have you tried with printing Log?Print log message under onclick method.So,you can test weather your onclick method is being called or not?Or may be any View is there on your listview.I mean Listview is in background of any other view..So try in reference to that also. – Aditi Parikh Sep 04 '14 at 10:30
  • 1
    When we code it we always import them, so we need not to write in this case. We need to write new DialogInterface.OnClickListener() in AlertDialog Box as it will not going to import it. I have printed log and now it is working thanks. – Shvet Sep 04 '14 at 10:32
1

try this

add this cartlist_row.xml in parent Linearlayout

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

for Button add this

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

and AlertDialog.Builder builder is not working , you never used show().. Please Cross check .. Use AlertDialog alert = builder.create(); alert.show();

Rajesh Mikkilineni
  • 854
  • 10
  • 22