我正在开发一个Android项目,我需要创建两个活动,每个活动都有一个cardViews列表:ListServices.Java
和HomePage.java
我希望如果我点击ListServices中的cardview,我会被重定向回HomePage,同样的cardView应该被创建/添加到cardViews的主页列表中。 在我的日志中,我可以在HomePage.java的列表中看到添加的cardView,但它不会在视图中显示为卡片。
这是我到目前为止所做的:
首页:
private RecyclerView recyclerView;
public static ServicesAdapter homeAdapter;
public static List<Service> serviceList;
final static String ADD_NEW_ITEM_TYPE="add new item";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//cards
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
serviceList = new ArrayList<>();
homeAdapter = new ServicesAdapter(this, serviceList);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(homeAdapter);
initServices();
}
public void initServices () {
Service a = new Service(ADD_NEW_ITEM_TYPE);
serviceList.add(a);
homeAdapter.notifyDataSetChanged();
}
和此活动的适配器:
public class ServicesAdapter extends RecyclerView.Adapter<ServicesAdapter.MyViewHolder> {
private Context mContext;
private List<Service> serviceList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//click on new item homepage
if(title.getText().toString().equals(ADD_NEW_ITEM_TYPE)) {
Intent redirect = new Intent(v.getContext(), ListServices.class);
v.getContext().startActivity(redirect);
}
}
});//end onclick listener
}
}
public ServicesAdapter(Context mContext, List<Service> serviceList) {
this.mContext = mContext;
this.serviceList = serviceList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.service_card, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
Service serv = serviceList.get(position);
holder.title.setText(serv.getName());
}
@Override
public int getItemCount() {
return serviceList.size();
}
}
ListServices:
RecyclerView recyclerView;
private ServiceListAdapter adapter;
private List<Service> servicesArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_liste_services);
//cards
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
servicesArray = new ArrayList<>();
adapter = new ServiceListAdapter(this, servicesArray);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new HomePageServices.GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
prepareServices();
}
private void prepareServices() {
Service a = new Service("Indoor Geolocation");
servicesArray.add(a);
a = new Service("Restauration du cartier");
servicesArray.add(a);
a = new Service("Signaler a dysfonctionnement");
servicesArray.add(a);
a = new Service("Boite à idée");
servicesArray.add(a);
a = new Service("Activités inter-employés");
servicesArray.add(a);
adapter.notifyDataSetChanged();
}
它是适配器:
public class ServiceListAdapter extends RecyclerView.Adapter<ServiceListAdapter.MyHolder> {
private Context mContext;
private List<Service> servicesArray;
public class MyHolder extends RecyclerView.ViewHolder {
public TextView title;
public MyHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//disable card
//redirect to homepage
Intent redirect = new Intent(v.getContext(), HomePageServices.class);
v.getContext().startActivity(redirect);
//create new card view
Service s=new Service(title.getText().toString());
serviceList.add(s);
homeAdapter.notifyDataSetChanged();
for (int i =0; i<serviceList.size(); i++){
Log.d("serviceLoist", serviceList.get(i).toString());
}
}
});//end onclick listener
}
}
public ServiceListAdapter(Context mContext, List<Service> serviceList) {
this.mContext = mContext;
this.servicesArray = serviceList;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.service_card, parent, false);
return new MyHolder(itemView);
}
@Override
public void onBindViewHolder(final MyHolder holder, int position) {
Service serv = servicesArray.get(position);
holder.title.setText(serv.getName());
}
@Override
public int getItemCount() {
return servicesArray.size();
}
}
所以你可以看到问题在这里:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//disable card
//redirect to homepage
Intent redirect = new Intent(v.getContext(), HomePageServices.class);
v.getContext().startActivity(redirect);
//create new card view
Service s=new Service(title.getText().toString());
serviceList.add(s);
homeAdapter.notifyDataSetChanged();
谢谢