如何在Bundle中添加String - Integer对?

时间:2015-10-08 06:09:04

标签: android bundle

我曾经这样使用但它不起作用。 我希望将String作为键存储,将Integer作为值存储,并通过调用getInt(),我应该获得Integer值,可以根据列表项的单击动态进行动态调用。我发布了我的代码。

public class MainActivity extends Activity {

ListView emrcontact;

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

    emrcontact=(ListView) findViewById(R.id.listView1);

    final String[] emergency={"Local Police", "Fire Service", "Ambulance", "Blood Bank", "Womens Helpline", "Child Helpline", "Indian Railways"};
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,emergency);
    emrcontact.setAdapter(adapter);





    emrcontact.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                long arg3) {

            Bundle dataBundle=new Bundle();
            dataBundle.putInt("Local Police",100);
            dataBundle.putInt("Fire Service",101);
            dataBundle.putInt("Ambulance",102);
            dataBundle.putInt("Blood Bank",104);
            dataBundle.putInt("Womens Helpline",181);
            dataBundle.putInt("Child Helpline",1098);
            dataBundle.putInt("Indian Railways",1322);
            Intent dataIntent = new Intent();
            dataIntent.putExtras(dataBundle);

            Bundle b=getIntent().getExtras();

            switch (pos) {

            case 0:
                if(b!=null) 
                {
                callToemrcontact(b.getInt("Local Police"));
                }
                break;

            case 1:

                if(b!=null) 
                {
                callToemrcontact(b.getInt("Fire Service"));
                }
                break;

            case 2:

                if(b!=null) 
                {
                callToemrcontact(b.getInt("Ambulance"));
                }
                break;

            case 3:

                if(b!=null) 
                {
                callToemrcontact(b.getInt("Blood Bank"));
                }
                break;

            case 4:

                if(b!=null) 
                {
                callToemrcontact(b.getInt("Womens Helpline"));
                }
                break;

            case 5:

                if(b!=null) 
                {
                callToemrcontact(b.getInt("Child Helpline"));
                }
                break;

            case 6:

                if(b!=null) 
                {
                callToemrcontact(b.getInt("Indian Railways"));
                }
                break;

            default:
                break;
            }


        }
    });




}
void callToemrcontact(int i)
{
     Intent intent=new Intent(Intent.ACTION_DIAL);
     intent.setData(Uri.parse("tel:"+"i"));
     startActivity(intent);
}
}

logcat结果如下:

10-08 06:20:25.407: E/Trace(805): error opening trace file: No such file or directory (2)
10-08 06:20:25.967: D/dalvikvm(805): GC_FOR_ALLOC freed 58K, 8% free 2442K/2640K, paused 44ms, total 52ms
10-08 06:20:26.017: I/dalvikvm-heap(805): Grow heap (frag case) to 4.767MB for 2359312-byte allocation
10-08 06:20:26.107: D/dalvikvm(805): GC_FOR_ALLOC freed 2K, 5% free 4743K/4948K, paused 83ms, total 83ms
10-08 06:20:26.227: D/dalvikvm(805): GC_CONCURRENT freed <1K, 5% free 4743K/4948K, paused 5ms+4ms, total 119ms
10-08 06:20:26.467: D/dalvikvm(805): GC_CONCURRENT freed 2305K, 45% free 3115K/5616K, paused 20ms+25ms, total 79ms
10-08 06:20:26.737: D/libEGL(805): loaded /system/lib/egl/libEGL_emulation.so
10-08 06:20:26.747: D/(805): HostConnection::get() New Host Connection established 0x2a148e68, tid 805
10-08 06:20:26.777: D/libEGL(805): loaded /system/lib/egl/libGLESv1_CM_emulation.so
10-08 06:20:26.797: D/libEGL(805): loaded /system/lib/egl/libGLESv2_emulation.so
10-08 06:20:26.877: W/EGL_emulation(805): eglSurfaceAttrib not implemented
10-08 06:20:26.897: D/OpenGLRenderer(805): Enabling debug mode 0

3 个答案:

答案 0 :(得分:0)

你可以这样说:

dataBundle.putString("Ambulance",String.valueOf(YourInt));
....
....
Bundle b=getIntent().getExtras();
String temp = b.getString("Ambulance");

int myInt = Integer.parseInt(temp);

答案 1 :(得分:0)

你必须这样做 在片段A中

        `Bundle dataBundle=new Bundle();
        dataBundle.putInt("Local Police",100);
        dataBundle.putInt("Fire Service",101);`

在进行片段事务之前,请确保将包传递给 fragment.setArguments(包)
在片段B中

        `Bundle dataBundle=getArguments();
        dataBundle.getInt("Local Police");
        dataBundle.getInt("Fire Service");`

答案 2 :(得分:0)

您需要使用dataBundle变量而不是bb=getIntent().getExtras()将根据mainactivity收到的意图而不是您创建的包

提供捆绑