如何从listview中的textview获取字符串值并将其传递给数据库?

时间:2013-03-25 14:12:00

标签: java android listview textview

很抱歉,如果我在这里开始讨论傻编码问题,我就开始开发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();


        }





    }

}

0 个答案:

没有答案