【问题标题】:How to implement the prev and next button in second activity using Simpleadapter in Fragment?如何使用 Fragment 中的 Simpleadapter 在第二个活动中实现上一个和下一个按钮?
【发布时间】:2016-02-11 07:20:10
【问题描述】:

我使用 Simpleadapter 在 Fragment 中集成了一个无线电频道列表。现在我想在第二个活动中实现上一个和下一个按钮以在列表之间切换。

现在我使用 Bundle 传递数据,但通过单击下一个按钮,我可以只增加一次位置,在第二次单击时应用程序停止。 我的错误是什么?

1) 片段

public class RadioFragment extends ListFragment {

 String[] radioLink = new String[]{
        "http://vivisolive-lh.akamaihd.net/i/syg_rd_berfin_1@383073/index_128_a-b.m3u8?sd=10&rebase=on",
        "http://yayin.firatfm.net:2021",
        "http://95.173.188.155:9984/;",





};
//Med Nûçe
// Array of strings storing country names
String[] radioName = new String[]{
        "Dengê Kûrdî",
        "Firat Fm",
        "Radyo Dengê Kûrdî",

};

// Array of integers points to images stored in /res/drawable/
int[] radioLogo = new int[]{
        R.drawable.ic_menu_camera,
        R.drawable.ic_menu_camera,
        R.drawable.ic_menu_camera,




};

// Array of strings to store Radio_Number
String[] channel_number = new String[100];
private Activity activity;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Each row in the list stores country name, currency and flag
   List<HashMap<String, String>> bList = new ArrayList<HashMap<String, String>>();


    //fill the TV Number list
    int count ;
    for(count =1; count< radioLink.length+1;count++){
        channel_number[count-1]= "Radio: "+count;

    }


    for (int i = 0; i < radioLink.length; i++) {
        HashMap<String, String> hm = new HashMap<String, String>();
        hm.put("number_radio", "channel : " + radioName[i]);
        hm.put("name_radio", "number : " + channel_number[i]);
        hm.put("logo_radio", Integer.toString(radioLogo[i]));
        bList.add(hm);
    }





    // Keys used in Hashmap
    String[] from = {"logo_radio", "number_radio", "name_radio"};

    // Ids of views in listview_layout
    int[] to = {R.id.logo_radio, R.id.number_radio, R.id.name_radio};

    // Instantiating an adapter to store each items
    // R.layout.listview_layout defines the layout of each item
    SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), bList, R.layout.fragment_radio, from, to);

    setListAdapter(adapter);
    //Data From Fragment to Activity





    return super.onCreateView(inflater, container, savedInstanceState);
}


@Override
public void onStart() {
    super.onStart();


    getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getActivity(), radioName[position], Toast.LENGTH_SHORT).show();


            Bundle bundle_send = new Bundle();

            Intent intent = new Intent(getActivity(), RadioPlayActivity.class);


            switch (position) { 
                case 0:

                    Toast.makeText(getActivity(), radioLink[position], Toast.LENGTH_SHORT).show();

                    bundle_send.putString("radiodata", radioLink[position]);
                    bundle_send.putInt("position", position);

                    bundle_send.putStringArray("array", radioLink);

                    intent.putExtras(bundle_send);
                    startActivity(intent);

                    break;
                case 1:

                    Toast.makeText(getActivity(), radioLink[position], Toast.LENGTH_SHORT).show();

                    bundle_send.putString("radiodata", radioLink[position]);
                    bundle_send.putInt("position", position);

                    bundle_send.putStringArray("array", radioLink);

                    intent.putExtras(bundle_send);
                    startActivity(intent);

                    break;
                case 2:

                    Toast.makeText(getActivity(), radioLink[position], Toast.LENGTH_SHORT).show();

                    bundle_send.putString("radiodata", radioLink[position]);
                    bundle_send.putInt("position", position);

                    bundle_send.putStringArray("array", radioLink);

                    intent.putExtras(bundle_send);
                    startActivity(intent);

                    break;




                default:

            }


        }
    });
}

}

