我如何在asynctask中得到这个

时间:2012-04-21 18:38:43

标签: android multithreading asynchronous background android-asynctask

我让我的应用程序工作,所以它从json api返回信息。 现在我意识到我必须把所有东西都放在一个异步任务中,所以它不会崩溃那么多 并且进度对话框更容易,只有我真的不知道如何做到这一点,所以我想知道是否有人知道一个非常好的教程或想要编辑我的代码有点让我开始

    package net.thinkbin;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class culture extends ListActivity {

private static final String TITLE = "Title";
private static final String AUTHOR = "Author";
private static final String VIEWS = "Views";
private static final String RATES = "Rates";
private static final String CONTENT = "Content";
final Context context = this;

JSONArray ideas = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listplaceholder2);

    Button view = (Button) findViewById(R.id.button1);
    view.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent("net.thinkbin.TUTORIAL1"));
            overridePendingTransition(0, 0);
            finish();
        }
    });

    Button share = (Button) findViewById(R.id.button2);
    share.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent("net.thinkbin.SHARE"));
            overridePendingTransition(0, 0);
            finish();
        }
    });

    Button menu = (Button) findViewById(R.id.buttonhome);
    menu.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);

            TextView text = (TextView) dialog.findViewById(R.id.text);
            text.setText("Loading...");
            ImageView image = (ImageView)  dialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.hourglass);

            dialog.show();
            Thread th = new Thread(new Runnable() {
                public void run() {

                    startActivity(new  Intent("net.thinkbin.MENU"));
                    overridePendingTransition(0, 0);
                    dialog.dismiss();
                    finish();

                }
            });
            th.start();
        }
    });

    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
            this, R.array.spinnerorder,
            android.R.layout.simple_spinner_item);
     adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    Spinner s = (Spinner) findViewById(R.id.cultureorder);
    s.setAdapter(adapter2);

    s.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> adapter2, View view,
                int position, long id) {
            if (position == 1) {
                startActivity(new Intent("net.thinkbin.CULTURE2"));
                overridePendingTransition(0, 0);
                finish();
            }
            if (position == 2) {
                startActivity(new Intent("net.thinkbin.CULTURE3"));
                overridePendingTransition(0, 0);
                finish();
            }

        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String,  String>>();

    JSONObject json = JSONfunctions
             .getJSONfromURL("http://www.thinkbin.net/include/api/index.php? cat=Culture&type=Newest&i=10");

    try {

        ideas = json.getJSONArray("Ideas");

        for (int i = 0; i < ideas.length(); i++) {
            JSONObject c = ideas.getJSONObject(i);

            String title = c.getString(TITLE);
            String author = c.getString(AUTHOR);
            String views = c.getString(VIEWS);
            String rates = c.getString(RATES);
            String content = c.getString(CONTENT);

            HashMap<String, String> map = new HashMap<String, String> ();

            map.put(TITLE, "Title: " + title);
            map.put(AUTHOR, "Author: " + author);
            map.put(VIEWS, "Views: " + views);
            map.put(RATES, "Rates: " + rates);
            map.put(CONTENT, content);

            mylist.add(map);

        }

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

    ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.main2,
            new String[] { TITLE, AUTHOR, VIEWS, RATES, CONTENT },
            new int[] { R.id.item_title, R.id.item_subtitle,  R.id.item3,
                    R.id.item4, R.id.item5 });

    setListAdapter(adapter);

    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            String Title2 = ((TextView)  view.findViewById(R.id.item_title))
                    .getText().toString();
            String Author2 = ((TextView) view
                     .findViewById(R.id.item_subtitle)).getText().toString();
            String Content2 = ((TextView)  view.findViewById(R.id.item5))
                    .getText().toString();

            Intent in = new Intent(getApplicationContext(),  idea.class);
            overridePendingTransition(0, 0);
            in.putExtra(TITLE, Title2);
            in.putExtra(AUTHOR, Author2);
            in.putExtra(CONTENT, Content2);

            startActivity(in);

        }
    });

}
}

1 个答案:

答案 0 :(得分:0)

有很好的类 AsyncTask 可以在Android中执行Asyncronius。

示例:

  私有类DownloadFilesTask扩展了AsyncTask   {

 protected Long doInBackground(URL... urls) {
     int count = urls.length;
     long totalSize = 0;
     for (int i = 0; i < count; i++) {
         totalSize += Downloader.downloadFile(urls[i]);
         publishProgress((int) ((i / (float) count) * 100));
     }
     return totalSize;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }  }

http://developer.android.com/reference/android/os/AsyncTask.html