Android 如何在AYSNTASK中从后台线程到UI线程检索数据?

Android 如何在AYSNTASK中从后台线程到UI线程检索数据?,android,arrays,android-asynctask,android-adapter,Android,Arrays,Android Asynctask,Android Adapter,我已经从api中提取了数据,但我不知道如何将这些提取的数据分配给我的适配器 问题: 如何将提取的数据设置为上面定义的数组 如何设置适配器 公共类FetchList扩展了异步任务{ public List listTitles=new ArrayList(); @凌驾 受保护列表doInBackground(整数…参数){ int count=参数[0]; int offset=参数[1]; 字符串URL字符串=”https://us14.api.mailchimp.com/lists?apike

我已经从api中提取了数据,但我不知道如何将这些提取的数据分配给我的适配器

问题:

  • 如何将提取的数据设置为上面定义的数组

  • 如何设置适配器

    公共类FetchList扩展了异步任务{

    public List listTitles=new ArrayList();
    @凌驾
    受保护列表doInBackground(整数…参数){
    int count=参数[0];
    int offset=参数[1];
    字符串URL字符串=”https://us14.api.mailchimp.com/lists?apikey=efb918ee8791215bac4c8a3a8a77-us14“
    urlString=urlString+“&count=“+count+”&offset=“+offset;
    试一试{
    URL=新URL(URL字符串);
    HttpURLConnection connection=(HttpURLConnection)url.openConnection();
    connection.setRequestMethod(“GET”);
    InputStream=connection.getInputStream();
    BufferedReader reader=新的BufferedReader(新的InputStreamReader(流));
    字符串行=reader.readLine();
    字符串响应=”;
    while(行!=null){
    响应+=行;
    line=reader.readLine();
    }
    JSONObject对象=新的JSONObject(响应);
    JSONArray lists=object.getJSONArray(“列表”);
    对于(int i=0;i
    }

  • @覆盖
    受保护的void onPostExecute(列表){
    MyCustomAdapter=新的MyCustomAdapter(上下文、邮件列表);
    myListView.setAdapter(适配器);
    }
    
    由于您的列表是自定义列表,所以您需要编写一个客户适配器


    您可以将ListView声明为全局变量,并且可以在执行后访问它。

    您可以在线程内部使用runnable,因为您使用的是异步,所以您可以在onPostExecute方法中设置适配器检查下面的答案
        public List<MailChimpList> listTitles = new ArrayList<>();
    
        @Override
        protected List<MailChimpList> doInBackground(Integer... params) {
            int count = params[0];
            int offset = params[1];
    
            String urlString = "https://us14.api.mailchimp.com/lists?apikey=efb918ee8791215bac4c8a3a8a77-us14"
    
            urlString = urlString + "&count=" + count + "&offset=" + offset;
    
            try {
                URL url = new URL(urlString);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                InputStream stream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
                String line = reader.readLine();
                String response = "";
                while (line != null) {
                    response += line;
                    line = reader.readLine();
                }
    
                JSONObject object = new JSONObject(response);
    
                JSONArray lists = object.getJSONArray("lists");
    
                for (int i = 0; i < lists.length(); i++) {
                    JSONObject listData = (JSONObject) lists.get(i);
                    MailChimpList mailChimpList = new MailChimpList();
                    mailChimpList.id = listData.getString("id");
                    mailChimpList.title = listData.getString("name");
    
                    String id = listData.getString("id");
                    String title = listData.getString("name");
    
                    Log.d("ashutosh","id are: "+id);
                    Log.d("ashutosh","list name are: "+title);
    
                    listTitles.add(mailChimpList);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return listTitles;
        }
    
    @Override
    protected void onPostExecute(List<MailChimpList> mailChimpLists) {
    
    }
    
    @Override
    protected void onPostExecute(List<MailChimpList> mailChimpLists) {
         MyCustomAdapter adapter = new MyCustomAdapter(context, mailChimpLists);
         myListView.setAdapter(adapter);
    }