通过逐个单击列表视图项目,我可以将数据从 RadioFragment 发送到 RadioActivity 并可以播放我的 Radio。

现在我使用 Bundle 传递数据,但通过单击下一个按钮,我可以只增加一次位置,在第二次单击时应用程序停止。 我的错误是什么?

1) RadioActivity

公共类 RadioPlayActivity 扩展 AppCompatActivity 实现 OnClickListener {

private ProgressBar playSeekBar;

private Button buttonPlay;

private Button buttonStopPlay;

private MediaPlayer player = new MediaPlayer();;

private Button buttonPrevious;
private Button buttonNext;

private int playerId=0;
private int newId = 0;
private String[] array;
private int Counter=0;
private int pos=0;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.radiobuffer);


    initializeUIElements();

    initializeMediaPlayer();


}

private void initializeUIElements() {


    playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
    playSeekBar.setMax(100);
    playSeekBar.setVisibility(View.INVISIBLE);

    buttonPlay = (Button) findViewById(R.id.buttonPlay);
    buttonPlay.setOnClickListener(this);

    buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
    buttonStopPlay.setEnabled(false);
    buttonStopPlay.setOnClickListener(this);


}


public void onClick(View v) {
    if (v == buttonPlay) {
        startPlaying();
    } else if (v == buttonStopPlay) {
        stopPlaying();
    }
}

private void startPlaying() {
    buttonStopPlay.setEnabled(true);
    buttonPlay.setEnabled(false);

    playSeekBar.setVisibility(View.VISIBLE);

    player.prepareAsync();

    player.setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            player.start();
        }
    });

}

private void stopPlaying() {
    if (player.isPlaying()) {
        player.stop();
        player.release();
        initializeMediaPlayer();
    }

    buttonPlay.setEnabled(true);
    buttonStopPlay.setEnabled(false);
    playSeekBar.setVisibility(View.INVISIBLE);
}

private void initializeMediaPlayer() {
    //Get Radio_Data from RadioFragment
    Bundle bundle_get = getIntent().getExtras();
    // final String tvListTvFragment = zielkorb.getString("radiodata");
    pos = bundle_get.getInt("position");
    array = bundle_get.getStringArray("array");

    try {

        //player.setDataSource(tvListTvFragment);

        Toast.makeText(RadioPlayActivity.this, String.valueOf(pos), Toast.LENGTH_SHORT).show();
      //  player.setDataSource(array[position]);

        buttonNext = (Button) findViewById(R.id.button_next);


buttonNext.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

        int position = pos + 1;

        try {
            Toast.makeText(RadioPlayActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
            player.setDataSource(array[position]);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (!(position >= array.length)) {
            pos += 1;

        } else {
            pos = -1;
        }



    }
});


    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }


    player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            playSeekBar.setSecondaryProgress(percent);
            Log.i("Buffering", "" + percent);
        }
    });
}

@Override
protected void onPause() {
    super.onPause();
    if (player.isPlaying()) {
        player.stop();
    }
}

}

【问题讨论】:

  • 您好,您能详细说明一下,单击“上一个”按钮和单击“下一个”按钮时要显示什么?

标签: android listview button android-activity simpleadapter


【解决方案1】:

我想切换我的收音机列表,例如从列表位置 1 到 2. 活动中的列表位置,方法是单击下一个或上一个按钮。我想在一个活动中播放所有广播频道。

【讨论】:

  • 有人知道如何解决我的问题吗?请帮忙。
  • 这是我的解决方案:我的问题是“player.setDatasource()”的 Try-Catch 块。我将 catch(IOException e) 更改为 catch(Exception e)。在代码代码中,您可以看到我对按钮 next 和 previous 的增量和减量。
猜你喜欢
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多