Autocompletetextview with multiple variables

138
July 08, 2017, at 4:05 PM

i have a JSON :

{"results":[
    {"id":"21","name":"Jonny"},
    {"id":"22","name":"Samuel"},
    {"id":"23","name":"Martha"}
    ]}

i required to put "name" into an autocompletetextview . i need to post "id" to other activity, based on selected name in autocompletetextview. Please help, thanks before

Answer 1

First convert json to hashmap:

HashMap<String, Integer> result = readJson("Your json string here");

public HashMap<String, Integer> readJson(String json) {
    try {
        JSONObject jsonObj = new JSONObject(json);
        // Getting JSON Array node
        JSONArray results = jsonObj.getJSONArray("results");
        final int j = results.length();
        HashMap<String, Integer> resultMap = new HashMap<>(j);
        for (int i = 0; i < j; i++) {
            JSONObject item = results.getJSONObject(i);
            resultMap.put(item.getString("name"), item.getInt("id"));
        }
        return resultMap;
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
}

Now set autocomplete adapter like:

autoComplete.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_dropdown_item_1line, new ArrayList<>(result.keySet())));
public void startNextActivity(int id) {
    Intent intent = new Intent(getContext(), NextActivity.class);
    intent.putExtra("id", id);
    getContext().startActivity(intent);
}

call the start activity like:

        startNextActivity(result.get(autoComplete.getText().toString()));
READ ALSO
How can I read a file packaged with an android app through a C++ library?

How can I read a file packaged with an android app through a C++ library?

I have written a Unity application in C# that uses a C++ library built using Android NDKI want to read some files that are packaged with the android application (in the assets folder) from this C++ library

170
Unable to read output from barcode scanner

Unable to read output from barcode scanner

I want to upload the result from the barcode scanner to the edit text, but it is not displaying in the edit textMay I know what the problem is and how do I solve it?

208
vue-cli + webpack: where to include npm package js and css files?

vue-cli + webpack: where to include npm package js and css files?

I'm trying to import an npm package into my vue-cli dev siteHere are the instructions provided by the package website:

396
check 2 arrays and see if an ID is in them and then swap out the ID for another and do this for multiple collections

check 2 arrays and see if an ID is in them and then swap out the ID for another and do this for multiple collections

I have a bunch of collections/ models that have useful and notUseful arrays in each documentsThey may contain ids

132