Pfil Mintz
Pfil Mintz

Reputation: 161

increment counter with button click in recyclerview

I am working on app that fetches data using volley and displays in feed using RecyclerView.I have a Button in each card that when clicked, will count the number of clicks and display it in a TextView in that particular card**(like instagram like button)**. The app crashes when I run it. What am I doing wrong?

Here is my code

package net.simplifiedcoding.myfeed;
import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Belal on 11/9/2015.
 */
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
    //Imageloader to load image
    private ImageLoader imageLoader;
    private Context context;

    //List to store all superheroes
    List<SuperHero> superHeroes;

    //Constructor of this class
    public CardAdapter(List<SuperHero> superHeroes, Context context) {
        super();
        //Getting all superheroes
        this.superHeroes = superHeroes;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.superheroes_list, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder( final ViewHolder holder,    final int position) {
        //Getting the particular item from the list
         final SuperHero superHero = superHeroes.get(position);

        //Loading image from url
        imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
        imageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView, R.drawable.image, android.R.drawable.ic_dialog_alert));

        //Showing data on the views
        holder.imageView.setImageUrl(superHero.getImageUrl(), imageLoader);
        holder.textViewName.setText(superHero.getName());
        holder.textViewPublisher.setText(superHero.getPublisher());
        holder.textViewLikes.setText(superHero.getLikes());



        holder.custom_button.setOnClickListener(new View.OnClickListener() {
            int count;


            @Override
            public void onClick(View v) {

                count = 0;
                superHeroes.get(position).setCount(superHeroes.get(position).getCount()+1);
                holder.txtCount.setText(superHeroes.get(position).getCount());



            }

        });
        holder.txtCount.setText(superHeroes.get(position).getCount());


    }
    @Override
    public int getItemCount() {
        return superHeroes.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        //Views
        public NetworkImageView imageView;
        public TextView textViewName;
        public TextView textViewPublisher;
        public TextView textViewLikes;
        public TextView txtCount;
        public ImageButton custom_button;

        //Initializing Views
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
            textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
            textViewLikes = (TextView) itemView.findViewById(R.id.textViewlikes);
            txtCount = (TextView) itemView.findViewById(R.id.txtCount);
            custom_button = (ImageButton) itemView.findViewById(R.id.custom_button);
        }
    }
}





public class SuperHero {

    //Data Variables
    private String imageUrl;
    private String name;
    private String publisher;
    private String likes;
    private int count;


    //Getters and Setters

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public String getLikes() {
        return likes;
    }

    public void setLikes(String likes) {
        this.likes = likes;
    }

    public int getCount() {
        return count ;
    }

    public void setCount(int count) {
        this.count = count;
    }




}




