在TextView中显示ArrayMap的对象

时间:2019-02-12 15:28:52

标签: android array-map

我需要在TextView中显示ArrayMap的对象。 这是我的代码:

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);

}
}

我尝试过,但是它说我需要一个字符串而不是一个对象,但是我不知道如何转换它。 如果有人可以帮助我,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果我是正确的,那应该做的

String prova = arrayMap.toString();

答案 1 :(得分:1)

要在TextView中显示ArrayMap的对象,必须将其转换为String对象。

为此,您只需通过键即可获取对象,然后将其转换:

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