我似乎无法弄清楚这有什么问题,代码非常干净/简单,但出于某种原因,当我拉动刷新时,它只显示加载图标无穷大。不仅如此,我甚至没有收到任何错误。我用来刷新的库是this one。
我花了几个小时试图解决这个问题,但事实上它甚至没有给我错误,这让我很难追查。
是的,我确信其他请求正在运作。
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);
}
}
这是我的GridView
适配器的代码:
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);
}
}
}
答案 0 :(得分:0)
您应该在RestClient.post
中对RestClientUsage
的调用中POST到10.0.2.2而不是localhost。
我不熟悉JsonHttpResponseHandler
,但我会想象你可能想要覆盖的onFailure
方法(或类似的东西)。如果有,您可以在其中拨打mPullRefreshGridView.onRefreshComplete()
。