I have a problem with my onListItemClick.
This is my Menu.java class :
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();
}
}
And this is my MainActivity class (it's a BMI calculator.):
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";
}
}
}
If I launch my emulator, it doesn't respond when clicking on a list item. Someone an idea? Yes, I've checked all related questions on this website.
EDIT: (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"/>
change public class Menu extends ListActivity {
to
public class Menu extends ListActivity implements OnItemClickListener {
and try listview.setOnItemClickListener(this);
update
add
ListView list1 = (ListView) findViewById(R.id.list);
list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, classes));
list1 = getListView();
to your onCreate
and then
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();
}
}
}
);
You did not register your event click with your LIstView Activity. Here How:In your Menu.java write the following..
@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();
}
This is what finnaly worked for me:
@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;
}
}
Thanks everybody who helped me out, and answered my questions!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.