Lars Van Beek
Lars Van Beek

Reputation: 51

Positioning in android listview

I'm having some trouble with the positioning in an android listview, the next activity isn't loading, while they are both in the manifest.

Here's my code:

package com.WNF;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class Actiemenu extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // storing string resources into Array
    String[] actiemenu = getResources().getStringArray(R.array.actiemenu);

    // Binding resources Array to ListAdapter
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.actiemenu, R.id.label,     actiemenu));

    ListView lv = getListView();

    // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

            switch(position) {
            case '0' :            
                Intent intent = new Intent(Actiemenu.this, Acties.class);
                intent.putExtra("extras", position);
                startActivity(intent);
                break;
            case '1' :            
                Intent intent2 = new Intent(Actiemenu.this, Acties2.class);
                intent2.putExtra("extras", position);
                startActivity(intent2);
                break;
        }


      }
    });

}
}

The code to receive the intent is also present in Acties

package com.WNF;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

public class Acties extends Activity{
// aanroepen van een bundle, kan je elke naam geven die je maar wilt, 
//zolang de bundle als de onCreate maar dezelfde naam hebben
@Override  
public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        // de setContentView is niets meer dan de gegevens van de
//View ophalen uit de R.layout.naamvandeXML
        // Onthoud goed dat je dezelfde XMLs voor meerdere pagina's 
//kan gebruiken.
        setContentView(R.layout.acties1);

        Intent i = getIntent();
        i.getStringExtra("extras");
   }
}

Can anyone see what I'm doing wrong?? Thanks in advance :)

Upvotes: 0

Views: 92

Answers (1)

theisenp
theisenp

Reputation: 8719

Your switch statement is incorrectly trying to match the integer position value to the ASCII characters '0' and '1'. Since the ASCII value of '0' is 48 and the ASCII value of '1' is 49, nothing happens when you click the first two list items.

You should eliminate the single quotes and simply use integers:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    switch(position) {
    case 0: //Instead of '0' 
        Intent intent = new Intent(Actiemenu.this, Acties.class);
        intent.putExtra("extras", position);
        startActivity(intent);
        break;
    case 1: //Instead of '1'
        Intent intent2 = new Intent(Actiemenu.this, Acties2.class);
        intent2.putExtra("extras", position);
        startActivity(intent2);
        break;
}

Upvotes: 1

Related Questions