[英]how do i pass user-defined array parameter to asynctask in android
但是,当我只检查用户定义的数组参数,外部类中的customerList时,内部就有值,但是uploadAsyncTask内部类在模拟器上不断失败(不幸的是,显示应用失败)。
private ArrayList<CustomerData> customerList = new ArrayList<CustomerData>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DBAdapter(this);
presidents = getResources().getStringArray(R.array.presidents_array);
client = new AsyncHttpClient();
Button btn_Save, btn_Backup;
btn_Save = (Button) findViewById(R.id.btnSave);
btn_Backup = (Button) findViewById(R.id.btnBackup);
txt_AcctNum = (TextView) findViewById(R.id.txtAcctNum);
txt_AcctName = (TextView) findViewById(R.id.txtAcctName);
s1 = (Spinner) findViewById(R.id.spinner);
txt_Amt = (TextView) findViewById(R.id.txtAmt);
/*
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, presidents);
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, presidents);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
//Toast.makeText(getBaseContext(),
// "You have selected item : " + presidents[index],
// Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
-------other codes here----
btn_Backup.setOnClickListener(new Button.OnClickListener() {
@SuppressWarnings(value = "unchecked")
public void onClick(View view) {
ArrayList<CustomerData> custList = new ArrayList<CustomerData>();
// db = new DBAdapter(context);
db.open();
//db.backupToSD();
//---get all contacts---
Cursor c = db.getAllCustomers();
if (c.moveToFirst()) {
do {
//DisplayContact(c);
//sendData();
custList = addAndDisplayCustomer(c);
} while (c.moveToNext());
}
/* for (int i = 0; i < custList.size(); i++) {
Toast.makeText(getApplicationContext(), "---Customer Data--- " + "id: " +
custList.get(i).getId() + " Acct Name: " +
custList.get(i).getAcctName() + " Acct Num: " +
custList.get(i).getAcctNum() + " Tnx type: " +
custList.get(i).getTxnType() + " Amt: " +
custList.get(i).getAmt(), Toast.LENGTH_LONG).show(); }*/
Toast.makeText(getApplicationContext(), " Uploading data ... " + custList.get(0).getId(), Toast.LENGTH_LONG).show();
UploadASyncTask upload = new UploadASyncTask();
upload.execute(custList);
}
public ArrayList<CustomerData> addAndDisplayCustomer(Cursor c)
{
CustomerData customer = new CustomerData(c.getString(0), c.getString(1),
c.getString(2), c.getString(3), c.getString(4));
customerList.add(customer);
return customerList;
}
//int delRows = db.deleteAll();
//db.backupToSD();
//db.dropTable();
//Toast.makeText(getApplicationContext(), " Table successfully dropped ! ", Toast.LENGTH_LONG).show();
//db.close();
});
}
private class UploadASyncTask extends AsyncTask<ArrayList<CustomerData>, Void, Void> {
private Cursor c;
private String id;
private String acct_Name ;
private String acct_Num;
private String txnType;
private String amt;
//private ArrayList<CustomerData> custList;
private Context mContext1;
private ProgressDialog dialog = null;
private Context mContext = null;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle(" Sending to the server... ");
dialog.setMessage("Please wait...");
dialog.setProgressDrawable(mContext.getWallpaper());
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setCancelable(false);
dialog.show();
}
@Override
@SafeVarargs
final protected Void doInBackground(ArrayList<CustomerData>... custList) {
try {
ArrayList<CustomerData> custom = custList[0];
for (int i = 0; i<custom.size(); i++) {
String id = custom.get(i).getId();
String acct_Name = custom.get(i).getAcctName();
String acct_Num = custom.get(i).getAcctNum();
String txnType = custom.get(i).getTxnType();
String amt = custom.get(i).getAmt();
/*runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Welcome guy: " + id,
Toast.LENGTH_LONG).show();
}
});*/
HttpParams params = new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient(params);
HttpPost httpPost = new HttpPost
("http://10.0.2.2:8080/RestWebService/rest/customer");
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("id", id));
postParams.add(new BasicNameValuePair("acct_name", acct_Name));
postParams.add(new BasicNameValuePair("acct_num", acct_Num));
postParams.add(new BasicNameValuePair("txn_type", txnType));
postParams.add(new BasicNameValuePair("amt", amt));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
entity.setContentEncoding(HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
String result = "";
id = "";
acct_Name = "";
acct_Num = "";
txnType = "";
amt = "";
}
}
catch (Exception e)
{
Log.e("Server Error: ",e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
//custList.clear();
dialog.dismiss();
}
}
}
当我看到您的代码时:有一个小问题。 我不知道这是个好答案,但是:
private Context mContext = null;
和
dialog.setProgressDrawable(mContext.getWallpaper());
这行是nullPointerException
对我来说,您可以在AsyncTask上删除上下文和光标。 在AsyncTask上创建一个构造函数,并将上下文放在参数中,将其用于progressDialog
希望对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.