Guernica
Guernica

Reputation: 246

Android, java listview notifyDataSetChanged(); not working

Hi could someone have a look at this and see if they know where i've went wrong please. It works fine until the post execute task in GetTheMoreData() im trying to append the entries from both GetTheMoreData and GetTheData into a listview. when i click the load more button i get no errors and the listview remains the same. Postexecute does fire as i can see the data in the log. thanks

SimpleAdapter mAdapter;
String mCurFilter;
List<Map<String, String>> items = new ArrayList<Map<String, String>>();

@Override 
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    String  letter = getActivity().getIntent().getStringExtra("LETTER");
    new GetTheData().execute(letter);

    final ListView lv = getListView();
    Button btnLoadMore = new Button(getActivity());
    btnLoadMore.setText("Load More");
    lv.addFooterView(btnLoadMore); 
    lv.setTextFilterEnabled(true);         
    btnLoadMore.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            new GetTheMoreData().execute("A");
            Log.i(TAG, "loadmore clicked : ");
        }
    });

    lv.setOnItemClickListener(new OnItemClickListener() {       
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //room for stuff             
        }   
    });
    setListShown(false);
    getLoaderManager().initLoader(0, null, this);
}

private class GetTheMoreData extends AsyncTask<String, Void, List<Map<String, String>>> {   
    @Override
    protected List<Map<String, String>> doInBackground(String... res) {    
        List<Map<String, String>> items = new ArrayList<Map<String, String>>();  
        start = start +25;  
        JSONObject json = JSONfunctions.getJSONfromURL("http://www.myurl.com/main.php?letter="+res[0]+"&start="+start);        
        try{
            JSONArray  songlist = json.getJSONArray("mainlist");                
            for(int i=0;i<songlist.length();i++){                       
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = songlist.getJSONObject(i);           
                map.put("id",  String.valueOf(i));
                map.put("Item", e.getString("Item"));               
                items.add(map); 
                Log.i(TAG, "dat:" + items);
            }       
        }catch(JSONException e)        {
            Log.e("log_tag", "Error parsing data "+e.toString());
        }        
        return items;
    }

    @Override
    protected void onPostExecute(List<Map<String, String>> items) { 

        mAdapter.notifyDataSetChanged();
        super.onPostExecute(items);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }
}

private class GetTheData extends AsyncTask<String, Void, List<Map<String, String>>> {   
    @Override
    protected List<Map<String, String>> doInBackground(String... res) {         
        JSONObject json = JSONfunctions.getJSONfromURL("http://www.myurl.com/main.php?letter="+res[0]+"&start=0");        
        try{
            JSONArray  songlist = json.getJSONArray("mainlist");

            for(int i=0;i<songlist.length();i++){                       
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = songlist.getJSONObject(i);           
                map.put("id",  String.valueOf(i));
                map.put("Item", e.getString("Item"));
                items.add(map);         
            }       
        }catch(JSONException e)        {
            Log.e("log_tag", "Error parsing data "+e.toString());
        }        
        return items;
    }

    @Override
    protected void onPostExecute(List<Map<String, String>> items) { 
        String[] from = new String[] { "Item" };
        int[] to = new int[] { android.R.id.text1 };

        mAdapter = new SimpleAdapter( getActivity(), items,
                android.R.layout.simple_list_item_1, from, to );
        setListAdapter( mAdapter );
        super.onPostExecute(items);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}

@Override 
public void onListItemClick(ListView l, View v, int position, long id) {
    switch( position )
    {}

    Log.i(TAG, "Item clicked: " + id);
}

// These are the rows that we will retrieve.
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // mAdapter.swapCursor(data);

    // The list should now be shown.
    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}

public void onLoaderReset(Loader<Cursor> loader) {
    //    mAdapter.swapCursor(null);
}

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
    // TODO Auto-generated method stub
    return null;
}

Upvotes: 0

Views: 3263

Answers (1)

yDelouis
yDelouis

Reputation: 2094

It's because in the method doInBackground() of GetTheMoreData, you do :
List<Map<String, String>> items = new ArrayList<Map<String, String>>();
So a new list is created and you're adding element to a list which exists only in this method and which is not linked to your adapter.
Instead of this line, put this : items.clear();

Upvotes: 3

Related Questions