user4235226
user4235226

Reputation:

Button OnClickListener within ListView

I am having some trouble with my button listener in my listView. I have my custom adapter which is printing out my row (TextView..Button..Edittext..Button), Within this class I have set up my Listeners. What I am trying to do is when either one of the buttons is clicked they will increment or decrement the editText in the row. The trouble that I am having though is the onClickListener() and getLayoutInflater() are both coming up red. How do I fix this and is my code correct for what I am trying to do? Thanks

My custom adapter class

package com.example.rory.dbtest;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.pinchtapzoom.R;


public abstract class CustomCursorAdapter extends CursorAdapter implements View.OnClickListener {

public int counter = 0;

public CustomCursorAdapter(Context context, Cursor c) {
    super(context, c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // when the view will be created for first time,
    // we need to tell the adapters, how each item will look
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.row, parent, false);

    return retView;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    // here we are setting our data
    // that means, take the data from the cursor and put it in views

    TextView textViewPersonName = (TextView) view.findViewById(R.id.item1);
    textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(0))));

}

public View getView(final int position, View convertView, ViewGroup parent)
{
    if (convertView == null)
    {
        LayoutInflater inflater = getLayoutInflater();
        convertView = (LinearLayout)inflater.inflate(R.layout.row, null);
    }

    final EditText edit1 = (EditText)convertView.findViewById(R.id.runningTotal);
    Button plusButton = (Button) convertView.findViewById(R.id.plusButton);
    plusButton.setOnClickListener(new onClickListener()
    {
       public void onClick(View v)
       {
           counter++;
           edit1.setText(Integer.toString(counter));

       }
    });

    //final EditText edit1 = (EditText)convertView.findViewById(R.id.runningTotal);
    Button minusButton = (Button) convertView.findViewById(R.id.minusButton);
    plusButton.setOnClickListener(new onClickListener()
    {
        public void onClick(View v)
        {
            counter--;
            edit1.setText(Integer.toString(counter));

        }
    });



    return convertView;
}
}

And if it is needed my XML file for each row

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView android:id="@+id/item1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:paddingBottom="5dp"
    android:hint="@string/hint"/>

<Button
    android:id="@+id/plusButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/plusButton"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<EditText
    android:id="@+id/runningTotal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/hint"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/minusButton"
    android:layout_toStartOf="@+id/minusButton"
    android:layout_marginRight="30dp" />

<Button
    android:id="@+id/minusButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/minusButton"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />



</RelativeLayout>

Upvotes: 0

Views: 137

Answers (1)

Abdallah Alaraby
Abdallah Alaraby

Reputation: 2249

Instead of getLayoutInflater() use

LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

you can get the context from the constructor:

Context context;
public CustomCursorAdapter(Context context, Cursor c) {
    super(context, c);
    this.context = context;
}

And instead of onClickListener use View.onClickListener

You can remove the implements View.OnClickListener as well, you're not using it anyway.

Upvotes: 1

Related Questions