0

我有两个按钮和音频列表视图,对于第一个按钮,我使用 LongClick 和 startActivityForResult 方法从 MediaPlayer 的另一个活动中获取字符串。但我也想使用第二个按钮。我需要使用 startActivityForResult 为另一个 MediaPlayer 获取另一个字符串,这是我的代码:

public class MainActivity extends Activity {
String setData, gotArnswer,s,s1;
MediaPlayer mMediaPlayer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMediaPlayer = new MediaPlayer();
        Button b1 =(Button) findViewById(R.id.button1);
        Button b2 =(Button) findViewById(R.id.button2);
        b2.setOnLongClickListener(new View.OnLongClickListener() {

            public boolean onLongClick(View arg0) {
                // TODO Auto-generated method stub

                return false;
            }
        });
        b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    if (mMediaPlayer.isPlaying()) {
                          mMediaPlayer.reset();
                 }
                  mMediaPlayer.setDataSource(s); //using string here
                  mMediaPlayer.prepare();
                 mMediaPlayer.start();
            } catch (Exception e) {

            }
            }
        });
        b1.setOnLongClickListener(new View.OnLongClickListener() {

            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this, Activityone.class);
                startActivityForResult(i, 0);
                return false;

            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            Bundle basket = data.getExtras();
             s = basket.getString("arnswer");

        }
    }

我的 ListView onClick()

public void onItemClick(AdapterView parent, View v, int position,
                        long id) {
  System.gc();
  music_column_index = musiccursor
    .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
  musiccursor.moveToPosition(position);
  String filename = musiccursor.getString(music_column_index);
  Intent person = new Intent();
  Bundle backpack = new Bundle();
  backpack.putString("arnswer", filename);
  person.putExtras(backpack);
  setResult(RESULT_OK, person);
  finish();
}

};
4

2 回答 2

1

所以基本上你需要知道请求代码,这是你传递给 startActivityForResult 的第二个参数。从第一个按钮调用时可以使用 0 作为请求代码,从第二个按钮调用时可以使用 1。

在第一次单击按钮时启动结果活动

 startActivityForResult(i, 0);

在第二个按钮单击时启动结果活动

 startActivityForResult(i, 1);

并且您需要在 onActivityResult(..) 方法中添加检查,以了解哪个活动返回的意图结果如下所示。

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
           if(requestCode == 0) {
            // handling code of startActivityForResult of first button
           Bundle basket = data.getExtras();
             s = basket.getString("arnswer");
            }
           if(requestCode == 1) {
            // handling code of startActivityForResult of second button
            Bundle basket = data.getExtras();
             s1 = basket.getString("arnswer");
             }

        }
    }

编辑:- 不要使用 0 和 1 作为请求代码,而是定义常量以便将来阅读。

于 2013-01-31T18:25:48.650 回答
0

您的代码部分没用

b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    if (mMediaPlayer.isPlaying()) {
                          mMediaPlayer.reset();
                 }
                  mMediaPlayer.setDataSource(s); //using string here
                  mMediaPlayer.prepare();
                 mMediaPlayer.start();
            } catch (Exception e) {

            }
            }
        });

只有这部分工作

    b1.setOnLongClickListener(new View.OnLongClickListener() {

        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(MainActivity.this, Activityone.class);
            startActivityForResult(i, 0);
            return false;

        }
    });

使用两个监听器 一个长按监听器 b1.setOnLongClickListener(new View.OnLongClickListener() {})

和其他类似的 b1.setOnClickListener(new OnClickListener(){})

然后您将能够一键完成两项工作

于 2013-01-31T18:23:34.613 回答