简体   繁体   English

变量在活动之间丢失数据,我不知道为什么?

[英]Variables losing data between activities and I don't know why?

I am currently creating an android app that scans a network and outputs results in a ListView but I am trying to make it to where I tap on the network and it saves the data in a database then sends you to a page to show you what you selected but when I click an item it substrings the values correctly and displays work fine on the main activity but when I try to use the variables on my display page activity there values are set null.我目前正在创建一个 android 应用程序,它扫描网络并在 ListView 中输出结果,但我正在尝试将其发送到我点击网络的位置并将数据保存在数据库中,然后将您发送到一个页面以显示您的内容选择但当我单击一个项目时,它会正确地对值进行子串并在主活动上正常显示,但是当我尝试在显示页面活动上使用变量时,值设置为空。

Here is the main activity in the click listener:这是点击侦听器中的主要活动:

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



        }
    });

Here is my display page:这是我的显示页面:

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

        }


    });

}

} }

Here is my setters and getters class:这是我的 setter 和 getter 类:

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;

}

} }

Figured out you have to pass the variable with the intent:发现你必须传递带有意图的变量:

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

Then use the key that you set in putExtra()然后使用您在 putExtra() 中设置的密钥

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

Thanks for the help though!不过还是谢谢你的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM