2

I am trying to populate Grid View from JSON response received from Async Task. Below is the activity file

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();
        }


    }}

The Adapter class to fill grid view is as below

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;
       } 

}

the interface class defined is as below:

public interface AsyncResponse {

    public void WriteJsonArray(JSONArray tableList);

}

The Async task class is as below

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);
       }


}

On running this I am getting Stackoverflow error as soon as activity class is called. I tried many different options but still same error. Not sure what I am missing as unable to debug too as its giving error even before entering the activity class. Please advise. Thanks.

13
  • 1
    Look at the StackOverflowException, identify the method that causes the recursion, eliminate the recursion. Please respect that StackOverflow (this site) is not your debugger. Commented Aug 16, 2013 at 10:19
  • I don't know why you use OpenTableActivity openInstance=new OpenTableActivity(); in both your adapter and AsyncTask. You can read more about how to use adapter and asynctask before using them like that! Commented Aug 16, 2013 at 10:21
  • That was done when I was trying different options to make this work. Thanks for pointing this. Will remove it as not required now. @allprog : I understand this but I posted only after trying for several hours and still no luck. Commented Aug 16, 2013 at 10:24
  • At least add the stack trace to the question. Place break points in the code and execute step-by-step. Eclipse is a great help with that. Commented Aug 16, 2013 at 10:26
  • I tried doing that as well but it throws error as soon OpenTableActivity is called on pressing button. Its not even going to OnCreate Method. The error thrown is java.lang.ref.FinalizerReference.add(FinalizerReference.java:54) android.os.StrictMode$InstanceTracker.<init>(StrictMode.java:2225) at android.os.StrictMode.trackActivity(StrictMode.java:1927) at android.app.Activity.<init>(Activity.java:755) at com.example.roms.OpenTableActivity.<init>(OpenTableActivity.java:25) Commented Aug 16, 2013 at 10:33

2 Answers 2

1

Posting solution. From the discussion

You need to move the below to WriteJsonArray. Once you get the data you need to pass the same to the adapter class

    gridview.setAdapter(new OpenTableAdapter(this,tableList));

Replace your getCount by

@Override 
public int getCount() { 

return tablist.size(); 
}

In getview remove your for loop

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");

remove this

WebServiceAsyncTask webTask = new WebServiceAsyncTask(this); // at the beginnning

remove this from asynctask

OpenTableActivity openInstance=new OpenTableActivity();
Sign up to request clarification or add additional context in comments.

Comments

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) {
                     }
     }

3 Comments

it is work properly in my project in Onpreexection() use like loading dialog box...onbackground() get your all data from json...and onpostexecution() dismiss loadin dialog box or set you data or whatever u want to do next...it will work properly...
i don't think so. there are other mistakes in ops question that should be rectified. this answer does not help at all. check this discussion between me and op you will now yourself
your project is different from op's question. this does not answer the question posted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.