Reputation: 804
I have a custom ListView with list item layout as shown
this data is fetched from web service and set to ListView through BaseAdapter, here I provide code of getView() only
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.custom_list_item, null);
holder = new ViewHolder();
holder.desc = (TextView) vi.findViewById(R.id.desc);
holder.day=(TextView)vi.findViewById(R.id.day);
holder.high=(TextView)vi.findViewById(R.id.high);
holder.low=(ImageView)vi.findViewById(R.id.low);
vi.setTag( holder );
}
else
holder=(ViewHolder)vi.getTag();
if(arrayList.size()<=0)
{
holder.desc.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = ( Weather ) arrayList.get( position );
/************ Set Model values in Holder elements ***********/
holder.desc.setText(tempValues.getDesc());
holder.day.setText("$ "+tempValues.getDay());
holder.high.setText(tempValues.getHigh());
holder.low.setText(tempValues.getLow());
imageLoader.displayImage(arrayList.get(position).getThumbnail(), holder.image,
options, animationListener);
}
return vi;
}
Now the tricky part comes, I want to get Value of desc TextView of custom list item, I tried this way
private AdapterView.OnItemClickListener onItemClickListener(){
AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Weather weather= (Weather) weatherListView.getSelectedItem();
Intent intent = new Intent(ListActivity.this, Detail.class);
intent.putExtra(Detail.WEATHER_DESC, weather.getDesc());
startActivity(intent);
}
};
return onItemClickListener;
}
but at line
intent.putExtra(Detail.WEATHER_DESC, weather.getDesc());
I get error as
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: FATAL EXCEPTION: main
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: java.lang.NullPointerException
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at com.example.weatherservice.ListActivity.onItemClick(ListActivity.java:179)
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at android.widget.AdapterView.performItemClick(AdapterView.java:292)
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at android.widget.AbsListView$1.run(AbsListView.java:3168)
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:605)
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4424)
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-24 08:36:10.343 1460-1460/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
I have searched SO but no answer satisfies my requirement.
Upvotes: 0
Views: 468
Reputation: 8190
I think the problem come from your approach. I suggest you do it another way:
//create getWeather function in your adapter
public Weather getWeather(int position){
// do something like: return arryList.get(position)
}
// In your activity, set OnItemCLickListener for your listview:
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Weather weather = yourAdapter.getWeather(i);//use your adapter to get weather
}
});
Upvotes: 0
Reputation: 1062
You have an ArrayList
already and onItemClick
of the ListView
you are getting the position of the the item clicked, so you should do it like this.
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Weather weather= (Weather) arrayList.get(i); // i is the position of item clicked.
Intent intent = new Intent(ListActivity.this, Detail.class);
intent.putExtra(Detail.WEATHER_DESC, weather.getDesc());
startActivity(intent);
}
};
Upvotes: 0
Reputation: 5487
private AdapterView.OnItemClickListener onItemClickListener(){
AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//Weather weather= (Weather) weatherListView.getSelectedItem(); // instead of this
Intent intent = new Intent(ListActivity.this, Detail.class);
Weather weather= (Weather) adapterView.getItem(i); // use this
intent.putExtra(Detail.WEATHER_DESC, weather.getDesc());
startActivity(intent);
}
};
return onItemClickListener;
}
Upvotes: 2