03-18 05:34:00.791 2442-2442/net.simplifiedcoding.myfeed I/art: Not late-enabling -Xcheck:jni (already on)
03-18 05:34:00.947 2442-2442/net.simplifiedcoding.myfeed W/System: ClassLoader referenced unknown path: /data/app/net.simplifiedcoding.myfeed-2/lib/x86
03-18 05:34:03.952 2442-2502/net.simplifiedcoding.myfeed D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-18 05:34:04.130 2442-2502/net.simplifiedcoding.myfeed I/OpenGLRenderer: Initialized EGL, version 1.4
03-18 05:34:04.276 2442-2502/net.simplifiedcoding.myfeed W/EGL_emulation: eglSurfaceAttrib not implemented
03-18 05:34:04.276 2442-2502/net.simplifiedcoding.myfeed W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabefa7e0, error=EGL_SUCCESS
03-18 05:34:04.312 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 10.026ms
03-18 05:34:04.763 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 6.155ms
03-18 05:34:05.751 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 124.986ms
03-18 05:34:05.947 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 47.815ms
03-18 05:34:07.241 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 127.839ms
03-18 05:34:07.795 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 176.530ms
03-18 05:34:08.173 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 9.735ms
03-18 05:34:12.374 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 937.541ms
03-18 05:34:12.376 2442-2442/net.simplifiedcoding.myfeed I/Choreographer: Skipped 56 frames!  The application may be doing too much work on its main thread.
03-18 05:34:12.403 2442-2493/net.simplifiedcoding.myfeed D/Volley: [143] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://10.0.2.2/feed.php?page=1 0x1e7db229 NORMAL 1> [lifetime=4273], [size=270], [rc=200], [retryCount=0]
03-18 05:34:12.976 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 288.797ms
03-18 05:34:14.183 2442-2442/net.simplifiedcoding.myfeed W/System.err: org.json.JSONException: No value for likes
03-18 05:34:14.183 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-18 05:34:14.183 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
03-18 05:34:14.183 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity.parseData(MainActivity.java:136)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity.access$000(MainActivity.java:29)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity$1.onResponse(MainActivity.java:94)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity$1.onResponse(MainActivity.java:90)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Looper.loop(Looper.java:148)
03-18 05:34:14.191 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
03-18 05:34:14.191 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
03-18 05:34:14.192 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-18 05:34:14.192 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-18 05:34:14.192 2442-2442/net.simplifiedcoding.myfeed W/System.err: org.json.JSONException: No value for likes
03-18 05:34:14.193 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-18 05:34:14.193 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
03-18 05:34:14.198 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity.parseData(MainActivity.java:136)
03-18 05:34:14.198 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity.access$000(MainActivity.java:29)
03-18 05:34:14.199 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity$1.onResponse(MainActivity.java:94)
03-18 05:34:14.199 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity$1.onResponse(MainActivity.java:90)
03-18 05:34:14.199 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
03-18 05:34:14.200 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
03-18 05:34:14.201 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
03-18 05:34:14.201 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-18 05:34:14.201 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Looper.loop(Looper.java:148)
03-18 05:34:14.201 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
03-18 05:34:14.201 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
03-18 05:34:14.202 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-18 05:34:14.202 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-18 05:34:14.202 2442-2442/net.simplifiedcoding.myfeed W/System.err: org.json.JSONException: No value for likes
03-18 05:34:14.202 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-18 05:34:14.202 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
03-18 05:34:14.203 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity.parseData(MainActivity.java:136)
03-18 05:34:14.203 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity.access$000(MainActivity.java:29)
03-18 05:34:14.203 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity$1.onResponse(MainActivity.java:94)
03-18 05:34:14.204 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity$1.onResponse(MainActivity.java:90)
03-18 05:34:14.204 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
03-18 05:34:14.204 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
03-18 05:34:14.204 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
03-18 05:34:14.204 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-18 05:34:14.208 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Looper.loop(Looper.java:148)
03-18 05:34:14.208 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
03-18 05:34:14.209 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
03-18 05:34:14.209 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-18 05:34:14.209 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-18 05:34:14.211 2442-2442/net.simplifiedcoding.myfeed D/Volley: [1] Request.finish: 10444 ms: [ ] http://10.0.2.2/feed.php?page=1 0x1e7db229 NORMAL 1
03-18 05:34:14.398 2442-2442/net.simplifiedcoding.myfeed W/ResourceType: No package identifier when getting value for resource number 0x00000000
03-18 05:34:14.398 2442-2442/net.simplifiedcoding.myfeed D/AndroidRuntime: Shutting down VM
03-18 05:34:14.404 2442-2442/net.simplifiedcoding.myfeed E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: net.simplifiedcoding.myfeed, PID: 2442
                                                                           android.content.res.Resources$NotFoundException: String resource ID #0x0
                                                                               at android.content.res.Resources.getText(Resources.java:312)
                                                                               at android.widget.TextView.setText(TextView.java:4417)
                                                                               at net.simplifiedcoding.myfeed.CardAdapter.onBindViewHolder(CardAdapter.java:78)
                                                                               at net.simplifiedcoding.myfeed.CardAdapter.onBindViewHolder(CardAdapter.java:21)
                                                                               at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
                                                                               at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
                                                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
                                                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
                                                                               at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
                                                                               at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
                                                                               at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
                                                                               at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
                                                                               at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
                                                                               at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                               at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                               at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                               at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
                                                                               at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
                                                                               at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                               at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                               at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                               at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                               at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                               at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-18 05:34:14.518 2442-2452/net.simplifiedcoding.myfeed I/art: Background partial concurrent mark sweep GC freed 814(168KB) AllocSpace objects, 0(0B) LOS objects, 23% free, 13MB/17MB, paused 4.712ms total 124.488ms
