2016-11-16 45 views
-4

我目前正在創建一個掃描網絡並在ListView中輸出結果的android應用程序,但我試圖將其放到網絡中的任意位置,然後將數據保存到數據庫中,然後將您發送到頁面以向您顯示你選擇了什麼,但是當我點擊一個項目時,它正確地對這些值進行子串處理,並且在主要活動中顯示工作正常,但是當我嘗試在顯示頁面活動中使用變量時,值被設置爲null。變量在活動之間丟失數據,我不知道爲什麼?

這裏是在點擊監聽器的主要活動:

networklist.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      String grabItemInfo = wifis[position]; 

      Network_Info info1 = new Network_Info(); 

      info1.setMainBSSID(grabItemInfo.substring(grabItemInfo.indexOf('#') +1, grabItemInfo.lastIndexOf('#'))); 
      info1.setMainSSID(grabItemInfo.substring(0,(grabItemInfo.indexOf('#')))); 
      info1.setMainCAP(grabItemInfo.substring(grabItemInfo.lastIndexOf('#')+1, grabItemInfo.length())); 


      Toast toastTest = Toast.makeText(getApplicationContext(), info1.getMainSSID(), Toast.LENGTH_SHORT); 
      Toast toastTest2 = Toast.makeText(getApplicationContext(), info1.getMainBSSID(), Toast.LENGTH_SHORT); 
      Toast toastTest3 = Toast.makeText(getApplicationContext(), info1.getMainCAP(), Toast.LENGTH_SHORT); 
      toastTest.show(); 
      toastTest2.show(); 
      toastTest3.show(); 

      ContentValues dbv = new ContentValues(); 

      dbv.put("SSID", info1.getMainSSID()); 
      dbv.put("BSSID", info1.getMainBSSID()); 
      dbv.put("CAPABILITIES", info1.getMainCAP()); 




      netDataBase.insert("netDataTable", "NULL", dbv); 


      Intent intent = new Intent(getApplicationContext(), Attack_Page.class); 
      startActivity(intent); 



     } 
    }); 

這是我的顯示頁面:

public class Attack_Page extends Network_List { 


protected void onCreate(Bundle SavedIS){ 

    super.onCreate(SavedIS); 
    setContentView(R.layout.attack_page); 


    TextView SSIDview = (TextView) findViewById(R.id.SSIDView); 
    TextView BSSIDview = (TextView) findViewById(R.id.BSSIDView); 
    TextView CAPview = (TextView) findViewById(R.id.CAPView); 
    Button backButton = (Button) findViewById(R.id.backbutton); 
    Intent intent = getIntent(); 

    //String MainSSIDP = intent.getStringExtra(getMainSSID()); 

    Network_Info info1 = new Network_Info(); 

    Toast testToast = Toast.makeText(getApplicationContext(), info1.getMainSSID(), Toast.LENGTH_SHORT); 
    testToast.show(); 

    //Cursor IDselect = netDataBase.rawQuery("SELECT SSID FROM netDataTable WHERE SSID = "+getMainSSID()+"", wifis); 

    //SSIDview.setText(IDselect.toString()); 


    backButton.setOnClickListener(new View.OnClickListener(){ 

     public void onClick(View v){ 

      Intent bintent = new Intent(getApplicationContext(), Network_List.class); 
      startActivity(bintent); 

     } 


    }); 

} 

}

這是我的getter和setter方法類:

public class Network_Info { 
    private String mainCAP; 
    private String mainSSID; 
    private String mainBSSID; 

public void setMainSSID(String newMainSSID){ 

    mainSSID = newMainSSID; 

} 

public void setMainBSSID(String newMainBSSID){ 

    mainBSSID = newMainBSSID; 

} 

public void setMainCAP(String newMainCAP){ 

    mainCAP = newMainCAP; 

} 

public String getMainSSID(){ 

    return mainSSID; 

} 
public String getMainBSSID(){ 

    return mainBSSID; 

} 
public String getMainCAP(){ 

    return mainCAP; 

} 

}

+3

在您的diplay頁面上,您只是實例化Network_info,而不是實際從數據庫中讀取 –

+1

不相關,但是'netDataBase.insert(「netDataTable」,「NULL」,dbv);'應該有'null'作爲第二個param,而不是字面意思''NULL'' –

+0

另外,你真的需要數據庫嗎?你應該能夠直接在數據庫中傳遞數據。 –

回答

0

想通了,你必須傳遞變量的意圖:

Intent intent = new Intent(getApplicationContext(), Attack_Page.class); 
      intent.putExtra("EXTRA_SSID", info1.getMainSSID()); 
      intent.putExtra("EXTRA_BSSID", info1.getMainBSSID()); 
      intent.putExtra("EXTRA_CAP", info1.getMainCAP()); 
      startActivity(intent); 

然後使用您在putExtra設置鍵()

String MainSSIDP = intent.getStringExtra("EXTRA_SSID"); 

感謝儘管幫助!

相關問題