繁体   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