Show data in listview with Asynctask

后端 未结 2 1251
滥情空心
滥情空心 2021-01-23 07:42

I success show my data from web service JSON in listview, but I want to add Asyntask. Where I can put code Asyntask in my code.

This my code to show data in list

2条回答
  •  走了就别回头了
    2021-01-23 08:37

    This is my code please try this one,

    MAinActivity.java

    public class MyActivity extends Activity {
    
    private ListView contests_listView;
    private ProgressBar pgb;
    ActivitiesBean bean;
    ArrayList listActivities;
    ActivityAdapter adapter;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_listview);
    
        contests_listView = (ListView) findViewById(R.id.activity_listView);
        pgb = (ProgressBar) findViewById(R.id.contests_progressBar);
        listActivities = new ArrayList();
    
        new FetchActivitesTask().execute();
    
    
    }
    
    public class FetchActivitesTask extends AsyncTask {
    
        int i =0;
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
             pgb.setVisibility(View.VISIBLE);
        }
    
        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
    
            String url = "Your URL Here";
            String strResponse = util.makeWebCall(url);
    
            try {
                JSONObject objResponse = new JSONObject(strResponse);
    
                JSONArray jsonnodes = objResponse.getJSONArray(nodes);
    
                for (i = 0; i < jsonnodes.length(); i++) 
                {
                    String str = Integer.toString(i); 
                    Log.i("Value of i",str);
    
                    JSONObject jsonnode = jsonnodes.getJSONObject(i); 
    
                    JSONObject jsonnodevalue = jsonnode.getJSONObject(node);
    
                    bean = new ActivitiesBean();
    
                    bean.title = jsonnodevalue.getString(title);
                    bean.image = jsonnodevalue.getString(field_activity_image_fid);
    
                    listActivities.add(bean);
    
                }               
            }
            catch (JSONException e) {
    
                e.printStackTrace();
          }
    
            return null;
        }
    
        @Override
        public void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
    
            pgb.setVisibility(View.GONE);
            displayAdapter();
        }
    }
    
    public void displayAdapter()
    {
        adapter = new ActivityAdapter(this, listActivities);
        contests_listView.setAdapter(adapter);
        contests_listView.setOnItemClickListener(new OnItemClickListener() {
    
            //@Override
            public void onItemClick(AdapterView arg0, View arg1,
                    int position, long id) {
                // your onclick Activity
            }
    
        });
    
    }
    }
    
    
    

    util.class

    public static String makeWebCall(String url) {
    
        DefaultHttpClient client = new DefaultHttpClient();
    
        HttpGet httpRequest = new HttpGet(url);
      //  HttpPost post = new HttpPost(url);
    
        try {
    
            HttpResponse httpResponse = client.execute(httpRequest);
    
             final int statusCode = httpResponse.getStatusLine().getStatusCode();
    
             if (statusCode != HttpStatus.SC_OK) {
    
                return null;
             }
    
             HttpEntity entity = httpResponse.getEntity();
    
             InputStream instream = null;
    
             if (entity != null) {
                  instream = entity.getContent();
             }
    
                return iStream_to_String(instream);
        }
        catch (IOException e) {
            httpRequest.abort();
          // Log.w(getClass().getSimpleName(), "Error for URL =>" + url, e);
        }
    
        return null;
    
     }
    
    public static String iStream_to_String(InputStream is1) {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is1), 4096);
        String line;
        StringBuilder sb = new StringBuilder();
        try {
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            }
            rd.close();
    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String contentOfMyInputStream = sb.toString();
        return contentOfMyInputStream;
    }
    
    }
    

    ActivityBean.java

    public class ActivitiesBean implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    public String title;
    public String image;
    
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }
    

    提交回复
    热议问题