在 TextView 中显示 ArrayMap 的对象
Show in a TextView an object of the ArrayMap
我需要在 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);
}
}
我试过了,但它告诉我他需要一个字符串而不是一个对象,但我不知道如何转换它。
如果有人能帮助我,我将不胜感激。
如果我是正确的,这应该可以完成工作
String prova = arrayMap.toString();
要在 TextView 中显示 ArrayMap 对象,您必须将其转换为 String 对象。
要做到这一点,你可以简单地通过键获取对象然后转换它:
String itemAsString = arrayMap.get(2).toString();
我需要在 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);
}
}
我试过了,但它告诉我他需要一个字符串而不是一个对象,但我不知道如何转换它。 如果有人能帮助我,我将不胜感激。
如果我是正确的,这应该可以完成工作
String prova = arrayMap.toString();
要在 TextView 中显示 ArrayMap 对象,您必须将其转换为 String 对象。
要做到这一点,你可以简单地通过键获取对象然后转换它:
String itemAsString = arrayMap.get(2).toString();