没有调用接口回调方法?

时间:2016-12-15 02:38:13

标签: android

我正在使用Android项目。以下是您需要了解的问题。 (如果还不够,请告诉我):

我有一个片段,一个异步任务和一个名为async response的接口。 Async Task位于自己的类中,称为DownLoadRawData。这是片段的代码:

   public class RedditListFragment extends Fragment implements AsyncResponse{


   private RecyclerView mRecyclerView;
   private RedditAdapter mAdapter;
   private Parser parse;





@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_reddit_list, container, false);
          mRecyclerView = (RecyclerView) view.findViewById(R.id.reddit_list_view);
          mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));





        executeAsyncTask();





    updateUI();
    // Inflate the layout for this fragment
    return view;
}


**@Override
public void processFinish(String output) {
    parse = new Parser(output);
    Log.d("TAG", "The List to STring is " + parse.getRedditObjectList().toString());
    Log.d("TAG", "process finished reached");
}**

public void executeAsyncTask(){
    DownLoadRawData downLoadRawData = new DownLoadRawData();
    downLoadRawData.delegate = this;
    downLoadRawData.execute("https://www.reddit.com/r/Showerthoughts/.json");


    // Log.d("ERR", " TEXT WAS "  + jsonTEXT);
}






public void updateUI(){
        List<RedditObject> listOfRedditObject = parse.getRedditObjectList();

        mAdapter = new RedditAdapter(listOfRedditObject);
        mRecyclerView.setAdapter(mAdapter);

}




private class RedditHolder extends RecyclerView.ViewHolder{


    private TextView mTextView;


    public RedditHolder(View itemView) {
        super(itemView);

        mTextView = (TextView) itemView;  //by casting itemView to a TextView, this forces client to pass text view
    }

}


private class RedditAdapter extends RecyclerView.Adapter<RedditHolder>{


    private List<RedditObject> mListOfRedditObjects;



    public RedditAdapter(List<RedditObject> listOfRedditObject){
       mListOfRedditObjects = listOfRedditObject;
    }

    @Override
    public RedditHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View view = layoutInflater.inflate(R.layout.single_reddit_listing, parent, false);
        return new RedditHolder(view);
    }

    @Override
    public void onBindViewHolder(RedditHolder holder, int position) {
            RedditObject redditObject = mListOfRedditObjects.get(position);
            holder.mTextView.setText(redditObject.getmTitle());
    }

    @Override
    public int getItemCount() {
       return mListOfRedditObjects.size();
    }
}

}

DownloadRawData异步任务类位于:

package com.example.ridhwaan.redditforfaez;

  import android.os.AsyncTask;
  import android.util.Log;
   import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;


public class DownLoadRawData extends AsyncTask<String, Void, String> {
    private String mFileContents;
    **public  AsyncResponse delegate = null;**




public String getmFileContents() {
    return mFileContents;
}

@Override
protected String doInBackground(String... strings) {
    mFileContents = downloadRawJSON(strings[0]);

    return mFileContents;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    **delegate.processFinish(result);**
    Log.d("TAG", "RESULT" + result);


}

和Aync响应:

public interface AsyncResponse {

void processFinish(String output);

}

出于某种原因,在我的fragment类中,没有调用process finish方法。根据我对接口的理解,Async Response在提供参数的DownLoadRaw数据类中实现。然后我们在片段类中设置一个委托给它,以便它可以准备好返回方法结果,或者回调。如果它存在缺陷,请帮助我理解。

问题是根本没有调用process finish方法。我放的日志没有被打印出来,但是它们正在其他地方打印出来。

这个问题可能是什么原因?

由于

2 个答案:

答案 0 :(得分:0)

首先,您应该让您的活动成为被调用的活动,然后让活动通知片段任何更改。让活动通过该接口存储对片段的引用。

在处理片段和并发时,

Model View Presenter pattern有很大帮助。 (只是一个有用的提示)

但要回答你的问题:你不需要在onPostExecute()中调用Super()。很可能在此时停止执行。所以,如果你删除那行,你可能会没事。

(假设您的**伪代码**行实际上有效,因为您没有显示完整代码)

答案 1 :(得分:0)

是的,如果要调用onPostExecuted,则应打印日志。如果已调用,请检查onPostExecuted中的日志。