将数据从数据库填充到OnScrollListener上的ListView不起作用

时间:2012-01-16 00:55:11

标签: android database json listview

public class BabyNamesActivity extends ListActivity {

    public ArrayList<HashMap<String, String>> List1, List2, List3;
    public ListView lv;
    public BabyNamesActivity bnaInstance = null;

        this.getListView().setOnScrollListener(new OnScrollListener(){

            //useless here, skip!
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            //dumdumdum         
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount)  {


                if(firstVisibleItem + visibleItemCount == totalItemCount)
                {

        bnaInstance.fillListNew(10);        //NOT WORKING HERE
                }

    }
    });


        fillList(1);                    // WORKING HERE
        fillListNew(10);                // WORKING HERE


}


public void fillList (int RecordNo)
    {

        List1=myFun(RecordNo);

        ListAdapter adapter = new SimpleAdapter(this, List1 , R.layout.main, 
               new String[] { "name", "meaning", "gender" }, 
               new int[] { R.id.item_babyname, R.id.item_meaning, R.id.item_gender });

       lv = getListView();       
       lv.setAdapter(adapter);      
};



public  ArrayList<HashMap<String, String>> myFun (int Start)
                        {
            ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();        
            JSONObject json = JSONfunctions.getJSONfromURL("http://localhost/Andriod/babynames.php".concat("?start=").concat(Integer.toString(Start)));

            try{

                JSONArray  babynames = json.getJSONArray("babynames");

                for(int i=0;i<babynames.length();i++){                      
                    HashMap<String, String> map = new HashMap<String, String>();    
                    JSONObject e = babynames.getJSONObject(i);

                    map.put("id",  String.valueOf(i));
                    map.put("name", "" + e.getString("babyName"));
                    map.put("meaning",  e.getString("meaning"));
                    map.put("gender",  e.getString("gender"));

                    mylist.add(map);            
                }       
            }catch(JSONException e)        {

                Toast.makeText(BabyNamesActivity.this, e.toString() , Toast.LENGTH_LONG).show();
                Log.e("log_tag", "Error parsing data "+e.toString());

            }

            return mylist;  
};  

1 个答案:

答案 0 :(得分:0)

它在实现AsyncTask之后起作用。