这是我的Adapter类
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);
}
}
这是我的ViewHolderClass问题所在,我试图按照同一问题发布在这里的每篇帖子,但我没有得到正确答案。
我希望它像一个带有OnItemClickListener功能的listView
它将数据发送到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);
}
}
这是我的NextActivity类
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);
}
}
这是我的模特课
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;
}
}
这是显示的错误消息
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)
答案 0 :(得分:0)
我认为你应该将点击监听器移动到适配器中,这样你就可以将对象从arraylist中取出。
您的问题是v.getTag();
返回null,因为该视图没有设置标记。
@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());
}
答案 1 :(得分:-1)
尝试在开始活动之前添加此行
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);