我正在尝试从从异步任务收到的 JSON 响应填充网格视图。下面是活动文件
public class OpenTableActivity extends Activity implements AsyncResponse{
String serUri, method;
OpenTableAdapter op;
WebServiceAsyncTask webTask = new WebServiceAsyncTask(this);
ArrayList<HashMap<String, String>> tableList = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.open_table);
getTables();
webTask.listener = this;
GridView gridview = (GridView) findViewById(R.id.grid);
gridview.setAdapter(new OpenTableAdapter(this,tableList));
LinearLayout layout = (LinearLayout) findViewById(R.id.opentableLinear);
layout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent ev) {
hideKeyboard(view);
return false;
}
});
Button btn = (Button) findViewById(R.id.table_home_btn);
btn.setOnClickListener(homeBtn);
}
public boolean getTables() {
serUri = "tables.json";
method = "get";
WebServiceAsyncTask webServiceTask = new WebServiceAsyncTask(OpenTableActivity.this);
webServiceTask.execute(serUri, method, this);
return true;
}
@Override
public void WriteJsonArray(JSONArray result) {
// TODO Auto-generated method stub
try {
for (int i = 0; i < result.length(); i++) {
JSONObject c = result.getJSONObject(i);
String tabLabel = c.getString("tablabel");
String tabStatus = c.getString("tabstatus");
HashMap<String, String> map = new HashMap<String, String>();
map.put("table_name", tabLabel);
map.put("table_status", tabStatus);
tableList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}}
填充网格视图的适配器类如下
public class OpenTableAdapter extends BaseAdapter {
private Context mContext;
String orderNumber;
OpenTableActivity openInstance;
String tableLabel;
String tableStatus;
LayoutInflater mInflater;
ArrayList<HashMap<String, String>> tablist = null;
public OpenTableAdapter(Context c, ArrayList<HashMap<String, String>> tablelist ) {
// TODO Auto-generated constructor stub
mContext = c;
mInflater = LayoutInflater.from(c);
tablist = tablelist;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
openInstance = new OpenTableActivity();
return openInstance.tableList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.button,
parent, false);
holder = new ViewHolder();
holder.btn = (Button) convertView.findViewById(R.id.button);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Button btn = (Button) view.findViewById(R.id.button);
HashMap<String, String> map = tablist.get(position);
for (java.util.Map.Entry<String, String> mapEntry : map.entrySet()) {
String key = mapEntry.getKey();
String value = mapEntry.getValue();
holder.btn.setText(key);
return convertView;
}
static class ViewHolder
{
Button btn;
}
}
定义的接口类如下:
public interface AsyncResponse {
public void WriteJsonArray(JSONArray tableList);
}
异步任务类如下
public class WebServiceAsyncTask extends AsyncTask<Object,Void,JSONArray> {
AsyncResponse listener;
public WebServiceAsyncTask(){}
public WebServiceAsyncTask(Context appcontext)
{
this.context=appcontext;
listener = (AsyncResponse) this.context;
}
String serviceUrl;
OpenTableActivity openInstance=new OpenTableActivity();
CategoryActivity catAct;
private static JSONArray json = null;
private Context context = null;
private static JSONObject jsonObject = null;
protected JSONArray doInBackground(Object... params) {
// TODO Auto-generated method stub
serviceUrl = (String) params[0];
String method = (String) params[1];
final HTTPHelper httph = new HTTPHelper(serviceUrl,context);
json = httph.fetch();
return json;
}
@Override
protected void onPostExecute(JSONArray result) { // invoked on the ui thread
// TODO Auto-generated method stub
// dismiss progress dialog
// update ui here
super.onPostExecute(result);
if (listener != null){
listener.WriteJsonArray(result);
}
}
在运行此程序时,只要调用活动类,我就会收到 Stackoverflow 错误。我尝试了许多不同的选项,但仍然是同样的错误。即使在进入活动类之前,也不确定我缺少什么,因为它给出的错误也无法调试。请指教。谢谢。