如何使用intent.putextra设置videoview资源

时间:2018-10-18 03:07:42

标签: java android android-intent android-videoview

我的原始文件夹中有一些mp4视频,现在我想使用intent将这些视频传递到我的videoview。当用户单击某个项目时,将活动A中的多余内容投放到活动B中。

这是我的适配器类:

public class absadapter extends RecyclerView.Adapter<absadapter.exViewHolder> {

List<abs.Ex> exList;
Context context;
String ex1 = "android.resource://" + context.getPackageName() + "/" + R.raw.ex1;


absadapter(List exList,Context ctx) {
    this.exList= exList;
    context=ctx;
}
public static class exViewHolder extends RecyclerView.ViewHolder{
    CardView cardView;
    TextView exname;
    ImageView exlogo;


    exViewHolder(View itemView){
        super(itemView);
        cardView= itemView.findViewById(R.id.cardView);
        exname= itemView.findViewById(R.id.ex_name);
        exlogo=itemView.findViewById(R.id.exlogo);



    }


}
@Override
public absadapter.exViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View viewthigh= LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_abslayout,parent,false);
    absadapter.exViewHolder evh=new absadapter.exViewHolder(viewthigh);
    return evh;


}


@Override
public void onBindViewHolder(final absadapter.exViewHolder holder, final int position){
    holder.exname.setText(exList.get(position).name);
    holder.exlogo.setImageResource(exList.get(position).logoId);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (position == 0 ){
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.crunches);
                context.startActivity(i1);


            }
            if (position == 1) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.raw.ex1);
                i1.putExtra("text",R.string.declinecrunch);
                context.startActivity(i1);


            }
            if (position == 2) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.dumsidebend);
                context.startActivity(i1);

            }
            if (position == 3) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.hanglegraise);
                context.startActivity(i1);
                }
            if (position == 4) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.inclinelegraisegi);
                i1.putExtra("text",R.string.inclineleg);
                context.startActivity(i1);
                }
            if (position == 5) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.legraises);
                i1.putExtra("text",R.string.legraise);
                context.startActivity(i1);
            }
            if (position == 6) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.flatbenchlyingleg);
                context.startActivity(i1);
            }
            if (position == 7) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.jackknife);
                context.startActivity(i1);
            }
            if (position == 8) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.jackknifegif);
                i1.putExtra("text",R.string.jackknife);
                context.startActivity(i1);
            }
            if (position == 9) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.twisthip);
                i1.putExtra("text",R.string.twisthip);
                context.startActivity(i1);
            }
            if (position == 10) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.weightcrunch);
                context.startActivity(i1);
            }
            if (position == 11) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.plank);
                context.startActivity(i1);
            }
            if (position == 12) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.sideplank);
                context.startActivity(i1);
            }
            if (position == 13) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.superman);
                context.startActivity(i1);
            }
            if (position == 14) {
                Intent i1=new Intent(context,abdetails.class);

                i1.putExtra("text",R.string.twistcrunch);
                context.startActivity(i1);
            }


        }
    });


}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView){
    super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
    return  exList.size();
}

我想在bindviewholder内设置项目点击视频源的所有代码:

这是我的主班:

public class abdetails extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_abdetails);
    VideoView videoView;
    videoView=findViewById(R.id.vdvw);
    Intent i1=getIntent();

    TextView tv;


    tv = findViewById(R.id.exdesc);
    tv.setMovementMethod(new ScrollingMovementMethod());
    tv.setText(i1.getIntExtra("text",0));

    }

2 个答案:

答案 0 :(得分:1)

您不应传递整个视频。只需传递视频ID(或视频名称,...),然后在活动B中,再次访问raw文件夹即可获取目标视频

在您的适配器中,传递位置:

@Override
public void onBindViewHolder(final Absadapter.ExViewHolder holder, final int position){
    ...
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i1=new Intent(context, Abdetails.class);
            i1.putExtra("POSITION", position);
            context.startActivity(i1);
        }
    }
    ...
}

