java - 我怎样才能访问android中另一个类中选定的列表项

标签 java android listactivity

这是我的类(class),当我运行应用程序时,它在选择时崩溃并给我一个空错误。我正在设计一个修订应用程序,并且尝试做到这一点,而无需为涵盖的每个主题开设一门类(class),任何帮助将不胜感激。

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

        }

关于java - 我怎样才能访问android中另一个类中选定的列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34772026/

相关文章:

android - 如何在显示 SQlite 数据的 listActivity 中检索过滤 ListView 的特定列表项的 rowId?

java - 安卓 : Switching and update layout

java - 添加存储在字符串变量中的数字

java - Android 处理 App 的关闭

Android:如何在不显示预览的情况下捕获图像

android - 如何使用 adb 卸载 chainfire3d? (安卓)

android - 无法在 ListView 中显示表格的内容

java - xsd 未在某些系统上验证 : "Cannot resolve the name to a(n) ' type definition' component.“

java - 如何在透明 JPanel 上添加 JComboBox?

android - 动态 ListView 扩展 Activity