簡體   English   中英

Android Bundle Concept在活動之間傳遞deta

[英]Android Bundle Concept to pass deta between activities

我是android新手,當我嘗試將從curser檢索到的數據傳遞到bundle時遇到了問題,然后我無法獲取該變量的值。以下是我的代碼,請幫助我擺脫這種情況。

光標cur3 = db3.rawQuery(“ SELECT * FROM” + TableName,null);

try {

            db3  = this.openOrCreateDatabase("remoteid.db", MODE_PRIVATE, null);

            if(cur3 != null )  
            {   
                if(cur3.moveToFirst())
                {
                    do {
                        valueOfID =  cur3.getString(cur3.getColumnIndex("PretestID"));
                         valuOfDate = cur3.getString(cur3.getColumnIndex("Date"));
                         textType = cur3.getString(cur3.getColumnIndex("txtVCT"));                                                      
                         valueOfDDLTS = cur3.getString(cur3.getColumnIndex("ddlTestingSession"));
                         valueOfReason = cur3.getString(cur3.getColumnIndex("txtReason"));
                         valueOfHowmany = cur3.getString(cur3.getColumnIndex("txthowmany"));
                         valueOftxtques1 = cur3.getString(cur3.getColumnIndex("txtques1"));
                         valueOfrblques2a = cur3.getString(cur3.getColumnIndex("rblques2a"));
                         valueOfrblques2b = cur3.getString(cur3.getColumnIndex("rblques2b"));
                         valueOfrblques3 = cur3.getString(cur3.getColumnIndex("rblques3"));
                         valueOftxtques4 = cur3.getString(cur3.getColumnIndex("txtques4"));
                         valueOfrblques5 = cur3.getString(cur3.getColumnIndex("rblques5"));
                         valueOfrblques6 = cur3.getString(cur3.getColumnIndex("rblques6"));
                         valueOfrblques7 = cur3.getString(cur3.getColumnIndex("rblques7"));
                         valueOfrblques8 = cur3.getString(cur3.getColumnIndex("rblques8"));
                         valueOfrblques9 = cur3.getString(cur3.getColumnIndex("rblques9"));
                         valueOfddlsick = cur3.getString(cur3.getColumnIndex("ddlsick"));
                         valueOftxtques11 = cur3.getString(cur3.getColumnIndex("txtques11"));
                         valueOfrblques12 = cur3.getString(cur3.getColumnIndex("rblques12"));
                         valueOftxtques13 = cur3.getString(cur3.getColumnIndex("txtques13"));
                         valueOftxtques14 = cur3.getString(cur3.getColumnIndex("txtques14"));
                         valueOfrblques15 = cur3.getString(cur3.getColumnIndex("rblques15"));
                         valueOfrblques16 = cur3.getString(cur3.getColumnIndex("rblques16"));
                         valueOfrblques17 = cur3.getString(cur3.getColumnIndex("rblques17"));
                         valueOftxtques18 = cur3.getString(cur3.getColumnIndex("txtques18"));

                         //Toast.makeText(getApplicationContext(), valueOftxtques18, Toast.LENGTH_SHORT).show();

                    }while (cur3.moveToNext());
                }
            }

        }

        catch(Exception e) {
            Log.e("Error", "Error", e);

        } finally {
            if (db3 != null)
                db3.close();
        }
        cur3.close();   



        arrayadapter11 = new simpleefficientadapter(Screening.this,prtestData);
        arrayadapter22 = new simpleefficientadapter(Screening.this,screeningData);
        arrayadapter33 = new simpleefficientadapter(Screening.this,postData);

        mylist1.setAdapter(arrayadapter11);
        mylist1.setOnItemClickListener(this);
        mylist2.setAdapter(arrayadapter22);
        mylist2.setOnItemClickListener(this);
        mylist3.setAdapter(arrayadapter33);
        mylist3.setOnItemClickListener(this);
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Intent intent;
        switch (arg0.getId()) {
        case R.id.prescreenlist:            
            intent = new Intent(getApplicationContext(), NewScreening.class);       
            Bundle bundle = new Bundle();   

            bundle.putString("DateValue", valuOfDate);  
            bundle.putString("TT", textType);   
            bundle.putString("idValue", valueOfID); 
            bundle.putString("ddltsValue", valueOfDDLTS);   
            bundle.putString("reasonValue", valueOfReason); 
            bundle.putString("howmanyValue", valueOfHowmany);   
            bundle.putString("textqus1Value", valueOftxtques1); 
            bundle.putString("textqus2aValue", valueOfrblques2a);   
            bundle.putString("textqus2bValue", valueOfrblques2b);   
            bundle.putString("rbqs3Value", valueOfrblques3);    
            bundle.putString("rbqs4Value", valueOftxtques4);    
            bundle.putString("rbqs5Value", valueOfrblques5);    
            bundle.putString("rbqs6Value", valueOfrblques6);    
            bundle.putString("rbqs7Value", valueOfrblques7);    
            bundle.putString("rbqs8Value", valueOfrblques8);    
            bundle.putString("rbqs9Value", valueOfrblques9);    
            bundle.putString("ddlsValue", valueOfddlsick);  
            bundle.putString("tq11Value", valueOftxtques11);    
            bundle.putString("tq12Value", valueOfrblques12);    
            bundle.putString("tq13Value", valueOftxtques13);    
            bundle.putString("tq14Value", valueOftxtques14);    
            bundle.putString("rbqs15Value", valueOfrblques15);  
            bundle.putString("rbqs16Value", valueOfrblques16);
            bundle.putString("rbqs17Value", valueOfrblques17);  
            bundle.putString("rbqs18Value", valueOftxtques18);

            intent.putExtras(bundle);

            startActivityForResult(intent, 1);

            setResult(1,intent);


            break;

明智的做法可能是將該數據庫行的ID傳遞給下一個Activity。

而且您的代碼是不完整的,但是看起來只有最后一行的數據才會傳遞到下一個活動。

另外,如果您僅將Extra直接放入意圖中,而不是將中介Bundle放入意圖中,則可能會更有效和更明確。

好的做法是在附加名稱中包括程序包名稱作為前綴。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM