猿问

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

我的原始文件夹中有一些 mp4 视频,现在我想在用户单击某个项目时使用 Intent.put 从活动 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);


            }


胡子哥哥
浏览 138回答 2
2回答

30秒到达战场

你不应该通过整个视频。只需传递视频 ID(或视频名称,...),然后在活动 B 中,raw再次访问文件夹即可获取目标视频在您的适配器中,传递位置:@Overridepublic void onBindViewHolder(final Absadapter.ExViewHolder holder, final int position){&nbsp; &nbsp; ...&nbsp; &nbsp; holder.itemView.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context, Abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("POSITION", position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ...}然后在你的 AbDetailActivity 中:public class AbDetailActivity extends AppCompatActivity {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_abdetails);&nbsp; &nbsp; &nbsp; &nbsp; if (getIntent() != null && getIntent().getExtras() != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bundle bundle = getIntent().getExtras();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int position = bundle.getInt("POSITION", 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (position) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Load video 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Load video 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

qq_笑_17

我已经编辑了你的代码。希望它能解决你的问题。单击第一个项目时,下面的代码将播放您的视频文件。我的意思是当位置 == 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) {&nbsp; &nbsp; this.exList= exList;&nbsp; &nbsp; context=ctx;}public static class exViewHolder extends RecyclerView.ViewHolder{&nbsp; &nbsp; CardView cardView;&nbsp; &nbsp; TextView exname;&nbsp; &nbsp; ImageView exlogo;&nbsp; &nbsp; exViewHolder(View itemView){&nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; cardView= itemView.findViewById(R.id.cardView);&nbsp; &nbsp; &nbsp; &nbsp; exname= itemView.findViewById(R.id.ex_name);&nbsp; &nbsp; &nbsp; &nbsp; exlogo=itemView.findViewById(R.id.exlogo);&nbsp; &nbsp; }}@Overridepublic absadapter.exViewHolder onCreateViewHolder(ViewGroup parent, int viewType){&nbsp; &nbsp; View viewthigh= LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_abslayout,parent,false);&nbsp; &nbsp; absadapter.exViewHolder evh=new absadapter.exViewHolder(viewthigh);&nbsp; &nbsp; return evh;}@Overridepublic void onBindViewHolder(final absadapter.exViewHolder holder, final int position){&nbsp; &nbsp; holder.exname.setText(exList.get(position).name);&nbsp; &nbsp; holder.exlogo.setImageResource(exList.get(position).logoId);&nbsp; &nbsp; holder.itemView.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 0 ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context, abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("video", ex1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("img",R.raw.ex1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.declinecrunch);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.dumsidebend);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 3) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.hanglegraise);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 4) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("img",R.drawable.inclinelegraisegi);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.inclineleg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 5) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("img",R.drawable.legraises);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.legraise);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 6) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.flatbenchlyingleg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 7) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.jackknife);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 8) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("img",R.drawable.jackknifegif);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.jackknife);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 9) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("img",R.drawable.twisthip);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.twisthip);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.weightcrunch);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 11) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.plank);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 12) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.sideplank);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 13) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.superman);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position == 14) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i1=new Intent(context,abdetails.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i1.putExtra("text",R.string.twistcrunch);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}@Overridepublic void onAttachedToRecyclerView(RecyclerView recyclerView){&nbsp; &nbsp; super.onAttachedToRecyclerView(recyclerView);}@Overridepublic int getItemCount() {&nbsp; &nbsp; return&nbsp; exList.size();}这是您的视频播放器活动。public class abdetails extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_abdetails);&nbsp; &nbsp; VideoView videoView;&nbsp; &nbsp; videoView=findViewById(R.id.vdvw);&nbsp; &nbsp; Intent i1=getIntent();&nbsp; &nbsp; TextView tv;&nbsp; &nbsp; tv = findViewById(R.id.exdesc);&nbsp; &nbsp; tv.setMovementMethod(new ScrollingMovementMethod());&nbsp; &nbsp; tv.setText(i1.getIntExtra("text",0));&nbsp; &nbsp; if(i1 != null){&nbsp; &nbsp; &nbsp; &nbsp;String path = i1.getStringExtra("video", "");&nbsp; &nbsp; &nbsp; &nbsp;videoView.setVideoURI(Uri.parse(path));&nbsp; &nbsp; &nbsp; &nbsp;videoView.start();&nbsp; &nbsp; &nbsp; &nbsp;videoView.setVisibility(View.VISIBLE);&nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答