从我的回收适配器的点击功能传递数据
passing data from on click function of my recycler adaptor
public void onClick(View v) {
Intent intent = new Intent(context,notification.class);
Bundle bundle = new Bundle();
bundle.putSerializable("DATA",listmsgs.get(getPosition()));
intent.putExtras(bundle);
context.startActivity(intent);
}
在 bundle 对象中,我正在获取项目点击的数据,当我尝试在通知中使用此数据时 activity 我没有获取数据,我只获取了一个值。或建议我任何其他解决方案
这是我的通知activity
notif data = (notif)getIntent().getExtras().getSerializable("DATA");
title=data.getTitle();
tim=data.gettimestamp();
msg=data.getmsg();
您需要为此实现接口。
在您的适配器 class 中,在 Holder 构造函数中写入以下代码
itemView.setClickable(true);
itemView.setOnClickListener(this);
现在为您的 Holder class 实施 OnClickListener。执行此操作后,您将获得 onClick 方法。
@Override
public void onClick(View v) {
if (itemClick != null) {
itemClick.onItemClicked(getPosition());
}
}
创建一个界面class作为OnItemClick
public interface OnItemClick {
public void onItemClicked(int position);
}
并在您的适配器中创建它的对象 class 并生成它的 getter/setter 方法。
现在在你的 Activity 中,在将你的 RecyclerView 与适配器绑定下编写以下代码:
adapter.setItemClick(this);
执行此操作时,系统会要求您对 Activity 实施 OnItemClick。
在实现 onItemClicked 后,您将能够执行点击事件。
@Override
public void onItemClicked(int position) {
Intent intent = new Intent(context,notification.class);
Bundle bundle = new Bundle();
bundle.putSerializable("DATA",listmsgs.get(position));
intent.putExtras(bundle);
startActivity(intent);
}
完整代码如下:
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClick {
private List<Person> persons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeData();
RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
RVAdapter adapter = new RVAdapter(persons);
rv.setAdapter(adapter);
adapter.setItemClick(this);
}
private void initializeData() {
persons = new ArrayList<>();
persons.add(new Person("Emma Wilson", "23 years old", R.drawable.ic_launcher));
persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.ic_launcher));
persons.add(new Person("Lillie Watts", "35 years old", R.drawable.ic_launcher));
persons.add(new Person("Emma Wilson", "23 years old", R.drawable.ic_launcher));
persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.ic_launcher));
persons.add(new Person("Lillie Watts", "35 years old", R.drawable.ic_launcher));
persons.add(new Person("Emma Wilson", "23 years old", R.drawable.ic_launcher));
persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.ic_launcher));
persons.add(new Person("Lillie Watts", "35 years old", R.drawable.ic_launcher));
persons.add(new Person("Emma Wilson", "23 years old", R.drawable.ic_launcher));
persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.ic_launcher));
persons.add(new Person("Lillie Watts", "35 years old", R.drawable.ic_launcher));
}
@Override
public void onItemClicked(int position) {
Toast.makeText(getBaseContext(), ""+persons.get(position).name, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,Demo.class);
Bundle bundle = new Bundle();
bundle.putSerializable("DATA",persons.get(position));
intent.putExtras(bundle);
startActivity(intent);
}
}
适配器class:
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
List<Person> persons;
private OnItemClick itemClick;
public class PersonViewHolder extends RecyclerView.ViewHolder implements
OnClickListener {
CardView cv;
TextView personName, personAge;
ImageView personPhoto;
PersonViewHolder(View itemView) {
super(itemView);
itemView.setClickable(true);
itemView.setOnClickListener(this);
cv = (CardView) itemView.findViewById(R.id.cv);
personName = (TextView) itemView.findViewById(R.id.person_name);
personAge = (TextView) itemView.findViewById(R.id.person_age);
personPhoto = (ImageView) itemView.findViewById(R.id.person_photo);
}
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
if (itemClick != null) {
itemClick.onItemClicked(getPosition());
}
}
}
public RVAdapter(List<Person> persons) {
this.persons = persons;
}
@Override
public int getItemCount() {
return persons.size();
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int pos) {
personViewHolder.personName.setText(persons.get(pos).name);
personViewHolder.personAge.setText(persons.get(pos).age);
personViewHolder.personPhoto.setImageResource(persons.get(pos).photoId);
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int arg1) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.item, viewGroup, false);
PersonViewHolder holder = new PersonViewHolder(view);
return holder;
}
public OnItemClick getItemClick() {
return itemClick;
}
public void setItemClick(OnItemClick itemClick) {
this.itemClick = itemClick;
}
}
型号class:
import java.io.Serializable;
@SuppressWarnings("serial")
public class Person implements Serializable {
String name;
String age;
int photoId;
Person(String name, String age, int photoId) {
this.name = name;
this.age = age;
this.photoId = photoId;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Demo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Person person = (Person) getIntent().getExtras()
.getSerializable("DATA");
Log.e("", "===== " + person.name);
}
}
xml 个文件:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
item.xml
<?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="match_parent"
android:padding="16dp" >
<android.support.v7.widget.CardView
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp" >
<ImageView
android:id="@+id/person_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:contentDescription="@string/app_name" />
<TextView
android:id="@+id/person_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/person_photo"
android:layout_toRightOf="@+id/person_photo"
android:textSize="30sp" />
<TextView
android:id="@+id/person_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/person_name"
android:layout_toEndOf="@+id/person_photo"
android:layout_toRightOf="@+id/person_photo" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
将 recyclerview 和 cardview 外部库添加到您的项目中。
public void onClick(View v) {
Intent intent = new Intent(context,notification.class);
Bundle bundle = new Bundle();
bundle.putSerializable("DATA",listmsgs.get(getPosition()));
intent.putExtras(bundle);
context.startActivity(intent);
}
在 bundle 对象中,我正在获取项目点击的数据,当我尝试在通知中使用此数据时 activity 我没有获取数据,我只获取了一个值。或建议我任何其他解决方案
这是我的通知activity
notif data = (notif)getIntent().getExtras().getSerializable("DATA");
title=data.getTitle();
tim=data.gettimestamp();
msg=data.getmsg();
您需要为此实现接口。 在您的适配器 class 中,在 Holder 构造函数中写入以下代码
itemView.setClickable(true);
itemView.setOnClickListener(this);
现在为您的 Holder class 实施 OnClickListener。执行此操作后,您将获得 onClick 方法。
@Override
public void onClick(View v) {
if (itemClick != null) {
itemClick.onItemClicked(getPosition());
}
}
创建一个界面class作为OnItemClick
public interface OnItemClick {
public void onItemClicked(int position);
}
并在您的适配器中创建它的对象 class 并生成它的 getter/setter 方法。
现在在你的 Activity 中,在将你的 RecyclerView 与适配器绑定下编写以下代码:
adapter.setItemClick(this);
执行此操作时,系统会要求您对 Activity 实施 OnItemClick。 在实现 onItemClicked 后,您将能够执行点击事件。
@Override
public void onItemClicked(int position) {
Intent intent = new Intent(context,notification.class);
Bundle bundle = new Bundle();
bundle.putSerializable("DATA",listmsgs.get(position));
intent.putExtras(bundle);
startActivity(intent);
}
完整代码如下:
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClick {
private List<Person> persons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeData();
RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
RVAdapter adapter = new RVAdapter(persons);
rv.setAdapter(adapter);
adapter.setItemClick(this);
}
private void initializeData() {
persons = new ArrayList<>();
persons.add(new Person("Emma Wilson", "23 years old", R.drawable.ic_launcher));
persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.ic_launcher));
persons.add(new Person("Lillie Watts", "35 years old", R.drawable.ic_launcher));
persons.add(new Person("Emma Wilson", "23 years old", R.drawable.ic_launcher));
persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.ic_launcher));
persons.add(new Person("Lillie Watts", "35 years old", R.drawable.ic_launcher));
persons.add(new Person("Emma Wilson", "23 years old", R.drawable.ic_launcher));
persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.ic_launcher));
persons.add(new Person("Lillie Watts", "35 years old", R.drawable.ic_launcher));
persons.add(new Person("Emma Wilson", "23 years old", R.drawable.ic_launcher));
persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.ic_launcher));
persons.add(new Person("Lillie Watts", "35 years old", R.drawable.ic_launcher));
}
@Override
public void onItemClicked(int position) {
Toast.makeText(getBaseContext(), ""+persons.get(position).name, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,Demo.class);
Bundle bundle = new Bundle();
bundle.putSerializable("DATA",persons.get(position));
intent.putExtras(bundle);
startActivity(intent);
}
}
适配器class:
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
List<Person> persons;
private OnItemClick itemClick;
public class PersonViewHolder extends RecyclerView.ViewHolder implements
OnClickListener {
CardView cv;
TextView personName, personAge;
ImageView personPhoto;
PersonViewHolder(View itemView) {
super(itemView);
itemView.setClickable(true);
itemView.setOnClickListener(this);
cv = (CardView) itemView.findViewById(R.id.cv);
personName = (TextView) itemView.findViewById(R.id.person_name);
personAge = (TextView) itemView.findViewById(R.id.person_age);
personPhoto = (ImageView) itemView.findViewById(R.id.person_photo);
}
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
if (itemClick != null) {
itemClick.onItemClicked(getPosition());
}
}
}
public RVAdapter(List<Person> persons) {
this.persons = persons;
}
@Override
public int getItemCount() {
return persons.size();
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int pos) {
personViewHolder.personName.setText(persons.get(pos).name);
personViewHolder.personAge.setText(persons.get(pos).age);
personViewHolder.personPhoto.setImageResource(persons.get(pos).photoId);
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int arg1) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.item, viewGroup, false);
PersonViewHolder holder = new PersonViewHolder(view);
return holder;
}
public OnItemClick getItemClick() {
return itemClick;
}
public void setItemClick(OnItemClick itemClick) {
this.itemClick = itemClick;
}
}
型号class:
import java.io.Serializable;
@SuppressWarnings("serial")
public class Person implements Serializable {
String name;
String age;
int photoId;
Person(String name, String age, int photoId) {
this.name = name;
this.age = age;
this.photoId = photoId;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Demo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Person person = (Person) getIntent().getExtras()
.getSerializable("DATA");
Log.e("", "===== " + person.name);
}
}
xml 个文件:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
item.xml
<?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="match_parent"
android:padding="16dp" >
<android.support.v7.widget.CardView
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp" >
<ImageView
android:id="@+id/person_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:contentDescription="@string/app_name" />
<TextView
android:id="@+id/person_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/person_photo"
android:layout_toRightOf="@+id/person_photo"
android:textSize="30sp" />
<TextView
android:id="@+id/person_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/person_name"
android:layout_toEndOf="@+id/person_photo"
android:layout_toRightOf="@+id/person_photo" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
将 recyclerview 和 cardview 外部库添加到您的项目中。