Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store String Key-value pair

I am developing an android app, the main work of app is there is a scanner and i have to scan again & again and store result in key value pair.

  [
   {
    "0" : "816444014066",
    "1" : "747083010945",
    "2" : "816444010969"
  }
 ]

and by API i have to send all the scan result by array. I am getting scan result by another activity by startActivityForResult.User will scan again and again and by onActivityResult user will get result.i have to store all the result in key value pair and finally there is a button by tap on button by POST request i have to send all the scan result by array like above code.

Can i use here HashMap or i have to use Shared Preferences for storing result.

// Call Back method  to get the Message form other Activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // check if the request code is same as what is passed  here it is 2
        if (requestCode == 2 && data!=null) {
            String message = data.getStringExtra(SCAN_RESULT);
            // textView1.setText(message);
            if(message!= null){
                Log.e("SCAN_RESULT", "" + message);

                //Adding code will be here


              }
        }
    }
like image 921
Abhishek kumar Avatar asked Nov 28 '17 10:11

Abhishek kumar


3 Answers

here is the sample code for saving key-values with hashmap :

HashMap<String, String> data = new HashMap<String, String>();
data.put("key", "value");

or if the order matters to you use linkedHashMap :

HashMap<String, String> data = new LinkedHashMap<>();

the usage is the same;)

like image 191
SepJaPro2.4 Avatar answered Sep 21 '22 17:09

SepJaPro2.4


Data Structure Logic:

Hashmaps are used to access the content via its key.

In this sample, I guess you are scanning 'tuples' one by one and each tuple is different from other, so you won't need to access an old tuple via it's key.

So here, I suggest you to create the model class suitable for the key-value pairs and store them in a list. You can push that list when you are done.

Sample model for tuple:

public class KeyValuePair {
    private String key;
    private String value;
    public KeyValuePair(String key, String value) {
        this.key = key;
        this.value = value;
    }
}

Sample List to store:

List<KeyValuePair> keyValuePairList = new ArrayList<>();
keyValuePairList.add(new KeyValuePair("0", "816444014066"));
keyValuePairList.add(new KeyValuePair("1", "747083010945"));
keyValuePairList.add(new KeyValuePair("2", "816444010969"));

Storing:

If you cannot pass the data between activities, check out SQLite. You can store the data in SQLite and fetch it when needed. It is an offline database works on Android devices. You can delete the data when pushed to upstream.

Edit:

If the keys are the orders, you can simply use a String List like this:

List<String> keyValuePairList = new ArrayList<>();
keyValuePairList.add("816444014066");
keyValuePairList.add("747083010945");
keyValuePairList.add("816444010969");
like image 27
Bahadir Tasdemir Avatar answered Sep 21 '22 17:09

Bahadir Tasdemir


Use hashmap to store the data and then use Gson to convert HashMap to String and then save it to SharedPrefs

private void hashmaptest()
{
    //create test hashmap
    HashMap<String, String> testHashMap = new     HashMap<String, String>();
testHashMap.put("key1", "value1");
testHashMap.put("key2", "value2");

//convert to string using gson
Gson gson = new Gson();
String hashMapString = gson.toJson(testHashMap);

//save in shared prefs
SharedPreferences prefs = getSharedPreferences("test", MODE_PRIVATE);
prefs.edit().putString("hashString", hashMapString).apply();

//get from shared prefs
String storedHashMapString = prefs.getString("hashString", "oopsDintWork");
java.lang.reflect.Type type = new TypeToken<HashMap<String, String>>(){}.getType();
HashMap<String, String> testHashMap2 = gson.fromJson(storedHashMapString, type);

//use values
String toastString = testHashMap2.get("key1") + " | " + testHashMap2.get("key2");
Toast.makeText(this, toastString, Toast.LENGTH_LONG).show();
}

Source : Saving a hash map into Shared Preferences

like image 33
Araju Avatar answered Sep 18 '22 17:09

Araju