Android: onListItemClick not opening up the .xml file
public void onListItemClick(ListView l, View v, int position, long id) { if(pos开发者_C百科ition == 0){ setContentView(R.layout.cuisine); } }
I have an array of Strings and i'm using the above method to try and open up a new xml file called 'cuisine' when it is clicked. but it keeps failing!
Have I done this right, or what am I doing wrong?
Thanks.
Ok from looking at similar problems on the web, people have said to get the onListItemClick() to start a new activity and using that new activity to then open up the new view?
So what i've done is this...
protected void onListItemClick(ListView l, View v, int position, long id)
{
Intent dundrumIntent = new Intent(v.getContext(), DundrumSelector.class);
dundrumIntent.putExtra("position", position);
startActivityForResult(dundrumIntent, 0);
}
and then
import android.app.Activity;
import android.os.Bundle;
public class DundrumSelector extends Activity { @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
int position = getIntent().getExtras().getInt("position");
if(position == 0){
setContentView(R.layout.cuisine);
}
}
}
Yet i'm still getting the same problem. The program crashes when I click on an item in the listView. And yes i've added the activity to the manifest.
Does anyone have a resolution to this as alot of people seem to be having the same problem.
Thanks alot.
Ok i got it working.
I had changed the avd back to an original avd that i had set it up with and it seems to work ok now.
For some reason it didn't work when i had it on the google avd?
You haven't included a call to super in onListItemClick
Try making the first line: super.onListItemClick(l,v,position,id);
精彩评论