user1950262
user1950262

Reputation: 1

How can one set a OnLongClickListener to a Button in a ListView?

So what I am trying to do is, to set an OnLongClickListener to a ListView Button. I managed to put an OnClickListener to it and I also tried to put an OnLongItemClick to my whole ListView, but the problem is, that the OnLongItemClick is never called when I press the certain Button in the Item.

I was able to put the OnClickListener to the Buttons in the XML-File of the ListView-Rows by using android:onClick="myOnClickListener", but this doesn't work with the OnLongClickListener.

I also tried setting the OnLongClickListener to the button in the normal OnClickListener, but that's not what I really want because it just works after a normal click on it.

Is there any way to set the OnLongClickListener to the Buttons programmatically? They have to be unique in each row tho. Btw im not extending ListActivity.

Here is the OnClickListener for the Button:

//I attached this in my XML-File to the Button
public void handlerForPlus(View v) 
{
    //The LinearLayout where all my Items are in
    LinearLayout myItemLayout = (LinearLayout)v.getParent();

    //A random EditText in my Layout
    editTextCountItem = (EditText) myItemLayout.getChildAt(0);

    //The Button i want to add the OnLongClickListener to
    buttonItemPlus = (Button)myItemLayout.getChildAt(1);

    //Here i set the OnLongClickListener to the button, but I want to do this before it gets clicked normally.
    buttonItemPlus.setOnLongClickListener(this);

    editTextCountItem.setText((Integer.toString(Integer.parseInt(editTextCountItem.getText().toString()) + 1)));

    myItemLayout.refreshDrawableState();
}

Here is my XML-File for the LinearLayout of the ListView-Items:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="40" >

    <EditText
        android:id="@+id/etAnzahl"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:layout_weight="5"
        android:text="0" />

    <!-- This is the Button i want to set a OnLongClickListener to. -->
    <Button
        android:id="@+id/bItemPlus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="25"
        android:onClick="handlerForPlus"
        android:text="Item" />

    <Button
        android:id="@+id/bItemMinus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:onClick="handlerForMinus"
        android:text="-" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:weightSum="10"
        android:layout_weight="5"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvPriceItemSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:text="PreisS" />

        <TextView
            android:id="@+id/tvPriceItemLarge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:text="PreisL" />

    </LinearLayout>

</LinearLayout>

So does anybody know what could solve my Problem?

Best Regards, Alex.

Upvotes: 0

Views: 752

Answers (1)

XorOrNor
XorOrNor

Reputation: 8978

buttonItemPlus.setOnLongClickListener(this); has the effect for entire ListView. What I think you're looking for is buttonItemPlus.setOnItemLongClickListener(this); which has the effect for particular TextView in your ListView.

Upvotes: 1

Related Questions