簡體   English   中英

如何在Android的另一個班級中訪問選定的列表項

[英]How can i gain access to the selected list item in another class in android

這是我的類,當我運行應用程序時,它在選擇時崩潰,並給了我一個空錯誤。 我正在設計一個修訂版應用程序,並試圖做到這一點,而不必為每個涉及任何幫助的主題都上一堂課,將不勝感激。

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.w3c.dom.Text;

public class TheoryMain extends Activity {
    TheoryTopicList ttl;
    TextView tv1;
    String choice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.theory_layout);
        ttl = new TheoryTopicList();
        ttl.getChoice();
        tv1 = (TextView) findViewById(R.id.theory_tv);
        switch(choice){
            case("Photo-Electric effect"):
                tv1.setText(getString(R.string.photo_electric));
                break;
            case("Photons and Electrons"):
                tv1.setText(getString(R.string.photons_electrons));
                break;
            case("de Broglie wavelength"):
                tv1.setText(getString(R.string.de_broglie));
                break;
            case("Types of particles"):
                tv1.setText(getString(R.string.particles));
                break;
            case("Interactions"):
                tv1.setText(getString(R.string.interactions));
                break;
            case("Radiation"):
                tv1.setText(getString(R.string.radiation));
                break;
            case("Voltage, Current and Resistance(Ohms law)"):
                tv1.setText(getString(R.string.ohms_law));
                break;
            case("Circuits"):
                tv1.setText(getString(R.string.circuits));
                break;
            case("Power and Efficiency"):
                tv1.setText(getString(R.string.power_efficiency));
                break;
            case("Alternating Current and oscilloscope graphs"):
                tv1.setText(getString(R.string.ac_graphs));
                break;
            case("E.M.F and internal Resistance"):
                tv1.setText(getString(R.string.emf_resistance));
                break;
        }
    }
}






import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class TheoryTopicList extends ListActivity {
    String[] display = {"Photo-Electric effect", "Photons and Electrons", "de Broglie wavelength", "Types of particles",
                        "Interactions", "Radiation", "Voltage, Current and Resistance(Ohms law)", "Circuits",
                        "Power and Efficiency","Alternating Current and oscilloscope graphs", "E.M.F and internal Resistance"};
    String choice;
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        super.onListItemClick(l, v, position, id);
        choice = display[position];
        Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");
        startActivity(intent);
    }

    public String getChoice(){
        return choice;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(TheoryTopicList.this,android.R.layout.simple_list_item_1,display));
    }
}

嘗試這樣做

Intent intent=new Intent(this,AnotherClass.class);
intent.putExtra("yourStringVal",yourStringVal);
startActivity(intent);
and in your another class try 2 get these items by doing

  Bundle extras = getIntent().getExtras();
        if(extras!=null){
            String yourStringVal=extras.getString("yourStringVal");

        }

您對“活動”工作方式的理解有些錯誤。

通常,您永遠不會自己實例一個活動。

您需要研究如何使用IntentsstartActivityonActivityResult

首先,您為TheoryTopicList創建一個Intent ,然后設置結果數據並完成Activity 然后,您從onActivityResult讀取選定的選項。

在您的TheoryTopicList活動中,當您觸發Intent來調用TheoryMain活動時,可以將數據與該Intent一起發送。 該數據可由TheoryMain類接收,並用於執行適當的操作。

將數據添加到您在TheoryTopicList活動中的意圖。

Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");
intent.putExtra("choice_selected", choice);

//第一個參數是鍵,第二個參數是值。 //鍵,以便在下一個活動中檢索值。

startActivity(intent);

從TheoryMain中刪除活動實例化行,

ttl = new TheoryTopicList(); // Not needed and not good
ttl.getChoice(); // Not needed and not good

現在檢索上一個活動提供給您的數據。 在您的TheoryCreate的onCreate中添加,

...

String choice = getIntent().getExtra("choice_selected"); // Using the key
switch(choice){

...
}

替換此代碼:

Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");

startActivity(intent);

Intent intent = new Intent(TheoryTopicList .this,TheoryMain .class);

intent.putExtra("choice",choice);

startActivity(intent);

在理論上,主要活動是創建方法。

取代這個

ttl = new TheoryTopicList();
ttl.getChoice();

if(getIntent().hasExtra("choice"))
 {
     choice = getIntent().getStringExtra("choice");
 }
else{
      choice ="";
}

暫無
暫無

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

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