无尽的滚动列表视图

时间:2013-01-17 05:08:59

标签: android json

我是Android新手,我正在使用ListView,它目前从webservice加载数据。 ListView正在显示10个项目,当我向下滚动它应该再加载10个项目,如分页。我使用以下代码但是当我向下滚动时它没有加载更多项目。谁能帮我?任何答案或建议将不胜感激。

public class MainActivity extends ListActivity {

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



// FLAG FOR CURRENT PAGE
int current_page = 1;

// BOOLEAN TO CHECK IF NEW FEEDS ARE LOADING
Boolean loadingMore = true;

Boolean stopLoadingData = false;

// All static variables
static final String URL = "website/json";


 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";




@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 {
                    JSONArray 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();

                    }


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



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

        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);

        }
    });









// ONSCROLLLISTENER
list.setOnScrollListener(new OnScrollListener() {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        int lastInScreen = firstVisibleItem + visibleItemCount;
        if ((lastInScreen == totalItemCount) && !(loadingMore)) {

            if (stopLoadingData == false) {
                // FETCH THE NEXT BATCH OF FEEDS
                new loadMorePhotos().execute();
            }

        }
    }
});
}


private class loadMorePhotos extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {

        // SET LOADING MORE "TRUE"
        loadingMore = true;

        // INCREMENT CURRENT PAGE
        current_page += 1;


         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 {
                        JSONArray 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;
    }
    @Override
    protected void onPostExecute(Void result) {

        // get listview current position - used to maintain scroll position
        int currentPosition = list.getFirstVisiblePosition();

        // APPEND NEW DATA TO THE ARRAYLIST AND SET THE ADAPTER TO THE
        // LISTVIEW

        list.setAdapter(adapter);

        // Setting new scroll position
      list.setSelection(currentPosition + 1);

        // SET LOADINGMORE "FALSE" AFTER ADDING NEW FEEDS TO THE EXISTING
        // LIST
        loadingMore = false;
    }

}

  }

0 个答案:

没有答案