【问题标题】:Interface is not being called android接口没有被称为android
【发布时间】:2015-02-18 10:21:57
【问题描述】:

我已经使用了一个接口来确定异步任务是否完成的天气。但是当我使用该接口时,该接口的 2 个方法没有被调用。

自定义适配器类

public class MyResourceCustomAdaper extends BaseAdapter {


    static GetMatchingJobsArray getMatchingJobsArray;


    public MyResourceCustomAdaper(Context context, ArrayList<HashMap<String, String>> data) {
        this.context = context;
        this.data = data;


    }

    public static void setInterface(GetMatchingJobsArray getMatchingJobsArray) {
        MyResourceCustomAdaper.getMatchingJobsArray = getMatchingJobsArray;
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        doing stuffs here


        return view;
    }
}



public static class GetMatchingJobsAsync extends AsyncTask<String, String, String> {
    String response;
    Context c;


    public GetMatchingJobsAsync(Context c) {

        this.c = c;

    }

    @Override
    protected String doInBackground(String... strings) {


        calling web method here

    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        arrayMatchingJobs = new ArrayList<HashMap<String, String>>();



                getMatchingJobsArray.getMatchingJobs(true, arrayMatchingJobs);// here i am passing my array to the interface


                getMatchingJobsArray.hasCompleted(true);

                Intent i = new Intent(c, MyResourceMatchingJobs.class);
                c.startActivity(i);




        }
    }


}

public interface GetMatchingJobsArray {

    public void getMatchingJobs(boolean value, ArrayList<HashMap<String, String>> arrayMatchingJobs);

    public void hasCompleted(boolean value);

}

}

实现接口的类

public class MyResourceMatchingJobs extends Activity implements MyResourceCustomAdaper.GetMatchingJobsArray {
    private ListView listView;
    private MyResourceMatchingJobsCustomList adapter;
    private static ProgressDialog pDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_resource_matching_jobs_list);
        listView = (ListView) findViewById(R.id.lv_my_resource_matching_jobs);
        MyResourceCustomAdaper.setInterface(this);//use for initailizing the interface



    }


    @Override
    public void getMatchingJobs(boolean value, ArrayList<HashMap<String, String>> arrayMatchingJobs) {

adapter = new MyResourceMatchingJobsCustomList(MyResourceMatchingJobs.this, arrayMatchingJobs);
            listView.setAdapter(adapter);

    }

    @Override
    public void hasCompleted(boolean value) {
        Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();

    }

在这个活动中,为什么我的界面没有被调用???

【问题讨论】:

    标签: java android interface


    【解决方案1】:

    在这个活动中,为什么我的界面没有被调用???

    因为在MyResourceMatchingJobs Activity 未运行时调用getMatchingJobs

    所以在从onPostExecute 启动MyResourceMatchingJobs Activity 之后尝试调用这两个方法。

    建议:因为发送arrayMatchingJobs 和一个布尔值,所以使用i.putExtra(KEY,VALUE)MyResourceMatchingJobs Activity 中获取这两个值

    【讨论】:

    • @Anuj:为什么要从onPostExecute 开始MyResourceMatchingJobs 活动?
    • 那我怎么称呼新的activity呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2021-07-20
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    相关资源
    最近更新 更多