0

This is my Adapter class

public class NtindaCinemaAdapter  extends    RecyclerView.Adapter<NtindaCinemaViewHolders>{
    private   String url="http://10.0.2.2/UgandaEntertainment/Ntinda/NtindaPics/";
    private List<ItemObjects> itemList = null;
    private Context context;


    private ArrayList<ItemObjects> arraylist;
    public NtindaCinemaAdapter(Context context, List<ItemObjects> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public NtindaCinemaViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.ntinda_cinema_list, parent,false);
        NtindaCinemaViewHolders rcv = new NtindaCinemaViewHolders(layoutView,context);
        return rcv;
    }

    @Override
    public void onBindViewHolder(NtindaCinemaViewHolders holder, int position) {
        ItemObjects feedItem = itemList.get(position);
              ViewGroup parent;
        //Download image using picasso library

        Picasso.with(context).load(url+feedItem.getPhoto())
                .error(R.drawable.placeholder).
                resize(500,500)
                .placeholder(R.drawable.placeholder)

                .into(holder.moviepic);

        holder.moviename.setText(itemList.get(position).getMovieName());
    }

    @Override
    public int getItemCount() {
        return (null != itemList ? itemList.size() : 0);
    }
}

This is my ViewHolderClass where the problem is, I tried to follow each post posted here on the same issue but I haven't got the correct answer.
I want it to be like a listView with OnItemClickListener functionality.
And it sends data to the NextActivity

package oerrride.we.huzykamz.ugentapp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;
import java.util.List;

import adapters.NtindaCinemaAdapter;


public class NtindaCinemaViewHolders extends RecyclerView.ViewHolder  {

    public TextView moviename;
    public ImageView moviepic;
    public static String KEY_SYNOPSIS="Synopsis";
    View root;
    private List<ItemObjects> itemList1 = null;


    public NtindaCinemaViewHolders(final View itemView, final Context c) {
        super(itemView);
        root = itemView;

        root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            /*   Toast.makeText(c,"Clicked"+getPosition(),Toast.LENGTH_LONG).show();*/

                ItemObjects feedItem = (ItemObjects) v.getTag();
                Intent intent = new Intent(c, MovieScrollingActivity.class);

                intent.putExtra(KEY_SYNOPSIS, feedItem.getSynopsis());
                c.startActivity(intent);

            }
        });


        moviename = (TextView) itemView.findViewById(R.id.ntinda_cinema_txt);
        moviepic = (ImageView) itemView.findViewById(R.id.ntinda_cinema_photo);
    }
}

This is my NextActivity class

package oerrride.we.huzykamz.ugentapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

public class MovieScrollingActivity extends AppCompatActivity {

    TextView  synpsis_txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_scrolling);

        getSupportActionBar().setDisplayShowHomeEnabled(false);
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.hide();

        synpsis_txt = (TextView) findViewById(R.id.synopsis_txt);

        Bundle bundle = getIntent().getExtras();
        synpsis_txt.setText(bundle.getString(NtindaCinemaViewHolders.KEY_SYNOPSIS));

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab2);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

This is my model class

package oerrride.we.huzykamz.ugentapp;

public class ItemObjects {
    private String CinemaName;
    private String CinemaxPhoto;
    private String contact;
    private String longitude;
    private String latitude;
    private String MovieName;
    private String Synopsis;
    private String Others;
    private String photo;
    private String Monday;
    private String Tuesday;
    private String Wednesday;
    private String Thursday;
    private String Friday;
    private String Saturday;
    private String Sunday;
    private String ActorsName;
    private String ActorsPhoto;


    public String getCinemaName() {
        return CinemaName;
    }

    public void setCinemaName(String cinemaName) {
        CinemaName = cinemaName;
    }

    public String getCinemaxPhoto() {
        return CinemaxPhoto;
    }

