gpt4 book ai didi

java - 如何将列表或数组列表从 AsyncTask 类传递到另一个类

转载 作者:行者123 更新时间:2023-12-01 22:24:52 25 4
gpt4 key购买 nike

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

ReadChequeDetails.java 中,我正在检索 jason 数据并将它们显示在 ListView 中。

我需要将检索到的数据(例如: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();
}
}
}

最佳答案

看来您应该只在 Intent 中发送它。

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)

关于java - 如何将列表或数组列表从 AsyncTask 类传递到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28996083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com