03-18 05:34:14.526 2442-2452/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 6.015ms
03-18 05:34:20.542 2442-2442/net.simplifiedcoding.myfeed I/Process: Sending signal. PID: 2442 SIG: 9

Upvotes: 1

Views: 4423

Answers (2)

Ravi Makvana
Ravi Makvana

Reputation: 2902

just simply do this

Replace this line

holder.txtCount.setText(superHeroes.get(position).getCount());

with this

holder.txtCount.setText(String.valueOf(superHeroes.get(position).getCount()));

your app gone crash because textView never set Integer value.

Upvotes: 2

Rohit Heera
Rohit Heera

Reputation: 2727

package net.simplifiedcoding.myfeed;
import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Belal on 11/9/2015.
 */
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
    //Imageloader to load image
    private ImageLoader imageLoader;
    private Context context;

    //List to store all superheroes
    List<SuperHero> superHeroes;

    //Constructor of this class
    public CardAdapter(List<SuperHero> superHeroes, Context context) {
        super();
        //Getting all superheroes
        this.superHeroes = superHeroes;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.superheroes_list, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder( final ViewHolder holder,    final int position) {
        //Getting the particular item from the list
          SuperHero superHero = superHeroes.get(position);

        //Loading image from url
        imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
        imageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView, R.drawable.image, android.R.drawable.ic_dialog_alert));

        //Showing data on the views
        holder.imageView.setImageUrl(superHero.getImageUrl(), imageLoader);
        holder.textViewName.setText(superHero.getName());
        holder.textViewPublisher.setText(superHero.getPublisher());
        holder.textViewLikes.setText(superHero.getLikes());



        holder.custom_button.setOnClickListener(new PlusListener(position,superHero,holder.txtCount));
        holder.txtCount.setText(superHeroes.get(position).getCount());


    }
    @Override
    public int getItemCount() {
        return superHeroes.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        //Views
        public NetworkImageView imageView;
        public TextView textViewName;
        public TextView textViewPublisher;
        public TextView textViewLikes;
        public TextView txtCount;
        public ImageButton custom_button;

        //Initializing Views
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
            textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
            textViewLikes = (TextView) itemView.findViewById(R.id.textViewlikes);
            txtCount = (TextView) itemView.findViewById(R.id.txtCount);
            custom_button = (ImageButton) itemView.findViewById(R.id.custom_button);
        }
    }
}


 class PlusListener implements View.OnClickListener
    {
    SuperHero Data;
    int position;
    TextView txtCount;
    Listener(int position,SuperHero Data,TextView txtCount)
    {
    this.Data = Data;
    this.position = position;
    this.txtCount= txtCount
    }
     @Override
            public void onClick(View view) {

      int count  = data.getCount();

            count = count+1;
            data.setCount(count);
                txtCount.setText(""+count);

            }
    }


class MinusListener implements View.OnClickListener
    {
    SuperHero Data;
    int position;
    TextView txtCount;
    Listener(int position,SuperHero Data,TextView txtCount)
    {
    this.Data = Data;
    this.position = position;
        this.txtCount= txtCount
    }
     @Override
            public void onClick(View view) {

            int count  = data.getCount();
if(count>0)
{
            count = count-1;
            data.setCount(count);
                txtCount.setText(""+count);
} 

 }
    }


public class SuperHero {

    //Data Variables
    private String imageUrl;
    private String name;
    private String publisher;
    private String likes;
    private int count;


    //Getters and Setters

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public String getLikes() {
        return likes;
    }

    public void setLikes(String likes) {
        this.likes = likes;
    }

    public int getCount() {
        return count ;
    }

    public void setCount(int count) {
        this.count = count;
    }




}

Upvotes: 0

Related Questions