gpt4 book ai didi

java - Android Studio - 如何从 ListView 打开特定 Activity ?

转载 作者:行者123 更新时间:2023-12-02 03:51:41 26 4
gpt4 key购买 nike

我在完成代码以打开我制作的列表中的 Activity 时遇到问题。我正在制作一个方程式应用程序,我想要一个主题列表,当您单击其中一个时,它会启动包含我已经拥有的方程式的 .xml 文件。我已经将 Activity 与 java 类联系起来了。

这是我的代码:

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);

//get list view from xml
temasListView = (ListView) findViewById(R.id.temasListView);


String[] Temas = {
"Conversion",
"Suma",
"Trigonometria",
"Primera",
"Momento",
"Centro",
"Segunda1",
"MRU",
"MRUA",
"Tiro",
"Segunda2"};

ListAdapter temasAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Temas);
ListView temasListView = (ListView) findViewById(R.id.temasListView);
temasListView.setAdapter(temasAdapter);

temasListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String temas = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(Temas.this, temas, Toast.LENGTH_LONG).show();

if (position == 0) {
Intent intent = new Intent(this, Conversion.class);
startActivity(intent);
}
else if (position == 1) {
Intent intent = new Intent(this, Suma.class);
startActivity(intent);
}
else if (position == 2) {
Intent intent = new Intent(this, Trigonometria.class);
startActivity(intent);
}
else if (position == 3) {
Intent intent = new Intent(this, Primera.class);
startActivity(intent);}

else if (position == 4) {
Intent intent = new Intent(this, Momento.class);
startActivity(intent);
}
else if (position == 5) {
Intent intent = new Intent(this, Centro.class);
startActivity(intent);
}
else if (position == 6) {
Intent intent = new Intent(this, Segunda1.class);
startActivity(intent);
}

else if (position == 7) {
Intent intent = new Intent(this, MRU.class);
startActivity(intent);
}
else if (position == 8) {
Intent intent = new Intent(this, MRUA.class);
startActivity(intent);
}
else if (position == 9) {
Intent intent = new Intent(this, Tiro.class);
startActivity(intent);
}
else if (position == 10) {
Intent intent = new Intent(this, Segunda2.class);
startActivity(intent);
}




});
}

字符串.xml:

<resources>
<string name="app_name"></string>

<string-array name="temas">
<item>Conversión de Unidades</item>
<item>Suma de Vectores</item>
<item>Trigonometría</item>
<item>Primera Ley de Newton</item>
<item>Momento de Fuerzas</item>
<item>Centro de Gravedad</item>
<item>Componente de Velocidad</item>
<item>Segunda Ley de Newton</item>
<item>Movimiento Rectilíneo Uniforme</item>
<item>MRUA</item>
<item>Tiro Vertical</item>
<item>Segunda Ley de Newton (DCL)</item>


</string-array>

我的主要 Activity :

<LinearLayout   
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="@array/temas"
android:id="@+id/temasListView"
android:layout_weight="1.05"
android:background="#dadada" />

</LinearLayout>

我需要帮助才能完成此任务!

最佳答案

当您在匿名内部类中时,this 将不会引用您当前的 Activity 类。

您应该使用MainActivity.this而不是this

Intent intent = new Intent(MainActivity.this, Conversion.class);
startActivity(intent);

您可以像这样重构代码,以摆脱 switch case。

String className= parent.getItemAtPosition(position).toString();
Class myClass=Class.forName("yourpackagename"+className);
Intent intent = new Intent(MainActivity.this, myClass);
startActivity(intent);

不需要使用很多开关条件。

另外,正如上面答案中指出的,在 Toast 消息中使用 MainActivity.this 代替 Temas.this

关于java - Android Studio - 如何从 ListView 打开特定 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35831476/

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