我的onListItemClick有问题。
这是我的 Menu.java类:
public class Menu extends ListActivity{
String classes[] = { "MainActivity", "etcetera1", "etcetera2", "etcetera3", "etcetera4", "etcetera5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
}
@SuppressWarnings("rawtypes")
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String positie = classes[position];
try{
Class ourClass = Class.forName("com.jacob.eindproject.MAIN" + positie);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
这是我的 MainActivity类(它是BMI计算器。):
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate het menu
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void calculateClickHandler(View view) {
// handeld het klikken op de button
if (view.getId() == R.id.berekenButton) {
// referenties
EditText gewichtText = (EditText)findViewById(R.id.gewichtText);
EditText lengteText = (EditText)findViewById(R.id.lengteText);
TextView resultaat = (TextView)findViewById(R.id.resultaat);
// waarden
float gewicht = Float.parseFloat(gewichtText.getText().toString());
float lengte = Float.parseFloat(lengteText.getText().toString());
// berekend de BMI
float bmiWaarde = berekenBMI(gewicht, lengte);
// zoekt de bijbehorende label (overgewicht etc.)
String bmiInterpretation = interpretBMI(bmiWaarde);
// weergeeft het resultaat
resultaat.setText(bmiWaarde + "-" + bmiInterpretation);
}
}
// de formule
private float berekenBMI (float gewicht, float lengte) {
return (float) (gewicht / (lengte * lengte));
}
// bijbehorende labels:
private String interpretBMI(float bmiWaarde) {
if (bmiWaarde < 16) {
return "Ernstig Ondergewicht";
}
else if (bmiWaarde < 18.5) {
return "Ondergewicht";
}
else if (bmiWaarde < 25) {
return "Normaal";
}
else if (bmiWaarde < 30) {
return "Overgewicht";
}
else {
return "Obees";
}
}
}
如果我启动我的模拟器,则单击列表项时它不会响应。有人有想法吗?是的,我已在本网站上查看了所有相关问题。
编辑:(main.xml)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/aboutUs"
android:title="@+id/About"/>
<item
android:id="@+id/instellingen"
android:title="@+id/Instellingen"/>
答案 0 :(得分:0)
将public class Menu extends ListActivity {
更改为
public class Menu extends ListActivity implements OnItemClickListener {
并尝试listview.setOnItemClickListener(this);
<强>更新强>
添加
ListView list1 = (ListView) findViewById(R.id.list);
list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, classes));
list1 = getListView();
到onCreate
然后
list1.setOnItemClickListener(
new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String positie = classes[position];
try{
Class ourClass = Class.forName("com.jacob.eindproject.MAIN" + positie);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
);
答案 1 :(得分:0)
您没有在LIstView活动中注册您的活动点击。 这里如何:在你的Menu.java中写下以下内容..
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// this you want to do
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
答案 2 :(得分:0)
这最终对我有用:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//Posistion 0 is the first item (the BMI-Calculator in my project.)
super.onListItemClick(l, v, position, id);
switch(position)
{
case 0:
Intent openStartingPoint = new Intent(getApplicationContext(),MainActivity.class);
startActivity(openStartingPoint);
break;
}
}
感谢所有帮助我的人,并回答了我的问题!