如何将列表或arraylist从AsyncTask类传递到另一个类

时间:2015-03-11 19:52:47

标签: java android android-asynctask

我有两个课程,一个是ReadChequeDetails.java,另一个是SearchMain.java

in ReadChequeDetails.java我正在检索jason数据并在列表视图中显示它们。

我需要将检索到的数据(例如:chequeNo)传递给 在SearchMain.java

上引用的autocompletetextview

我将ReadChequeDetails.java代码放在

下面
package com.example.chequeinfo;

public class ReadChequeDetails extends ListActivity implements OnClickListener{


    private ProgressDialog pDialog;
    private static final String READ_COMMENTS_URL ="http://www.xxxx.com/abc.php";
    private Button add,searchoption;

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_POSTID = "post_id";
    private static final String TAG_POSTS = "details";
    private static final String TAG_CHEQUENO = "cheque_no";
    private static final String TAG_BANKNAME = "bank_name";
    private static final String TAG_AMOUNT = "amount";
    private static final String TAG_CHEQUEDATE = "cheque_date";
    private static final String TAG_CHEQUEOWNER = "cheque_owner";
    private static final String TAG_SAVEDDATE = "saved_date";
    private static final String TAG_SAVEDBY = "saved_by";


    private JSONArray mComments = null;
    private ArrayList<HashMap<String, String>> mCommentList;
    ArrayList<String> newlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.read_cheque_details);
        add = (Button)findViewById(R.id.post_details);

        //search option button which take to the SearchMain.java class
        searchoption=(Button)findViewById(R.id.mainsearch);
        add.setOnClickListener(this);
        searchoption.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        new LoadComments().execute();
        // loading the comments via AsyncTask
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
            case R.id.post_details:
                Intent i = new Intent("com.example.chequeinfo.ADDCHEQUEDETAILS");
                startActivity(i);
                break;
            case R.id.mainsearch:
                Intent i1 = new Intent("com.example.chequeinfo.SEARCHMAIN");
                startActivity(i1);
                break;
        }
    }

    public void updateJSONdata() {
        mCommentList = new ArrayList<HashMap<String, String>>();
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);

        try {
            mComments = json.getJSONArray(TAG_POSTS);
            for (int i = 0; i < mComments.length(); i++) {
                JSONObject c = mComments.getJSONObject(i);

                String postId = c.getString(TAG_POSTID);
                String chequeNo = c.getString(TAG_CHEQUENO);
                String bankName = c.getString(TAG_BANKNAME);
                String amount = c.getString(TAG_AMOUNT);
                String chequeDate = c.getString(TAG_CHEQUEDATE);
                String chequeOwner = c.getString(TAG_CHEQUEOWNER);
                String savedDate = c.getString(TAG_SAVEDDATE);
                String savedBy = c.getString(TAG_SAVEDBY);

                //newlist is a String type array list which i need to pass into the AutocompletetextView 
                //which is referenced in SearchMain.java 

                newlist.add(chequeNo);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public class LoadComments extends AsyncTask<Void, Void, Boolean> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ReadChequeDetails.this);
            pDialog.setMessage("Loading Details...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(Void... arg0) {
            updateJSONdata();
            return null;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            pDialog.dismiss();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

看起来你应该只是意图发送它。

case R.id.mainsearch:
 Intent i1 = new Intent("com.example.chequeinfo.SEARCHMAIN");
 i1.putStringArrayListExtra("cheque_list", newlist);

 startActivity(i1);
 break;
}

然后在SearchMain.java中:

//Make this a member variable:
ArrayList<String> chequeList;

//in onCreate:
Intent i = getIntent();      
chequeList = i.getStringArrayListExtra("cheque_list");

参考文献: Passing ArrayList through Intent

http://developer.android.com/reference/android/content/Intent.html#getStringArrayListExtra(java.lang.String)