如何使用Fragment中的Simpleadapter在第二个活动中实现prev和next按钮?

时间:2016-02-11 07:20:10

标签: android listview button android-activity simpleadapter

我使用Simpleadapter在片段中集成了 Radiochannel列表。现在我希望在第二个活动中实现prev和next按钮以在List之间切换。

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

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传递数据,但是通过单击nextButton我可以只增加位置一次,在第二次单击时应用停止。 我的错误是什么?

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();
    }
}

}

1 个答案:

答案 0 :(得分:0)

我想通过单击下一个或上一个按钮来切换我的电台列表,例如从列表位置1切换到2.活动中的列表位置。我想在一个活动中播放所有无线电频道。