I am trying to update the ListView when the Data is update in the Android.
I use mLeDeviceListAdapter.addRSSIandAddress("20");
to set the value to addRSSIandAddress
function.
And in the addRSSIandAddress
, it receive the value and call mLeDeviceListAdapter.notifyDataSetChanged();
. But the NullPointerException
happened at the viewHolder.deviceRssi.setText("RSSI:" + String.valueOf(mrssi));
It seems the viewHolder.deviceRssi
is null or the viewHolder
is null .
The code of ListView
and the Adapter
is like the following:
private LeDeviceListAdapter mLeDeviceListAdapter;
private static ListView device_list;
mLeDeviceListAdapter = new LeDeviceListAdapter(getActivity());
device_list.setAdapter(mLeDeviceListAdapter);
public class LeDeviceListAdapter extends BaseAdapter{
private LayoutInflater mInflator;
ArrayList<BluetoothDevice> mLeDevices;
private String mrssi;
public LeDeviceListAdapter(Context context) {
// TODO Auto-generated constructor stub
mInflator = LayoutInflater.from(context);
}
private void addRSSIandAddress(String rssi) {
// TODO Auto-generated method stub
mrssi = rssi;
mLeDeviceListAdapter.notifyDataSetChanged();
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
Log.d(TAG, "getView");
if(view == null){
view = mInflator.inflate(R.layout.device_list, null);
viewHolder = new ViewHolder();
viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
}else {
viewHolder = (ViewHolder) view.getTag();
viewHolder.deviceRssi.setText("RSSI:" + String.valueOf(mrssi));
}
return view;
}
}
static class ViewHolder {
TextView deviceRssi;
}
How to solve this problem ? Thanks in advance.
As the others have said. I think you forgot to set
view.setTag(viewHolder).
Try Put this line here In your code
if(view == null)
{
view = mInflator.inflate(R.layout.device_list, null);
viewHolder = new ViewHolder();
viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
view.setTag(viewHolder); //THE CODE
}
else
{
viewHolder = (ViewHolder) view.getTag();
viewHolder.deviceRssi.setText("RSSI:" + String.valueOf(mrssi));
}
return view;
You use:
viewHolder = (ViewHolder) view.getTag();
But do you setTag
somewhere? I don't think so.
Hope it helps
Try with below code:
if(view == null){
view = mInflator.inflate(R.layout.device_list, null);
viewHolder = new ViewHolder();
viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
view.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.deviceRssi.setText("RSSI:" + String.valueOf(mrssi));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.