Convert String with multiple values to arraylist and add programmatically into a spinner?

Asked

Viewed 71 times

0

((I am using the browser to enter the site, because app here is problem)

I’m pulling several results from formats of a video on Youtube. However, when I step into the spinner only one result passes, which is usually the first, which is usually audio. Then there are no (remaining) video formats for any result in the spinner.

Edited: First I see if the app has received any valid Youtube link:

    // Check how it was started and if we can get the youtube link
    if (savedInstanceState == null && Intent.ACTION_SEND.equals(getIntent().getAction())
        && getIntent().getType() != null && "text/plain".equals(getIntent().getType())) {

        String ytLink = getIntent().getStringExtra(Intent.EXTRA_TEXT);

        if (ytLink != null
            && (ytLink.contains("://youtu.be/") || ytLink.contains("youtube.com/watch?v="))) {
            youtubeLink = ytLink;
            // We have a valid link
            getUrl(youtubeLink);
        } else {
            Toast.makeText(this, R.string.app_name, Toast.LENGTH_LONG).show();
            finish();
        }
    } else if (savedInstanceState != null && youtubeLink != null) {
        getUrl(youtubeLink);
    } else {
        //finish();
    }
}

Then I take the link to convert and expand the action of the app(formats) and then I send the result to a new method to add the download options in the spinner:

private void getUrl(String youtubeLink) {
    new YouTubeExtractor(this) {
        @Override
        public void onExtractionComplete(SparseArray<YtFile> ytFiles, VideoMeta vMeta) {
            if (ytFiles == null) {
                // Something went wrong we got no urls. Always check this.
                finish();
                return;
            }
            // Iterate over itags
            for (int i = 0, itag; i < ytFiles.size(); i++) {
                itag = ytFiles.keyAt(i);
                // ytFile represents one file with its url and meta data
                YtFile ytFile = ytFiles.get(itag);

                // Just add videos in a decent format => height -1 = audio
                if (ytFile.getFormat().getHeight() == -1 || ytFile.getFormat().getHeight() >= 360) {
                }
                    initSpinner(vMeta.getTitle(), ytFile);
                }
           }
    }.extract(youtubeLink, true, false);
}

New method to add *format options to the spinner:

public void initSpinner(final String videoTitle, final YtFile ytfile){
    spinner = (Spinner) findViewById(R.id.spinner);

    // Display some buttons and let the user choose the format
    String opcFrmt = (ytfile.getFormat().getHeight() == -1) ? "Audio " +
        ytfile.getFormat().getAudioBitrate() + " kbit/s":
    ytfile.getFormat().getHeight() + "p";
    opcFrmt += (ytfile.getFormat().isDashContainer()) ? " dash" : "";

    // Spinner click listener
    spinner.setOnItemSelectedListener(this);

    // Spinner Drop down elements
    List<String> strList = new ArrayList<String>();
    //return the list representation of array

    strList.add(opcFrmt);
    strList.add(opcFrmt);

    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, strList);

    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinner.setAdapter(dataAdapter);
}

The problem is that all of this only appears one result in the spinner.

  • 1

    You just make one add of a String in strList.add(opcFrmt); logo always has only one element. Where are the various elements you want to add ?

  • You are adding in your strList only a string that is opcFrmt. Can you edit the question and send the output of the opcFrmt variable? You may need to work this variable so that it is an array and not a string. Thus, Voce can make a repeat loop and add all urls in the strList'

  • Edited! Help me please **

  • In fact this app worked very well when receiving the result in this String and add in buttons. Separately added each good format. Button btn = new Button(this); btn.setText(opcFrmt);mainLayout.addView(btn);

  • This second code block is part of the method initSpinner? If not, what method is that?

  • Exactly. The second block is the initSpinner.

  • Edited block of code**

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.