在android中正确传递数据?

发布于 2024-11-28 13:37:13 字数 2830 浏览 0 评论 0原文

我在我的第一个活动中有这个:

    private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener() 
    {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
    {
        // Now we want to actually get the data location of the file
        String [] proj={MEDIA_DATA};
        // We request our cursor again
        _cursor = managedQuery(_contentUri,
                proj, // Which columns to return
                null,       // WHERE clause; which rows to return (all rows)
                null,       // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)
        // We want to get the column index for the data uri
        int count = _cursor.getCount();
        //
        _cursor.moveToFirst();
        //
        _columnIndex = _cursor.getColumnIndex(MEDIA_DATA);
        // Lets move to the selected item in the cursor
        _cursor.moveToPosition(position);
        // And here we get the filename
        String filename = _cursor.getString(_columnIndex);
        //*********** You can do anything when you know the file path :-)
        showToast(filename);

        Intent i = new Intent("com.ave.EDITORSCREEN");
        i.putExtra("mnt/sdcard-ext", _ID);
        startActivity(i);

    }

这不是完整的代码,但完整的代码从 SD 卡收集所有视频缩略图并显示它们及其路径(在 toast 中)。我希望能够单击缩略图并将数据传递到下一个要播放、停止、暂停等的活动。您可以看到我在第一个活动中传递数据的位置:

public class Editor extends Activity {
ImageButton video1;
int isClicked = 0;
ImageButton audio;
int isClicked1 = 0;

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editor);

    video1 = (ImageButton) findViewById(R.id.video1);
    video1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (isClicked == 0) {
                video1.setImageResource(R.drawable.video_pressed);
                isClicked = 1;
            } else {
                video1.setImageResource(R.drawable.video1);
                isClicked = 0;
            }
          }
     });

    audio = (ImageButton) findViewById(R.id.audio);
    audio.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (isClicked1 == 0) {
                audio.setImageResource(R.drawable.audio_pressed);
                isClicked1 = 1;
            } else {
                audio.setImageResource(R.drawable.audio);
                isClicked1 = 0;
            }
          }
      });
    }
}

我想获取我的数据则需要把

String data = getIntent().getStringExtra("mnt/sdcard-ext");

onCreate方法放在哪里?或者这是否是获取传递数据的正确方法?最后我怎样才能播放视频?是否有某种视频播放器的代码?如果是这样,我会将其放置在我的最终活动中的什么位置?

I have this in my first activity:

    private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener() 
    {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
    {
        // Now we want to actually get the data location of the file
        String [] proj={MEDIA_DATA};
        // We request our cursor again
        _cursor = managedQuery(_contentUri,
                proj, // Which columns to return
                null,       // WHERE clause; which rows to return (all rows)
                null,       // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)
        // We want to get the column index for the data uri
        int count = _cursor.getCount();
        //
        _cursor.moveToFirst();
        //
        _columnIndex = _cursor.getColumnIndex(MEDIA_DATA);
        // Lets move to the selected item in the cursor
        _cursor.moveToPosition(position);
        // And here we get the filename
        String filename = _cursor.getString(_columnIndex);
        //*********** You can do anything when you know the file path :-)
        showToast(filename);

        Intent i = new Intent("com.ave.EDITORSCREEN");
        i.putExtra("mnt/sdcard-ext", _ID);
        startActivity(i);

    }

This is not the full code, but as the full code gathers all the video thumbnails from the sd card and displays them along with their path's (in toasts). I want to be able to click a thumbnail and have the data passed on to the next activity to be played, stopped, paused, etc. You can see where I have passed the data in the first activity:

public class Editor extends Activity {
ImageButton video1;
int isClicked = 0;
ImageButton audio;
int isClicked1 = 0;

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editor);

    video1 = (ImageButton) findViewById(R.id.video1);
    video1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (isClicked == 0) {
                video1.setImageResource(R.drawable.video_pressed);
                isClicked = 1;
            } else {
                video1.setImageResource(R.drawable.video1);
                isClicked = 0;
            }
          }
     });

    audio = (ImageButton) findViewById(R.id.audio);
    audio.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (isClicked1 == 0) {
                audio.setImageResource(R.drawable.audio_pressed);
                isClicked1 = 1;
            } else {
                audio.setImageResource(R.drawable.audio);
                isClicked1 = 0;
            }
          }
      });
    }
}

I imagine to get the data I will need to put

String data = getIntent().getStringExtra("mnt/sdcard-ext");

But where the onCreate method? Or is this even the right way to get the passed data? And lastly how can I can I play the video? Is there some sort of code for say a video player? If so where would I place this in my final activity?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情出卖 2024-12-05 13:37:13

您不必在 onCreate() 方法中执行此操作,但为什么不呢?你还会把它放在哪里?

如果您正在寻找任何类型的文档,我建议您查看 Android 开发人员 Java (Android)文档Android 开发者 总体来说有一个很棒的网站,全部都是关于 Android 的。我认为 音频/视频在这里是开始接触 Android 的好地方。

通常,您可以在 onCreate() 中设置 MediaPlayer 界面(播放/暂停按钮和视图)的布局,并设置在收到输入时执行的单击/触摸侦听器。

对于第一门编程课程,看起来你做得很好。祝你好运!

You do not have to do it in the onCreate() method, but why not? Where else would you put it?

If you are looking for any sort of documentation, I would recommend checking out the Android Developers Java(Android)Doc. The Android Developers have a great site in general, all about Android. I think a good place to start with Android Audio/Video is here.

Generally, you would set up the layout of your MediaPlayer interface (Play/Pause buttons and views) in onCreate() and also setup your click/touch listeners to be executed when input is received.

For a first programming course, it looks like you are doing pretty well. Good Luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文