2

我正在尝试从从异步任务收到的​​ 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 错误。我尝试了许多不同的选项,但仍然是同样的错误。即使在进入活动类之前,也不确定我缺少什么,因为它给出的错误也无法调试。请指教。谢谢。

4

2 回答 2

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();
于 2013-08-16T11:06:06.330 回答
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) {
                     }
     }
于 2013-08-16T11:08:47.830 回答