Listactivity does not work Android

Asked

Viewed 175 times

0

I have an Activity with a button calling a List Activity. This Listactivity quickly creates a string list, I just want to show this list on the screen and display the selected item in a Toast.

But it’s not working! Follow the code of the List class

public class Lista extends ListActivity{

    protected void OnCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        String[] itens = new String[]{"João","josé","pedro",};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,itens);
    setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Object o = this.getListAdapter().getItem(position);
        String item = o.toString();
        Toast.makeText(this, "Clicou em: "+item, Toast.LENGTH_SHORT).show();
    }

}

What is missing?

  • What is the error shown in the console? Is the method even called? (put the debug or a sysout to check) If a better description of the scenario becomes difficult to help

1 answer

4


The code Java is case-sensitive, that means that Double is different from double, String is different string and so on.

Therefore, you have an error in your code since the method Oncreate does not exist in the class ListActivity (and in turn, in the Activity) which was inherited and it turns out that it never runs in your class. Therefore, you just need to change the spelling error to onCreate in the signature of the method:

protected void onCreate(Bundle savedInstanceState)

It is always good to use the note @Override, thus ensures that the method that is actually wanting to redefine exists in the super class.

  • Got it. Well, I’m a beginner in java, at this point I can’t test this, but I will validate your answer as soon as possible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.