0

几天前我开始研究 Android 开发。我正在尝试从 REST API 获取数据,并且正在使用 AsyncTask 类来执行 HTTP 事务。问题是,我无法获取主要活动的上下文,以便在 onPostExecute() 中找到我的 ListView 并将内容分配给它。

这是 MainActivity.java:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new DownloadMediaList(this).execute();
    }
}

这里是 DownloadMediaList.java:

public class DownloadMediaList extends AsyncTask<Void, Void, ArrayList<Media>> {

    ListView listView = null;
    Context mainContext = null;

    public DownloadMediaList(Context main){
        this.mainContext = main;
    }

    @Override
    protected void onPreExecute(){
        listView = (ListView) mainContext.this.findViewById(R.id.media_list);
    }

    // Operations that we do on a different thread.
    @Override
    protected ArrayList<Media> doInBackground(Void... params){
        // Set an ArrayList to store the medias.
        ArrayList<Media> mediaList = new ArrayList<Media>();

        // Call the REST API and get the request info and media list in a JSONObject.
        RESTFunctions restRequest = new RESTFunctions();
        JSONObject jsonMedia = restRequest.getMediaList();

        // Try catch to catch JSON exceptions.
        try {
            // Store the media list into a JSONArray.
            JSONArray mediaArray = jsonMedia.getJSONArray("media");

            // Create an instance of media to store every single media later.
            Media media = new Media();

            // Loop through the JSONArray and add each media to the ArrayList.
            for (int i=0; i<mediaArray.length();i++){
                media = new Media();
                JSONObject singleMedia = mediaArray.getJSONObject(i);
                media.setTitle(singleMedia.getString("titre"));
                media.setYear(singleMedia.getString("annee"));
                media.setLength(singleMedia.getString("duree"));
                mediaList.add(media);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Return the ArrayList.
        return mediaList;
    }

    // Operations we do on the User Interface. Synced with the User Interface.
    @Override
    protected void onPostExecute(ArrayList<Media> mediaList){
        // Set TextViews in ListViews here
    }
}

这些类位于两个单独的文件中。

这条线尤其给我带来了麻烦:

listView = (ListView) mainContext.this.findViewById(R.id.media_list);

我究竟做错了什么?它告诉我上下文无法解析为类型,即使我已经导入了 android.content.Context。我尝试实例化上下文,但我也不能这样做。

4

4 回答 4

1

Contexts 没有视图,也没有findViewById方法 - 尝试使用您mainContext的类型Activity

于 2013-07-04T03:22:08.683 回答
1

代替

listView = (ListView) mainContext.this.findViewById(R.id.media_list);

listView = (ListView) mainContext.findViewById(R.id.media_list);

并且错误应该消失。

于 2013-07-04T03:23:10.187 回答
1

在你的活动课上

    public interface TheInterface {
public void theMethod(ArrayList<Media> result);

 }
     }

然后

    DownloadMediaList task = new DownloadMediaList (MainAactivity.this,new TheInterface() {
             @Override
             public void theMethod(ArrayList<Media> result) {
                // result available here do whatever with list media
            }  
        }); 

然后在 AsyncTask

Context mainContext = null;
TheInterface mlistener;
public DownloadMediaList(Context main,TheInterface listener){
    this.mainContext = main;
    mlistener = listener; 
}

然后在 onPostExecute

 @Override
protected void onPostExecute(ArrayList<Media> mediaList){
   if (mlistener != null) 
    {
         mlistener.theMethod(mediaList);
    }
} 

编辑:

作为备选。

您可以在 asycntask 中定义接口并让您的活动类实现该接口。

于 2013-07-04T03:48:43.473 回答
1

您可以改为将 AsyncTask 添加为 Activity 中的内部类。

public class MainActivity extends Activity {
  private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=this;
    new DownloadMediaList(this).execute();
}

private class DownloadMediaList extends AsyncTask<Void, Void, ArrayList<Media>> {

ListView listView = null;




@Override
protected void onPreExecute(){
    listView = (ListView) context.findViewById(R.id.media_list);
}

// Operations that we do on a different thread.
@Override
protected ArrayList<Media> doInBackground(Void... params){
    // Set an ArrayList to store the medias.
    ArrayList<Media> mediaList = new ArrayList<Media>();

    // Call the REST API and get the request info and media list in a JSONObject.
    RESTFunctions restRequest = new RESTFunctions();
    JSONObject jsonMedia = restRequest.getMediaList();

    // Try catch to catch JSON exceptions.
    try {
        // Store the media list into a JSONArray.
        JSONArray mediaArray = jsonMedia.getJSONArray("media");

        // Create an instance of media to store every single media later.
        Media media = new Media();

        // Loop through the JSONArray and add each media to the ArrayList.
        for (int i=0; i<mediaArray.length();i++){
            media = new Media();
            JSONObject singleMedia = mediaArray.getJSONObject(i);
            media.setTitle(singleMedia.getString("titre"));
            media.setYear(singleMedia.getString("annee"));
            media.setLength(singleMedia.getString("duree"));
            mediaList.add(media);
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Return the ArrayList.
    return mediaList;
}

// Operations we do on the User Interface. Synced with the User Interface.
@Override
protected void onPostExecute(ArrayList<Media> mediaList){
    // Set TextViews in ListViews here
}
  }}
于 2013-07-04T03:28:55.317 回答