Question

For some reason my onListItemClick(View listView, View view, int position, long id){} is not firing under certain conditions. My application flows like this. The ListActivity fires another activity, and sometimes users, or other conditions, may cause that activity to finish with a result. The first time the ListActivity is fired, meaning that the onCreate method is called, then the onListemClick() always works. The same is the case for the first time the users return to the same activity, then the onListItemClick always work. The problem arises the second time the users return to the ListActivity, then the onListItemClick doesn't fire at all, its like there is no listener attached to the views at all.

I did double check that a listAdapter is set. It is the standard ArrayAdapter. All other listeners are functional, so my suspicions revolve around that the ListActivity is not used as the listener for some reason, but I cannot fathom why. It is the exact same code with the exact same values running both times, but it seems the listener is null'ed for some reason. The code is executed from the onResume method on the line after setContentView(int id) is called. Does anyone have any idea why this is happening?

The following code initiates the layout, there are no problems there, it all works out as intended.

private void initLayout(){

    projectsToList(user.getProjects());

    ttvEmailField = (TextView) findViewById(R.id.ttv_email);
    ttvNameField = (TextView) findViewById(R.id.ttv_username);

    String email = user.getEMail();
    String name = user.getName();

    if(email != null && email.length() > 0){
        ttvEmailField.setText(user.getEMail());
    }

    if(name != null && email.length() > 0){
        ttvNameField.setText(user.getName());
    }

    btnLogout = (Button) findViewById(R.id.btn_logout);

    btnLogout.setOnClickListener(this);

}

The below code sets the ArrayAdapter, the onListItem only works after onCreate and the first time the user navigates back to the currentactivity.

private void projectsToList(List<ProjectPojo> projects){
    int max = projects.size();
    String[] arrProjects = new String[max];

    for(int i = 0; i < max; ++i){
        arrProjects[i] = projects.get(i).getName();
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item_simpletext, R.id.ttv_listtextview, arrProjects);

    setListAdapter(adapter);
}
Was it helpful?

Solution 3

I never found out what was going on in this one. The solution I came up with was not to use inheritance and implement an OnItemClickListener manually instead. Now it works every time.

OTHER TIPS

Do your initialization work in oncreate and also setting adapter in oncreate() method. May be yyour are not setting listener in OnResume() method. Or you are setting listener is equal to null in on resume method.

Re-initialised your list or listener in Onresume() method! Your solution lies in onResume() method only..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top