然后在您的AbDetailActivity中:

public class AbDetailActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_abdetails);

        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle = getIntent().getExtras();
            int position = bundle.getInt("POSITION", 0);

            switch (position) {
                case 0:
                    // Load video 0
                    break;

                case 1:
                    // Load video 1
                    break:
                ...
        }
    }
}

答案 1 :(得分:0)

我已经编辑了您的代码。希望它能解决您的问题。单击第一项时,以下代码将播放您的视频文件。我的意思是当position == 0;

public class absadapter extends RecyclerView.Adapter<absadapter.exViewHolder> {

List<abs.Ex> exList;
Context context;
String ex1 = "android.resource://" + context.getPackageName() + "/" + R.raw.ex1;

absadapter(List exList,Context ctx) {
    this.exList= exList;
    context=ctx;
}
public static class exViewHolder extends RecyclerView.ViewHolder{
    CardView cardView;
    TextView exname;
    ImageView exlogo;

    exViewHolder(View itemView){
        super(itemView);
        cardView= itemView.findViewById(R.id.cardView);
        exname= itemView.findViewById(R.id.ex_name);
        exlogo=itemView.findViewById(R.id.exlogo);

    }

}
@Override
public absadapter.exViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View viewthigh= LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_abslayout,parent,false);
    absadapter.exViewHolder evh=new absadapter.exViewHolder(viewthigh);
    return evh;
}

@Override
public void onBindViewHolder(final absadapter.exViewHolder holder, final int position){
    holder.exname.setText(exList.get(position).name);
    holder.exlogo.setImageResource(exList.get(position).logoId);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (position == 0 ){
                Intent i1=new Intent(context, abdetails.class);
                i1.putExtra("video", ex1);
                context.startActivity(i1);
            }
            if (position == 1) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.raw.ex1);
                i1.putExtra("text",R.string.declinecrunch);
                context.startActivity(i1);
            }
            if (position == 2) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.dumsidebend);
                context.startActivity(i1);
            }
            if (position == 3) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.hanglegraise);
                context.startActivity(i1);
                }
            if (position == 4) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.inclinelegraisegi);
                i1.putExtra("text",R.string.inclineleg);
                context.startActivity(i1);
                }
            if (position == 5) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.legraises);
                i1.putExtra("text",R.string.legraise);
                context.startActivity(i1);
            }
            if (position == 6) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.flatbenchlyingleg);
                context.startActivity(i1);
            }
            if (position == 7) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.jackknife);
                context.startActivity(i1);
            }
            if (position == 8) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.jackknifegif);
                i1.putExtra("text",R.string.jackknife);
                context.startActivity(i1);
            }
            if (position == 9) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("img",R.drawable.twisthip);
                i1.putExtra("text",R.string.twisthip);
                context.startActivity(i1);
            }
            if (position == 10) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.weightcrunch);
                context.startActivity(i1);
            }
            if (position == 11) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.plank);
                context.startActivity(i1);
            }
            if (position == 12) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.sideplank);
                context.startActivity(i1);
            }
            if (position == 13) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.superman);
                context.startActivity(i1);
            }
            if (position == 14) {
                Intent i1=new Intent(context,abdetails.class);
                i1.putExtra("text",R.string.twistcrunch);
                context.startActivity(i1);
            }
        }
    });

}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView){
    super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
    return  exList.size();
}

这是您的视频播放器活动。

public class abdetails extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_abdetails);
    VideoView videoView;
    videoView=findViewById(R.id.vdvw);
    Intent i1=getIntent();
    TextView tv;

    tv = findViewById(R.id.exdesc);
    tv.setMovementMethod(new ScrollingMovementMethod());
    tv.setText(i1.getIntExtra("text",0));

    if(i1 != null){
       String path = i1.getStringExtra("video", "");
       videoView.setVideoURI(Uri.parse(path));
       videoView.start();
       videoView.setVisibility(View.VISIBLE);
    }

    }