我正在尝试从Async Task收到的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();
}
}}
用于填充网格视图的Adapter类如下所示
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);
}
Async任务类如下
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错误。我尝试了很多不同的选项,但仍然是同样的错即使在进入活动类之前,也不确定我错过了什么,因为它无法进行调试。请指教。感谢。
答案 0 :(得分:1)
发布解决方案。来自讨论
您需要将以下内容移至WriteJsonArray。获得数据后,需要将其传递给适配器类
gridview.setAdapter(new OpenTableAdapter(this,tableList));
将您的getCount
替换为
@Override
public int getCount() {
return tablist.size();
}
在getview中删除你的for循环
HashMap<String,String> map = tablist.get(position);
String value = map.get("key");
// make sure the key i right. "key" here is only an example. replace with right key
holder.btn.setText("value");
删除此
WebServiceAsyncTask webTask = new WebServiceAsyncTask(this); // at the beginnning
从asynctask
中删除它OpenTableActivity openInstance=new OpenTableActivity();
答案 1 :(得分:0)
AsyncTask<Void, Void, Boolean> waitForCompletion = new AsyncTask<Void, Void, Boolean>() {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.setVisibility(View.VISIBLE);
}
@Override
protected Boolean doInBackground(Void... params) {
}
@Override
protected void onPostExecute(Boolean result) {
}
}