将列表信息传递给新活动

时间:2017-01-20 21:08:29

标签: java android android-layout android-activity

我建立了一个教程,我在其中创建了一个RecyclerView屏幕,其中包含带有可选选项的卡片。我想要一个可选择的选项,将用户带到一个新的activity,其中包含更多信息和更多信息。关于他们选择的卡的选项。我的问题是,当我尝试将该特定卡片的特征转移到下一个SlideViewActivity.java activity时,我无法成功执行此操作。我尝试将我的列表转换为array然后发送,但我一直在获取null值(这可能是由于我所知的所有语法)。

任何澄清&我们将非常感谢您的指导,如果您想要任何其他代码,请告诉我。

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
private Context mContext;
private List<Properties> dogList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title, count;
    public ImageView thumbnail, overflow;

    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title);
        count = (TextView) view.findViewById(R.id.count);
        thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
        overflow = (ImageView) view.findViewById(R.id.overflow);
    }
}

public Adapter(Context mContext, List<Properties> dogList) {
    this.mContext = mContext;
    this.dogList = dogList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card, parent, false);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    Properties dog = dogList.get(position);
    holder.title.setText(dog.getName());
    // loading dog cover using Glide library
    Glide.with(mContext).load(dog.getThumbnail()).into(holder.thumbnail);
    holder.overflow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showPopupMenu(holder.overflow);
        }
    });
}

/**
 * Showing popup menu when tapping on icon
 */
private void showPopupMenu(View view) {
    // inflate menu
    PopupMenu popup = new PopupMenu(mContext, view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu, popup.getMenu());
    popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
    popup.show();
}

/**
 * Click listener for popup menu items
 */
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {

    public MyMenuItemClickListener() {
    }

    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.action_add_favourite:
                Toast.makeText(mContext, "Add to favourite", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.action_more_info:
                Intent slideStart = new Intent(mContext, SlideViewActivity.class);
                String[] dogArray = new String[dogList.size()];
                slideStart.putExtra("List", dogArray);
                Log.e("putting extra", String.valueOf(dogArray[0]));
                //TODO:MAKE NAME TRANSFERS WORK
                mContext.startActivity(slideStart);
                return true;
            default:
        }
        return false;
    }
}

添加Properties.java:

public class Properties {
private String name;
private String info;
private int thumbnail;

public Properties() {
}

public Properties(String name, String info, int thumbnail) {
    this.name = name;
    this.info = info;
    this.thumbnail = thumbnail;
}

public String getName() {
    return name;
}

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

public String getInfo() {
    return info;
}

public void getInfo(String info) {
    this.info = info;
}

public int getThumbnail() {
    return thumbnail;
}

public void setThumbnail(int thumbnail) {
    this.thumbnail = thumbnail;
}
}

4 个答案:

答案 0 :(得分:1)

如果ArrayList<T>T,则可以传递Serializable

例如:

ArrayList<String> list = new ArrayList<String>();
intent.putExtra("list", list);

使用getSerializableExtra提取数据

答案 1 :(得分:0)

您的数组是dogList.size() - d值为null。

String[] dogArray = new String[dogList.size()];
slideStart.putExtra("List", dogArray);

您应该会在此处看到null

Log.e("putting extra", String.valueOf(dogArray[0]));

目前尚不清楚Properties的课程类型,但如果是您的课程,而不是java.util.Propertiesyou should implement Parcelable on that class,那么您就拥有了

intent.putParcelableArrayList("List", dogList)

提示:您应该将该类重命名为Dog以避免对自己和他人造成混淆)

但如果你只是担心得到null,那么案件很重要。您输入了密钥= "List",因此请确保您没有获得密钥= "list"或除"List"之外的任何其他内容

答案 2 :(得分:0)

可能的两种方式::

  1. 第一
  2. 使用intent ::

    发送列表时
    Intent slideStart = new Intent(mContext, SlideViewActivity.class);
    slideStart.putExtra("List", dogList);
    mContext.startActivity(slideStart);
    

    In这只是传递列表,因为它是::

    但您的课程Properties应该是&#34; Serializable&#34;

    现在收到那个意图::

    ArrayList<Properties> dogList =(ArrayList<Properties>) getIntent().getExtras().getSerializable("List");
    
    1. 第二种方式 ::使用库
    2. 有一个库用于将字符串转换为arraylist&amp;反之亦然,

      compile 'com.google.code.gson:gson:2.4'
      

      所以,为此,发送列表时将该列表转换为字符串,如::

      Type listType  = new TypeToken<List<Properties>>() {
                              }.getType();
      String listString=new Gson().toJson(dogList,listType );
      

      使用intent :::

      传递这个简单的字符串
      Intent slideStart = new Intent(mContext, SlideViewActivity.class);
      slideStart.putExtra("List", listString);
      mContext.startActivity(slideStart);
      

      在将其恢复到其他活动中时::

       String listString =getIntent().getExtras().getString("List");
      Type listType = new TypeToken<List<Properties>>() {}.getType();
       List<Properties> list=new Gson().fromJson(listString, listType);
      

      告诉我是否需要更多帮助..

答案 3 :(得分:0)

我做了一个类似的项目,我将分享我的代码。请看一看,如果有疑问请打电话

<强> DataAdapter.java

package com.example.vishnum.indiavideo;
import android.content.Context;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    private Context context;
    List<Video_Details> video;

    public DataAdapter(List<Video_Details> video, Context context) {
        super();
        this.context = context;
        this.video = video;
    }

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

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final Video_Details videoDetails =  video.get(position);
        String url;
        final String VideoID;

        holder.title.setText(video.get(position).getTitle());

        VideoID= video.get(position).getV_id();
        url = video.get(position).getThumb();

         Glide.with(context)
                 .load(url)
                 .override(150,70)
                 .into(holder.thumb);
       //viewHolder.thumb.setText(android.get(i).getVer());
      //  viewHolder.tv_api_level.setText(android.get(i).getApi());

        holder.vm.setOnClickListener(new View.OnClickListener() {
                                         @Override
                                         public void onClick(View v) {
                                             Toast.makeText(v.getContext(), "You Clicked"+video.get(position).getV_id(), Toast.LENGTH_SHORT).show();
                                             Intent intent = new Intent(v.getContext(),Play_Video.class);
                                             intent.putExtra("VideoId",(video.get(position).getV_id()));
                                             intent.putExtra("Title",(video.get(position).getTitle()));
                                             v.getContext().startActivity(intent);


                                         }
                                     }

        );
    }

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




    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView title;
        public ImageView thumb;
        public String videoid;
        public View vm;
        public ViewHolder(View view) {
            super(view);
            vm = view;
            title = (TextView)view.findViewById(R.id.title);
            thumb = (ImageView) view.findViewById(R.id.thumb);



            //tv_version = (TextView)view.findViewById(R.id.tv_version);
            //tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);

        }
    }


}

<强> Video_Details.java

package com.example.vishnum.indiavideo;
public class Video_Details {
    private String id;
    private String v_id;
    private String title;
    private String thumb;

    public String getId() {
        return id;
    }

    public String getV_id() {
        return v_id;
    }

    public String getTitle() {
        return title;
    }

    public String getThumb() {
        return (Constants.urlvideo+v_id+"/0.jpg");
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setV_id(String v_id) {
        this.v_id = v_id;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setThumb(String v_id) {
        this.thumb =Constants.urlvideo+v_id+"/0.jpg";
    }
}