使用JSON Adapter& amp;无限滚动列表显示

时间:2015-09-04 19:09:15

标签: android json android-listview

我使用ListView通过JSON从外部URL检索数据。 当我运行我的应用程序时,它只显示最多10个结果,因为我的api查询返回每页最多10个结果。

我想要做的是,当用户向下滚动到这10个结果的末尾时,它将从我的JSON外部URL(API)加载更多结果(逐个更大)

这就是我使用ListView& amp; JSONAdapter:

TabFragment1.java

ListView mainListView = (ListView) v.findViewById(R.id.main_listview);
        mJSONAdapter = new JSONAdapter(getActivity(), inflater);
        mainListView.setAdapter(mJSONAdapter);
getUpdates();

getUpdates()

private void queryUpdates(double lat, double lon, int distance) {

        // Create a client to perform networking
        AsyncHttpClient client = new AsyncHttpClient();

        // Show ProgressBar to inform user that a task in the background is occurring
        mProgress.setVisibility(View.VISIBLE);

        // Have the client get a JSONArray of data
        // and define how to respond
        client.get(API_URL + "?lat=" + lat + "&lon=" + lon + "&distance=" + distance,
                new JsonHttpResponseHandler() {

                    @Override
                    public void onSuccess(JSONObject jsonObject) {

                        // 11. Dismiss the ProgressDialog
                        mProgress.setVisibility(View.GONE);

                        // update the data in your custom method.
                        mJSONAdapter.updateData(jsonObject.optJSONArray("docs"));
                    }

                    @Override
                    public void onFailure(int statusCode, Throwable throwable, JSONObject error) {

                        // 11. Dismiss the ProgressDialog
                        mProgress.setVisibility(View.GONE);

                        getActivity().setContentView(R.layout.bad_connection);
                    }
                });
    }

我的JSONAdapter.java

public class JSONAdapter extends BaseAdapter implements StickyListHeadersAdapter {

    Context mContext;
    LayoutInflater mInflater;
    JSONArray mJsonArray;

    public JSONAdapter(Context context, LayoutInflater inflater) {
        mContext = context;
        mInflater = inflater;
        mJsonArray = new JSONArray();
    }

    @Override
    public int getCount() {
        return mJsonArray.length();
    }

    @Override
    public Object getItem(int position) {
        return mJsonArray.optJSONObject(position);
    }

    @Override
    public long getItemId(int position) {
        // your particular dataset uses String IDs
        // but you have to put something in this method
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        // check if the view already exists
        // if so, no need to inflate and findViewById again!
        if (convertView == null) {

            // Inflate the custom row layout from your XML.
            convertView = mInflater.inflate(R.layout.row_place_update, null);

            // create a new "Holder" with subviews
            holder = new ViewHolder();
            holder.myImageView = (ImageView) convertView.findViewById(R.id.myImage);
            holder.myTextView = (TextView) convertView.findViewById(R.id.myText);

            // hang onto this holder for future recyclage
            convertView.setTag(holder);
        } else {

            // skip all the expensive inflation/findViewById
            // and just get the holder you already made
            holder = (ViewHolder) convertView.getTag();
        }
        // More code after this
        // Get the current book's data in JSON form
        JSONObject jsonObject = (JSONObject) getItem(position);

        // Grab the title and author from the JSON
        if (jsonObject != null) {
            //Get from JSON
            String myImage = jsonObject.optString("myImage");
            String myText = jsonObject.optString("myText");

            //Replace Them
            Picasso.with(mContext).load(myImage).placeholder(R.drawable.loading).into(holder.myImageView);
            holder.myTextView.setText(myText);

            }

        return convertView;
    }


    // this is used so you only ever have to do
    // inflation and finding by ID once ever per View
    private static class ViewHolder {
        public ImageView myImageView;
        public TextView myTextView;
    }

    public void updateData(JSONArray jsonArray) {
        // update the adapter's dataset
        mJsonArray = jsonArray;
        notifyDataSetChanged();
    }

    }

如何通过向API_URL添加& page = 2,& page = 3并将这些结果加载到我的ListVIew中以及旧结果来显示更多结果?

0 个答案:

没有答案