I have a ListView which contains some texts and pic for each item (it’s the row actually). When I click on one of them, it triggers a listener and open a dialog.
The content shown in Dialog is retrieved from the item in ListView. I tried several ways but the parameters passed in are all null pointer.
Say, I’m trying to get the value of hotelName which is in the layout of listview. The id of that TextView is R.id.nameTV
final Context context = this;
public void onCreate(Bundle blabla){
final ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Dialog dialog = new Dialog(context);
// this is the TextView in Dialog to hold the content from ListView
TextView dHotelTV = (TextView)findViewById(R.id.hotelNameTV);
CharSequence hotelName;
/*
The way I tried:
hotelName = context.getText(R.id.nameTV);
hotelName = view.findViewById(R.id.nameTV);
*/
dialog.setTitle("EasyTrip");
dialog.setContentView(R.layout.dialog);
dHotelTV.setText(hotelName)
}
}
The hotelName from these methods are all null pointer. It drives me crazy.
What am I doing wrong?
Pull it from the textView:
Also, if dHotelTV is an element in the dialog, you’re not going to find it using
findViewByIdin the activity, and you’ll get another NullPointerException:Note that
findViewById()is scoped – It only looks for children. So if you have a listView where each Item is defined by this layout:then if you call
findViewById()on that item:The call is scoped to the item you clicked on.