【发布时间】:2016-06-20 06:42:15
【问题描述】:
我在完成代码以从我制作的列表中打开活动时遇到问题。我正在制作一个方程式应用程序,我想要一个主题列表,当您单击一个时,它会启动包含我已经拥有的方程式的 .xml 文件。我已经将活动与 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);
}
});
}
strings.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>
还有我的主要活动:
<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>
我需要帮助来完成这项工作!
【问题讨论】:
-
具体是什么问题?
-
请注意,您在 Android Studio 中编写代码,它不是启动活动,您的问题与 Android Studio 无关。
-
抱歉,问题是我不知道如何使用 ListView 启动活动。我尝试了代码,但它标记了很多错误。
标签: java android listview android-studio