0

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"/>


Jacobietje
  • 23
  • 5
  • I think you have to set `android:focusable="false" ` to the `ListView` . Please check this question http://stackoverflow.com/a/9841730/2438460 – Ye Lin Aung Dec 08 '13 at 15:31
  • Where are you getting your layout from in the Menu class? There is no setContentView() in the Menu class. And if there is a XML layout file, are you using @android:id/list as id? – Ruben Weerts Dec 08 '13 at 15:47
  • Hi @RubenWeerts. Thank you for your reply. I'll edit my question by adding my main.xml which is my menu.xml file. I added also the setContentView() in my menu class. It didn't work out. Again thanks for your reply! Jacob – Jacobietje Dec 08 '13 at 15:53

3 Answers3

0

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();
    }
}
                }
         );
Ahmed Ekri
  • 4,601
  • 3
  • 23
  • 42
  • I don't think I really got you. Where do you put the: listview.setOnItemClickListener(this); ? Thanks for your quick reply! – Jacobietje Dec 08 '13 at 15:46
  • it still isn't working. Do you have any further ideas? :) Thank you for your time! – Jacobietje Dec 08 '13 at 16:21
  • thank you for your help! I didn't used your method for 100 percent, but it helped me alot! I'll put mine as an answer to this question.. Thanks again! Jacob. – Jacobietje Dec 09 '13 at 15:23
  • welcome =), if it helped you please rate it up (the up arrow), if u are seeing it as an answer accept it (the tick) – Ahmed Ekri Dec 09 '13 at 16:37
0

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();
  }
0

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!

Jacobietje
  • 23
  • 5