Listview滚动直到结束像分页

时间:2013-01-18 10:19:48

标签: android json android-listview

经过SO和Google的大量搜索和试用后,我再次回到列表视图中无法滚动:( ...当我滚动到列表视图的末尾时,它应该在listview中加载更多项目,如分页。以下是我的代码任何人都可以帮助解决这个问题吗?答案将非常感谢。

public class MainActivity extends ListActivity implements OnScrollListener{
//implement onScrollListener for scrolling {

ArrayList<HashMap<String, String>> songsList;
ListView list;
LazyAdapter adapter;
JSONArray posts;


/*
* some private variables to implement endless scrolling
*/
private int visibleThreshold = 3;
private int previousTotal = 0;
private boolean loading = true;



// All static variables
static final String URL = "siteurl";


 static final String KEY_POSTS = "posts";
 static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_CONTENT = "content";
static final String KEY_AUTHOR = "author";
static final String KEY_NAME = "name";
static final String KEY_ATTACHMENTS = "attachments";
static final String KEY_SLUG = "slug";
static final String KEY_THUMB_URL = "thumbnail";
static final String KEY_IMAGES = "images";
static final String KEY_URL = "url";



public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {

if(loading){
//if trying to loading
if(totalItemCount > previousTotal){

loading = false;
previousTotal = totalItemCount;
}
}

if(!loading && ((totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold))){
new LoadMoreEntries().execute();
loading = true;
}
}


@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
Dialog loadingDialog = ProgressDialog.show(MainActivity.this, "", "Loading.Please wait...",false);

return loadingDialog;
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



     ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


        // Creating JSON Parser instance
                JSONParser jParser = new JSONParser();

                // getting JSON string from URL
                JSONObject json = jParser.getJSONFromUrl(URL);
                try {
                     posts = json.getJSONArray(KEY_POSTS);

        // looping through all song nodes <song>
                for(int i = 0; i < posts.length(); i++){
                    JSONObject c = posts.getJSONObject(i);
                    // Storing each json item in variable
                    String id = c.getString(KEY_ID);
                    String title = c.getString(KEY_TITLE);
                    String date = c.getString(KEY_DATE);
                    String content = c.getString(KEY_CONTENT);
                    // to remove all <P> </p> and <br /> and replace with ""
                     content = content.replace("<br />", "");
                     content = content.replace("<p>", "");
                     content = content.replace("</p>", "");

                    //authornumber is agin  JSON Object
                    JSONObject author = c.getJSONObject(KEY_AUTHOR);
                    String name = author.getString(KEY_NAME);

                    String url = null;
                    String slug = null;
                    try {
                    JSONArray atta = c.getJSONArray("attachments");
                    for(int j = 0; j < atta.length(); j++){
                        JSONObject d = atta.getJSONObject(j);

                        slug = d.getString(KEY_SLUG);

                        JSONObject images = d.getJSONObject(KEY_IMAGES);

                        JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
                        url = thumbnail.getString(KEY_URL);

                    }
                    } catch (Exception e) {
                        e.printStackTrace();

                    }




            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(KEY_ID, id);
            map.put(KEY_TITLE, title);
            map.put(KEY_DATE, date);
            map.put(KEY_NAME, name);
            map.put(KEY_CONTENT, content);
            map.put(KEY_SLUG, slug);
            map.put(KEY_URL, url);


            // adding HashList to ArrayList
            songsList.add(map);
                }   
                }catch (JSONException e) {
                    e.printStackTrace();

                    }



         final ListView  list=(ListView)findViewById(android.R.id.list);

         // Getting adapter by passing json data ArrayList
            adapter=new LazyAdapter(this, songsList);    
             list.setAdapter(adapter);



    // Launching new screen on Selecting Single ListItem
    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
            String date = ((TextView) view.findViewById(R.id.date)).getText().toString();
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String content = ((TextView) view.findViewById(R.id.content)).getText().toString();



            // Starting new intent
            Intent in = new Intent(getApplicationContext(),SampleDesp.class);
            in.putExtra(KEY_TITLE, title);
            in.putExtra(KEY_DATE, date);
            in.putExtra(KEY_NAME, name);
            in.putExtra(KEY_CONTENT, content);

            startActivity(in);

        }
    });


}
class LoadMoreEntries extends AsyncTask<String, Void, String>{

                        @Override
                        protected void onPreExecute() {
                        // show your dialog
                        super.onPreExecute();
                        showDialog(1);
                        }

                        protected String doInBackground(ArrayList... params) {
                        // do your request here

                         ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


                            // Creating JSON Parser instance
                                    JSONParser jParser = new JSONParser();

                                    // getting JSON string from URL
                                    JSONObject json = jParser.getJSONFromUrl(URL);
                                    try {
                                         posts = json.getJSONArray(KEY_POSTS);

                            // looping through all song nodes <song>
                                    for(int i = 0; i < posts.length(); i++){
                                        JSONObject c = posts.getJSONObject(i);
                                        // Storing each json item in variable
                                        String id = c.getString(KEY_ID);
                                        String title = c.getString(KEY_TITLE);
                                        String date = c.getString(KEY_DATE);
                                        String content = c.getString(KEY_CONTENT);
                                        // to remove all <P> </p> and <br /> and replace with ""
                                         content = content.replace("<br />", "");
                                         content = content.replace("<p>", "");
                                         content = content.replace("</p>", "");

                                        //authornumber is agin  JSON Object
                                        JSONObject author = c.getJSONObject(KEY_AUTHOR);
                                        String name = author.getString(KEY_NAME);

                                        String url = null;
                                        String slug = null;
                                        try {
                                        JSONArray atta = c.getJSONArray("attachments");
                                        for(int j = 0; j < atta.length(); j++){
                                            JSONObject d = atta.getJSONObject(j);

                                            slug = d.getString(KEY_SLUG);

                                            JSONObject images = d.getJSONObject(KEY_IMAGES);

                                            JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
                                            url = thumbnail.getString(KEY_URL);

                                        }
                                        } catch (Exception e) {
                                            e.printStackTrace();

                                        }




                                // creating new HashMap
                                HashMap<String, String> map = new HashMap<String, String>();

                                // adding each child node to HashMap key => value
                                map.put(KEY_ID, id);
                                map.put(KEY_TITLE, title);
                                map.put(KEY_DATE, date);
                                map.put(KEY_NAME, name);
                                map.put(KEY_CONTENT, content);
                                map.put(KEY_SLUG, slug);
                                map.put(KEY_URL, url);


                                // adding HashList to ArrayList
                                songsList.add(map);
                                    }   
                                    }catch (JSONException e) {
                                        e.printStackTrace();

                                        }



                        return null;
                        }

                        protected void onPostExecute(String result) {
                        // close the dialog and notify adapter for data
                        super.onPostExecute(result);
                        Dialog loadingDialog = null;
                        loadingDialog.dismiss();
                        adapter.notifyDataSetChanged();
                        //dismiss the progress dialog
                        }



                        @Override
                        protected String doInBackground(String... params) {
                            // TODO Auto-generated method stub
                            return null;
                        }
                        }
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // TODO Auto-generated method stub

}
   }

0 个答案:

没有答案