0

The following code gets a string vectorList from a webservice and assign it to another arraylist in order to show the content in a android listView that placed in a fragment. Also this code invoke the same webservice, to refresh the content of the list every 10sec.

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

}

}

When I run this piece of code, I catch a NullPointerException adapter.notifyDataSetChanged() line. please help me in this case to solve my problem.

mohammad
  • 90
  • 2
  • 12
  • I don't see a null check at all, may be you are doing it another program, can you check **Data.list** for null? – Dickens A S Jun 27 '15 at 07:31
  • I toggled a break point at where you mentioned, but when i debugged it received data and also passed data correctly to `refreshAdapterContent()` method. – mohammad Jun 27 '15 at 07:34
  • try to check null for userDataList.get(i).name and you are trying to call adapter.notifyDataSetChanged() without adding item to the **adapter** – Dickens A S Jun 27 '15 at 07:38
  • I checked `userDataList.get(i).name too, and it works correct, am I implement the logic of code for updating the content of adapter correct? – mohammad Jun 27 '15 at 07:44
  • I think you are trying to call **refreshAdapterContent()** before originally the component is rendered – Dickens A S Jun 27 '15 at 07:44
  • But I wait for response of the webserivce first, and once I got the response at `Wsdl2CodeFinished(String methodName, Object Data)` I call `refreshAdapterContent()` method, and logically it would not be empty. – mohammad Jun 27 '15 at 07:49
  • Change the loop as **adapter.addItem(userDataList.get(i).name)** – Dickens A S Jun 27 '15 at 07:53
  • I just changed the code of refreshAdapterContent() body to following lines: `adapter.clear(); for (int i=0 ; i – mohammad Jun 27 '15 at 08:35

0 Answers0