首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android中访问其他类中选择的列表项

如何在android中访问其他类中选择的列表项
EN

Stack Overflow用户
提问于 2016-01-14 00:33:04
回答 4查看 49关注 0票数 1

以下是我的类,当我运行应用程序时,它在选择时崩溃,并给我一个空错误。我正在设计一个修订应用程序,并试图做到这一点,而不必为涵盖的每个主题的类,任何帮助都会非常感谢。

代码语言:javascript
复制
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));
    }
}
EN

回答 4

Stack Overflow用户

发布于 2016-01-14 00:37:52

试着这样做

代码语言:javascript
复制
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");

        }
票数 0
EN

Stack Overflow用户

发布于 2016-01-14 00:39:00

你对你处理活动的方式的理解有一点错误。

通常,您永远不会自己实例化一个活动。

您需要研究如何使用IntentsstartActivityonActivityResult

首先,创建一个TheoryTopicListIntent,然后设置结果数据并完成该Activity。然后从onActivityResult读取选定的选项。

票数 0
EN

Stack Overflow用户

发布于 2016-01-14 01:25:46

在您的TheoryTopicList活动中,当您触发调用TheoryMain活动的意图时,您可以将数据与该意图一起发送。这些数据可以由TheoryMain类接收,并用于执行适当的操作。

在TheoryTopicList活动中向您的意图添加数据。

代码语言:javascript
复制
Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");
intent.putExtra("choice_selected", choice);

//第一个参数是key,下一个参数是value//键,以便在下一个活动中检索该值。

代码语言:javascript
复制
startActivity(intent);

从TheoryMain,中删除活动实例化行

代码语言:javascript
复制
ttl = new TheoryTopicList(); // Not needed and not good
ttl.getChoice(); // Not needed and not good

现在检索上一个活动提供给您的数据。在你的onCreate of TheoryMain add中,

代码语言:javascript
复制
...

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

...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34772026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档