如何将视频放在ListView的每个元素中?

时间:2018-09-26 18:49:14

标签: java android

我有一个由list_items制成并带有照片和文字的ListView 我希望当我单击“列表视图”的每个元素时,出现一个不同的视频。 目前,我只能播放视频,而第一个视频是我有条件切换的。

如何使每个视频与列表视图位置相关联?

我给你代码

MainActivity:

public class MainActivity extends AppCompatActivity {
   AdRequest adRequest;
   private int clicks = 0;
   public ArrayList<Crash> crash;
   public ListView listView;
   private AdView mAdView;
   public InterstitialAd mInterstitialAd;
   Video video = new video();

   class List implements OnItemClickListener {

       List() {}

       public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
           Intent intent = new Intent(MainActivity.this, Video.class);
           intent.putExtra("CRASH1", ((Crashes) MainActivity.this.crash.get(i)).getName());
           MainActivity.this.startActivity(intent);
       }
   }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView( R.layout.activity_main);

        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        ((TextView) findViewById(R.id.title)).setTypeface(Typeface.createFromAsset(getAssets(), “typeoffont”));
        this.mInterstitialAd = new InterstitialAd(this);
        this.mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        this.mInterstitialAd.loadAd(new Builder().build());
        this.listView = (ListView) findViewById(R.id.crash_list);
        this.crash = new ArrayList();

        listItem();

        this.listView.setAdapter(new CrashAdapter(this, this.crash));
        this.listView.setOnItemClickListener(new List());
        MobileAds.initialize(this, "");
        this.mAdView = (AdView) findViewById(R.id.example);
        this.mAdView.loadAd(new Builder().build());
    }

    public void listItem() {
        this.crash.add(new Crash(“Whatever”, “12412341212342”, “Hi1”));
        this.crash.add(new Crash(“Why”, “123123123“, “Hi2”));
        this.crash.add(new Crash(“What”, "5b3358d1bbe87a4fd56aac4b", “Hi3”));
    }
}

适配器类

public class EmotesAdapter extends BaseAdapter {
    public Context context;
    public ArrayList<Crash> crash;

    public long getItemId(int i) {
        return (long) i;
    }

    public CrashAdapter(Context context, ArrayList<Crash> arrayList) {
        this.context = context;
        this.emotes = arrayList;
    }

    public int getCount() {
        return this.crash.size();
    }

    public Object getItem(int i) {
        return this.crash.get(i);
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        view = View.inflate(this.context, R.layout.crash, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.crash_icon);

        TextView textView = (TextView) view.findViewById(R.id.crash_name);

        TextView textView2 = (TextView) view.findViewById(R.id.crash_number);

        Crash crashes = (Crash) this.crash.get(i);
        RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.video_image_background);

        Picasso.get().load("file:///android_asset/a" + crash.get(i).getUrl() + ".png").into(imageView);

        textView.setTypeface(Typeface.createFromAsset(this.context.getAssets(), “type font”));

        textView2.setTypeface(Typeface.createFromAsset(this.context.getAssets(), “type font”));
        textView.setText(crash.getName());
        textView2.setText(crash.getNumber());
        if (i < 21) {

        return view;
    }
}

视频课

public class Video extends AppCompatActivity {
    public VideoView videoView;

    int i = 0;

    class OnPrepared implements OnPreparedListener {
        OnPrepared() {}

        public void onPrepared(MediaPlayer mediaPlayer) {
            mediaPlayer.setLooping(true);
        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.video_menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() == R.id.cancel) {
            finish();
        }
        return true;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_video);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        getWindow().setLayout((int) (((double) metrics.widthPixels) * 0.7d), (int) (((double) metrics.heightPixels) * 0.5d));
        Bundle extras = getIntent().getExtras();

        videoView = findViewById(R.id.videoview);
        videoView.setVideoURI(Uri.parse(“CRASH1”));
        setVideo();
        videoView.start();

        videoView.setOnPreparedListener(new OnPrepared());
    }

    public void setVideo () {
        switch (i) {
            case 0:
                videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.crone));
                i++;
                break;

            case 1:
                videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.crtwo);
                break;

            case 2:
                videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.crthree));
                break;
        }
        videoView.setMediaController(new MediaController(Video.this));
        videoView.requestFocus();
        videoView.start();
    }
}

您能帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试在适配器类的getView()中添加以下代码:

view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                // Do what you want here when item is clicked
                Intent intent = new Intent(view.getContext(), Video.class);
                intent.putExtra("CRASH1", crash.getName());
                 view.getContext().startActivity(intent);
                }
            });

我不确定onClick()中的代码是否没有错误,但这是一般概念。请让我知道它是否对您有用。