0

I can't seem to figure out what is going wrong with this, the code is pretty clean/ simple, but for some reason when I pull to refresh, it just shows me the loading icon infinity. Not only that, but I don't even receive any errors. The library I used for pull to refresh is this one.

I've spent several hours trying to figure this out, but the fact that it's not even giving me errors is making it very hard for me to track down.

And yes, I am sure the rest request is working.

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public final class PullToRefresh extends Activity {

    static final int MENU_SET_MODE = 0;
    static final String KEY_FEED = "feed"; // parent node
    static final String KEY_UID_FK = "uid_fk";
    static final String KEY_FIRST_NAME = "first_name";
    static final String KEY_LAST_NAME = "last_name";
    public static final String KEY_NAME = "name";
    static final String KEY_MESSAGE = "message";
    static final String KEY_CREATED = "created";
    static final String KEY_THUMB_URL = "thumb_img";
    static final String KEY_DATA = "data";
    static final String KEY_HOMETOWN = "hometown";
    static final String KEY_BIO = "bio";

    private ArrayList<HashMap<String, String>> feedList = new ArrayList<HashMap<String, String>>();
    private PullToRefreshGridView mPullRefreshGridView;
    private GridView mGridView;
    private GridAdapter adapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptr_grid);

        mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
        mGridView = mPullRefreshGridView.getRefreshableView();

        // Set a listener to be invoked when the list should be refreshed.
        mPullRefreshGridView
                .setOnRefreshListener(new OnRefreshListener2<GridView>() {

                    @Override
                    public void onPullDownToRefresh(
                            PullToRefreshBase<GridView> refreshView) {
                        Toast.makeText(PullToRefresh.this, "Pull Down!",
                                Toast.LENGTH_SHORT).show();
                        try {
                            new RestClientUsage().getPublicTimeline();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onPullUpToRefresh(
                            PullToRefreshBase<GridView> refreshView) {
                        Toast.makeText(PullToRefresh.this, "Pull Up!",
                                Toast.LENGTH_SHORT).show();
                        try {
                            new RestClientUsage().getPublicTimeline();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                });

        ArrayList<HashMap<String, String>> feedList = new ArrayList<HashMap<String, String>>();
        TextView tv = new TextView(this);
        tv.setGravity(Gravity.CENTER);
        tv.setText("Empty View, Pull Down/Up to Add Items");
        mPullRefreshGridView.setEmptyView(tv);
        adapter = new GridAdapter(PullToRefresh.this, feedList);
        mGridView.setAdapter(adapter);
    }

    class RestClientUsage {

        public void getPublicTimeline() throws JSONException {
            RequestParams params = new RequestParams();
            params.put("loggedin_uid", TabHostFragmentActivity.loggedin_uid);

            RestClient.post("http://localhost/basic/rest/request/format/json",
                    params, new JsonHttpResponseHandler() {

                        private JSONArray feed;

                        @Override
                        public void onSuccess(JSONObject json) {
                            try {
                                Log.i("JSON->TRAILS RESPONSE", json.toString(3));

                                // Getting Feed Array
                                feed = json.getJSONArray(KEY_FEED);

                                /*
                                * Log & Debug Code String feed_log =
                                * feed.toString(3); int feed_length =
                                * feed.length(); String string_length =
                                * Integer.toString(feed_length);
                                * Log.i("JSON PROFILE FEED RESPONSE", feed_log
                                * ); Log.i("JSON PROFILE FEED LENGTH",
                                * string_length);
                                */

                                // looping through Feed of Updates
                                for (int i = 0; i < feed.length(); i++) {
                                    JSONObject c = feed.getJSONObject(i);

                                    String jFeedObj = c.toString(3);
                                    Log.i("JSON FEED OBJ", jFeedObj);

                                    // Storing each json item in variable
                                    String uid = c.getString(KEY_UID_FK);
                                    String first_name = c
                                            .getString(KEY_FIRST_NAME);
                                    String last_name = c
                                            .getString(KEY_LAST_NAME);
                                    String name = first_name + ' ' + last_name;
                                    String http = "http://10.0.2.2/CI_REST_LOGIN/UPLOADS/thumbs/";
                                    String base_url = c
                                            .getString(KEY_THUMB_URL);
                                    String thumb_url = http + base_url;
                                    String message = c.getString(KEY_MESSAGE);
                                    String created = c.getString(KEY_CREATED);

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

                                    // adding each child node to HashMap key =>
                                    // value
                                    map.put(KEY_UID_FK, uid);
                                    map.put(KEY_NAME, name);
                                    map.put(KEY_MESSAGE, message);
                                    map.put(KEY_CREATED, created);
                                    map.put(KEY_THUMB_URL, thumb_url);

                                    Log.i("Trails - line 130", "Success");
                                    // adding HashList to ArrayList
                                    feedList.add(map);
                                }

                                adapter.notifyDataSetChanged();
                                // Call onRefreshComplete when the list has been
                                // refreshed.
                                mPullRefreshGridView.onRefreshComplete();

                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    });
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(
                0,
                MENU_SET_MODE,
                0,
                mPullRefreshGridView.getMode() == Mode.BOTH ? "Change to MODE_PULL_DOWN"
                        : "Change to MODE_PULL_BOTH");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem setModeItem = menu.findItem(MENU_SET_MODE);
        setModeItem
                .setTitle(mPullRefreshGridView.getMode() == Mode.BOTH ? "Change to MODE_PULL_FROM_START"
                        : "Change to MODE_PULL_BOTH");

        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_SET_MODE:
            mPullRefreshGridView
                    .setMode(mPullRefreshGridView.getMode() == Mode.BOTH ? Mode.PULL_FROM_START
                            : Mode.BOTH);
            break;
        }

        return super.onOptionsItemSelected(item);
    }
}

And here is the code for my GridView adapter:

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.image.SmartImageView;

public class GridAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader;

    public GridAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data = d;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.mobile, null);

        TextView name = (TextView) vi.findViewById(R.id.grid_item_label); // title
        SmartImageView thumb_image = (SmartImageView) vi
                .findViewById(R.id.grid_item_image);

        HashMap<String, String> update = new HashMap<String, String>();
        update = data.get(position);

        // Setting all values in listview
        name.setText(update.get("name"));
        thumb_image.setImageUrl(update.get("thumb_img"));
        name.setOnClickListener(new myOnClickListener(position));
        thumb_image.setOnClickListener(new myOnClickListener(position));
        return vi;
    }

    public class myOnClickListener implements OnClickListener {

        private int position;
        private String clicked_uid;

        public myOnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) { // TODO Auto-generated method stub
            HashMap<String, String> update = new HashMap<String, String>();
            update = data.get(position);
            Log.i("Update Position:", update.toString());
            clicked_uid = update.get("uid");
            Log.d("Clicked UID:", clicked_uid + "");
            Intent i = new Intent(activity.getApplicationContext(),
                    TabHostFragmentActivity.class);
            i.putExtra("profile_uid", clicked_uid);
            activity.startActivity(i);
        }
    }
}
ChuckKelly
  • 1,742
  • 5
  • 25
  • 54

1 Answers1

0

You should POST to 10.0.2.2 instead of localhost in the call to RestClient.post in RestClientUsage.

I am not familiar withJsonHttpResponseHandler, but I would image there's an onFailure method (or something similar) that you may want to override. If there is, you may way to call mPullRefreshGridView.onRefreshComplete() in it.

Mike
  • 2,422
  • 23
  • 14
  • Wow....Wow, i feel so dumb.Thank you very much for pointing that error out to me.the RestClient uses : https://github.com/loopj/android-async-http and ive edited the request to hide my actual api calls, but you're right i did forget the localhost/ 10.0.2.2 thing :X . It still doesnt work as is unless i add " adapter = new GridAdapter(PullToRefresh.this, feedList); mGridView.setAdapter(adapter); mPullRefreshGridView.onRefreshComplete(); " is there a way i can clear the old adapter each time a user pulls down OR rig it to add only new list items? – ChuckKelly Mar 12 '13 at 05:13
  • If you add some sort of update method to your GridAdapter, you should just need to call [notifyDataSetChanged](http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()) after the update to get the GridView to update. – Mike Mar 13 '13 at 04:33