0

I am having trouble with assigning the adapter to titlesList now... I get the error "The method setAdapter(ArrayAdapter) is undefined for the type ArrayList" What am I doing wrong?

The problem is in the class ShowTitlesTask

package com.aer.illbehonest;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.commons.io.IOUtils;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class VideoPlay extends Activity {   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);

        new ShowTitlesTask().execute("");
    }

    class ShowTitlesTask extends AsyncTask<String, Void, ArrayList<String>> {

    ArrayList<String> titlesList;

    @Override
    protected ArrayList<String> doInBackground(String... args) {

    URL jsonURL = null;
    try {
           jsonURL = new URL("http://gdata.youtube.com/feeds/api/users/illbehonest/uploads?v=2&alt=jsonc");
        } catch (MalformedURLException e3) {
            e3.printStackTrace();
        }
            URLConnection jc = null;
            try {
                jc = jsonURL.openConnection();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            InputStream is = null;
            try {
                is = jc.getInputStream();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            String jsonTxt = null;
            try {
                jsonTxt = IOUtils.toString(is);
            } catch (IOException e) {
                e.printStackTrace();
            }

            JSONObject json = (JSONObject) JSONSerializer.toJSON(jsonTxt);

            JSONObject jdata = json.getJSONObject("data");

            JSONArray jentry = jdata.getJSONArray("items");
            for (int entryNumber = 0; entryNumber<=25; entryNumber++){
                    JSONObject entry = jentry.getJSONObject(entryNumber);
                    titlesList.add(entry.getString("title"));
            }

            return titlesList;
    }
    protected void onPostExecute(ArrayList<String> result) {
        ListView listView = (ListView)VideoPlay.this.findViewById(R.id.videolist);

        ArrayAdapter<String> adapter = new ArrayAdapter<String> (VideoPlay.this,
                android.R.layout.simple_list_item_1, android.R.id.text1, titlesList);
        titlesList.setAdapter(arrayAdapter);
        }
    }
}
1
  • can you pelase paste the logcat? Commented Jan 14, 2013 at 21:28

1 Answer 1

1

this is pointing to your AsyncTask instead of the Activity (the context). Try VideoPlay.this

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you but now the titlesList.setAdapter(adapter) gives me this: "The method setAdapter(ArrayAdapter<String>) is undefined for the type ArrayList<String>"
Errr... that's because you meant listView instead of titlesList. Sometimes the error messages are pretty clear in what's wrong.
I'm so sorry, it's been a long day. Thank you so much for helping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.