    public void setCinemaxPhoto(String cinemaxPhoto) {
        CinemaxPhoto = cinemaxPhoto;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getMovieName() {
        return MovieName;
    }

    public void setMovieName(String movieName) {
        MovieName = movieName;
    }

    public String getSynopsis() {
        return Synopsis;
    }

    public void setSynopsis(String synopsis) {
        Synopsis = synopsis;
    }

    public String getOthers() {
        return Others;
    }

    public void setOthers(String others) {
        Others = others;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getMonday() {
        return Monday;
    }

    public void setMonday(String monday) {
        Monday = monday;
    }

    public String getTuesday() {
        return Tuesday;
    }

    public void setTuesday(String tuesday) {
        Tuesday = tuesday;
    }

    public String getWednesday() {
        return Wednesday;
    }

    public void setWednesday(String wednesday) {
        Wednesday = wednesday;
    }

    public String getThursday() {
        return Thursday;
    }

    public void setThursday(String thursday) {
        Thursday = thursday;
    }

    public String getFriday() {
        return Friday;
    }

    public void setFriday(String friday) {
        Friday = friday;
    }

    public String getSaturday() {
        return Saturday;
    }

    public void setSaturday(String saturday) {
        Saturday = saturday;
    }

    public String getSunday() {
        return Sunday;
    }

    public void setSunday(String sunday) {
        Sunday = sunday;
    }

    public String getActorsName() {
        return ActorsName;
    }

    public void setActorsName(String actorsName) {
        ActorsName = actorsName;
    }

    public String getActorsPhoto() {
        return ActorsPhoto;
    }

    public void setActorsPhoto(String actorsPhoto) {
        ActorsPhoto = actorsPhoto;
    }
}

This is the error message it displays

 08-02 11:49:12.123 1775-1775/? E/AndroidRuntime: FATAL EXCEPTION: main
 Process: oerrride.we.huzykamz.ugentapp, PID: 1775
 java.lang.NullPointerException
     at oerrride.we.huzykamz.ugentapp.NtindaCinemaViewHolders$1.onClick(NtindaCinemaViewHolders.java:52)
     at android.view.View.performClick(View.java:4438)
     at android.view.View$PerformClick.run(View.java:18422)
     at android.os.Handler.handleCallback(Handler.java:733)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:136)
     at android.app.ActivityThread.main(ActivityThread.java:5001)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:515)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
     at dalvik.system.NativeStart.main(Native Method)
Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
  • `v.getTag();` -- It can be null. Please debug and check it – OneCricketeer Aug 02 '16 at 16:28
  • what do you mean, coz i thought getTag() is an inbulit library, please try making it clear for me , coz am a beginner . –  Aug 02 '16 at 16:32
  • `Object getTag()` -- returns *the Object stored in this view as a tag, or **null** if not set*. You never called `setTag`, so why would you expect `getTag()` not to return null? – OneCricketeer Aug 02 '16 at 16:34
  • So how should i first setTag(), to accomplish all this , please cricket_007, guide me –  Aug 02 '16 at 16:38
  • Overall, I think you have the click listener in the wrong place because you need to be using the List object to get the item. `ItemObjects feedItem = itemList1.get(position);` – OneCricketeer Aug 02 '16 at 16:41

2 Answers2

0

I think you should move the click listener into the adapter, so you can get the object out of the arraylist.

Your problem was that v.getTag(); was returning null because the view didn't have a tag set to it.

@Override
public void onBindViewHolder(NtindaCinemaViewHolders holder, int position) {
    final ItemObjects feedItem = itemList.get(position);

    holder.root.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(NtindaCinemaAdapter.this.context, MovieScrollingActivity.class);
            intent.putExtra(NtindaCinemaViewHolders.KEY_SYNOPSIS, feedItem.getSynopsis());

            c.startActivity(intent);
        }
    });

    //Download image using picasso library
    Picasso.with(context).load(url+feedItem.getPhoto())
            .error(R.drawable.placeholder).
            resize(500,500)
            .placeholder(R.drawable.placeholder)
            .into(holder.moviepic);

    holder.moviename.setText(feedItem.getMovieName());
}
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
  • Thanks so much cricket_007 for the solution , now the problem is on the root , i still have the problem on the root , how should i declere the root , coz i have declared View root; but still its not seen , am trying to follow your steps you gave on seting the clickListener ? thanks –  Aug 02 '16 at 17:21
  • I know that you need to use `holder.root` to get a view. I am not sure if it will still be null. – OneCricketeer Aug 02 '16 at 17:22
  • @huxaiphaerIdris It should work the way I wrote, but you can make `public View root;` in the Holder class, if not. – OneCricketeer Aug 02 '16 at 17:25
  • Cant i use the Adapter class , coz onBindViewHolder is in that class , please help me , coz i tried also , public View root; in the Adapter class but its not accessible, isnt there any way of accessing it –  Aug 02 '16 at 17:30
  • @huxaiphaerIdris You should be able to use the adapter class, plus you need to. The ViewHolder only holds views, it does not know about the object being displayed in it. Can you please try moving the ViewHolder class as a nested class of the Adapter while I try to find the problem? – OneCricketeer Aug 02 '16 at 17:34
  • Copy that , let me try and see –  Aug 02 '16 at 17:36
  • 1
    hooray it has worked out , the problem was , i forgot to nest the ViewHolder Class , but your initial answer is correct , thanks man –  Aug 02 '16 at 17:49
-1

try to add this line before start activity

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Shailendra Kushwah
  • 399
  • 1
  • 4
  • 16