以下代码从webservice获取字符串vectorList并将其分配给另一个arraylist,以便在放置在片段中的android listView中显示内容。此代码也会调用相同的Web服务,每10秒刷新一次列表内容。
public class MyListFragment extends Fragment implements IWsdl2CodeEvents {
// ServicesSource includes all required methods for call a webserivce
ServicesSource service = new ServicesSource(MyListFragment.this);
private Timer refreshTimer;
private String[] values;
// I receive a vectorList from my webserivce
private Vector<String> userDataList;
private StableArrayAdapter adapter;
ArrayList<String> list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = (LinearLayout) inflater.inflate(
R.layout.tab_my_lists_fragment, container, false);
final ListView listview = (ListView) rootView
.findViewById(R.id.listview_id);
values = new String[]{};
list = new ArrayList<String>();
for (int i = 0; i < values.length; i++) {
list.add(values[i]);
}
adapter = new StableArrayAdapter(
getActivity(), android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@SuppressLint("NewApi")
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
Toast.makeText(
getActivity(),
"selected item is: "
+ parent.getItemAtPosition(position),
Toast.LENGTH_SHORT).show();
}
});
updateMyListView();
return rootView;
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
private void updateMyMeetingsListView(){
refreshTimer = new Timer();
refreshTimer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
service.myWebService();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 0, 10000);
}
private void refreshAdapterContent(Vector<String> udList){
userDataList = udList;
list.clear();
for (int i=0 ; i<userDataList.size() ; i++){
list.add(userDataList.get(i).name);
}
adapter.notifyDataSetChanged();
}
@Override
public void Wsdl2CodeStartedRequest() {
// TODO Auto-generated method stub
}
private String WSTAGs = "VBSwebServicesCV";
@Override
public void Wsdl2CodeFinished(String methodName, Object Data) {
// TODO Auto-generated method stub
switch (Data) {
case 0:
//list content received from myWebService
refreshAdapterContent(Data.list);
break;
case 1:
//nothing returned from ws
break;
}
}
@Override
public void Wsdl2CodeFinishedWithException(Exception ex, String methodName) {
// TODO Auto-generated method stub
}
@Override
public void Wsdl2CodeEndedRequest() {
// TODO Auto-generated method stub
}
}
当我运行这段代码时,我抓住了NullPointerException
adapter.notifyDataSetChanged()
行。请帮助我解决我的问题。