Search code examples
androidarray-map

Show in a TextView an object of the ArrayMap


I need to show in a TextView an object of an ArrayMap. This is my code:

public class MainActivity extends AppCompatActivity {

Button btnHit;
TextView txtJson;
ArrayMap arrayMap = new ArrayMap();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnHit = (Button) findViewById(R.id.btnHit);
    txtJson = (TextView) findViewById(R.id.sku);
    Riempimento();
}

public void Riempimento(){

    arrayMap.put(1,"a");
    arrayMap.put(2,"b");
    arrayMap.put(3,"c");
    arrayMap.put(4,"d");

}

public void onClick(View v) {
    String prova = //here I need to get the object, for example the n°.2
    txtJson.setText(prova);

}
}

I tried but it says me that he needs a string and not an object but i Don't know how to convert it. If someone can help me I'll appreciate it.


Solution

  • To show an object of an ArrayMap in the TextView u must convert it to a String object.

    To do that, u can simply obtain the object by key and then convert it:

    String itemAsString = arrayMap.get(2).toString();