对不起,如果我刚开始开发 Android 应用程序时在这里问了愚蠢的编码问题。我想将 listview 中的 textview 中的字符串结果保存到我的数据库中作为路由历史记录,但是我怎样才能将字符串传递给我的数据库?saveHistory 类中的 post 参数不断给我错误。提前致谢。
public class RouteResult extends Activity {
private ListView lvRoute;
private Context context = this;
String source = null;
String destination = null;
String username = null;
ProgressDialog dialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.route_result);
lvRoute = (ListView) findViewById(R.id.listView1);
Bundle bundle = this.getIntent().getExtras();
source= bundle.getString("source");
destination=bundle.getString("destination");
new GetRoute().execute();
}
class GetRoute extends AsyncTask<String, String, String> {
ArrayList<NameValuePair> postParameters;
private List<HashMap<String, String>> fillMaps;
private ProgressDialog pDialog;
String response = null;
String[] from;
int[] to;
ListViewAdapter _adapter;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
postParameters = new ArrayList<NameValuePair>();
fillMaps = new ArrayList<HashMap<String, String>>();
// column name
from = new String[] { "source", "s_station","destination","d_station","t_price" };
to = new int[] { R.id.textView_sourceName, R.id.textView_sourceCode,
R.id.textView_destName, R.id.textView_destCode, R.id.textView_price };
pDialog = new ProgressDialog(context);
pDialog.setMessage("Calculating. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("source", source.toString()));
postParameters.add(new BasicNameValuePair("destination",destination.toString()));
try {
response = CustomHttpClient.executeHttpPost(
"url",
postParameters);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
if (response != null) {
try {
JSONArray jArray = new JSONArray(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("source",
json_data.getString("source"));
map.put("s_station",
json_data.getString("s_station"));
map.put("destination",
json_data.getString("destination"));
map.put("d_station",
json_data.getString("d_station"));
map.put("t_price",
json_data.getString("t_price"));
fillMaps.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
_adapter = new ListViewAdapter(context, fillMaps,
R.layout.route_item, from, to);
lvRoute.setAdapter(_adapter);
} else {
// setDialog("Connection Error",
// "Fail to connect to server.");
}
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_save:
Toast.makeText(RouteResult.this, "Save is Selected", Toast.LENGTH_SHORT).show();
new saveHistory().execute() ;
return true;
case R.id.menu_search:
Toast.makeText(RouteResult.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
Intent intent = new Intent(RouteResult.this, MainMenu.class);
startActivity(intent);
finish();
}
class saveHistory extends AsyncTask<String,String,String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(RouteResult.this, "",
"Saving... Please wait", true);
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("s_station", s_station.getText().toString()));
postParameters.add(new BasicNameValuePair("d_station", d_station.getText().toString()));
postParameters.add(new BasicNameValuePair("t_price", t_price.getText().toString()));
String response2 = null;
String abc = null;
try {
response2 = CustomHttpClient.executeHttpPost(
"url",
postParameters);
String result = response2.toString();
}
}
}