0

嗨,有人可以看看这个,看看他们是否知道我哪里出错了。它工作正常,直到 GetTheMoreData() 中的后执行任务我试图将 GetTheMoreData 和 GetTheData 中的条目附加到列表视图中。当我单击加载更多按钮时,我没有收到任何错误,并且列表视图保持不变。Postexecute 确实会触发,因为我可以在日志中看到数据。谢谢

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;
}
4

1 回答 1

3

这是因为在 的方法doInBackground()GetTheMoreData,您这样做:
List<Map<String, String>> items = new ArrayList<Map<String, String>>();
因此创建了一个新列表,并且您将元素添加到仅存在于此方法中且未链接到您的适配器的列表中。
而不是这一行,把这个: items.clear();

于 2012-11-12T19:09:57.513 回答