简体   繁体   中英

How do pass multiple data types to HashMap

I am trying to pass string and int data (possibly other data types like time) to a HashMap to use in a doinbackground task in Android to amend a URL. The URL uses key value pairs to update a mysql database.

I've read about using an object to pass multiple variable types, but can't get it to work.

private void addChore(){

    final String title2 = editTextTaskTitle.getText().toString().trim();
    final String description2 = editTextDescription.getText().toString().trim();
    final String person2 = itemPerson.toString().trim();
    final int monday2 = cbMon;

    class NewChore1 {

        String title1;
        String description1;
        String person1;
        int monday1;

        NewChore1(String title1, String description1, String person1, int monday1){

            this.title1 = title1;
            this.description1 = description1;
            this.person1 = person1;
            this.monday1 = monday1;
        }
    }

    class AddChoreM extends AsyncTask<Void,Void,String> {

        ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(AddChore.this,"Adding...","Wait...",false,false);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(AddChore.this,s,Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(Void... v) {
            HashMap<String, NewChore1> params1 = new HashMap<>();

            params1.put(Config.KEY_CHORE_TASK_TITLE,?);
            params1.put(Config.KEY_CHORE_DESCRIPTION,?);
            params1.put(Config.KEY_CHORE_PERSON,?);
            params1.put(Config.KEY_CHORE_MONDAY,?);

            RequestHandler rh = new RequestHandler();
            String res = rh.sendPostRequest(Config.URL_ADD, params1);
            return res;
        }
    }

    NewChore1 params = new NewChore1(title2, description2, person2, monday2);
    AddChoreM addChoreM = new AddChoreM();
    addChoreM.execute(params);
}

In RequestHandler, I have used the following.

private String getPostDataString(HashMap<String, Object> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for (Map.Entry<String, Object> entry : params.entrySet()) {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}

Create a constructor for AddChoreM class and set your NewChore1 object through it. You can now easily extract the properties of NewChore1 in doInBackground .

class AddChoreM extends AsyncTask<Void,Void,String> {

  ProgressDialog loading;
  NewChore1 newChore1Obj;

  public AddChoreM(NewChore1 newChore1Obj){
          this.newChore1Obj = newChore1Obj;
  }

  @Override
  protected String doInBackground(Void...v) {
          HashMap<String, NewChore1> params1 = new HashMap<>();
          String res = "";
          if(newChore1Obj != null) {
                  params1.put(Config.KEY_CHORE_TASK_TITLE, newChore1Obj.title1);
                  params1.put(Config.KEY_CHORE_DESCRIPTION, newChore1Obj.description1);
                  params1.put(Config.KEY_CHORE_PERSON,newChore1Obj.person1);
                  params1.put(Config.KEY_CHORE_MONDAY,newChore1Obj.monday1);

                  RequestHandler rh = new RequestHandler();
                  res = rh.sendPostRequest(Config.URL_ADD, params1);
          }
          return res;
  }

  // Other methods of AsyncTask
  //
}

Finally, create and execute AddChoreM like this.

NewChore1 params = new NewChore1(title2, description2, person2, monday2);
AddChoreM addChoreM = new AddChoreM(params);
addChoreM.execute();

If you use

Map<String, Object> params1 = new HashMap<>();

then you can store any type as value within the map.

Edit: I was not quick enough so there are other answers already but the changes below should work. You can pass your NewChore1 object to your task and extract the parameters in doInBackground:

class AddChoreM extends AsyncTask<NewChore1,Void,String> {

And:

  @Override
      protected String doInBackground(NewChore1...chore) {
              HashMap<String, String> params1 = new HashMap<>();

              params1.put(Config.KEY_CHORE_TASK_TITLE, chore[0].title1);
              params1.put(Config.KEY_CHORE_DESCRIPTION, chore[0].description1);
              params1.put(Config.KEY_CHORE_PERSON,chore[0].person1);
              params1.put(Config.KEY_CHORE_MONDAY,chore[0].monday1);

              RequestHandler rh = new RequestHandler();
              String res = rh.sendPostRequest(Config.URL_ADD, params1);

              return res;
  }

Finally:

NewChore1 params = new NewChore1(title2, description2, person2, monday2);
new addChoreM.execute(params);

Update: Since sendPostRequest only accepts HashMap<String, String> you need to change to: HashMap<String, String> params1 = new HashMap<>(); And change your NewChore1 class to take only Strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM