Show data in listview with Asynctask

后端 未结 2 1250
滥情空心
滥情空心 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:24

    Working ASyncTask tutorial,

    Full ASyncTask Eclipse Project,

    and here's some code that I think, when mixed with the above sample, will get you the result with the list that you desire (you'll have to adapt it to your needs a bit, though (pay attention to the list stuff, even though this is from a custom Dialog:

        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Kies Facebook-account");
            builder.setNegativeButton("Cancel", this);
            LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View dialogLayout = inflater.inflate(R.layout.dialog, null);
            builder.setView(dialogLayout);
    
            final String[] items = {"Red", "Green", "Blue" };
    
            builder.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items), 
                    new DialogInterface.OnClickListener() {
    
    
                public void onClick(DialogInterface dialog, int which) {
                    Log.v("touched: ", items[which].toString());
    
                }} 
                );
    
    
            return builder.create();
    
        }
    
    0 讨论(0)
  • 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<Object> 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<Object>();
    
        new FetchActivitesTask().execute();
    
    
    }
    
    public class FetchActivitesTask extends AsyncTask<Void, Void, Void> {
    
        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;
    }
    
    0 讨论(0)
提交回复
热议问题