gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:02:58 24 4
gpt4 key购买 nike

这是我的类(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com