几天前我开始研究Android开发。我正在尝试从REST API获取数据,而我正在使用AsyncTask类来执行HTTP事务。问题是,我无法获取主活动的上下文,以便找到我的ListView并在onPostExecute()中为其分配内容。
这是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。我试过实例化Context,但我也不能这样做。
答案 0 :(得分:1)
Context
没有观看次数,也没有findViewById
方法 - 请尝试改为mainContext
类型Activity
。
答案 1 :(得分:1)
替换
listView = (ListView) mainContext.this.findViewById(R.id.media_list);
与
listView = (ListView) mainContext.findViewById(R.id.media_list);
并且错误应该消失。
答案 2 :(得分:1)
您可以将AsyncTask添加为活动中的内部类。
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
}
}}
答案 3 :(得分: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中定义接口,并让您的活动类实现接口。