修复适配器construtcor为arraylist

时间:2015-10-03 13:38:26

标签: android

我正在学习android,我有这个错误,我不知道如何解决

适配器The constructor is undefined

的参数中的错误getdata()
   adapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData());

我知道striraylist的Gridviewadapter的构造函数应该修复..但是如何?

public GridViewAdapter(Context context, int layoutResourceId, ArrayList<Listitem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.mcontext =context;
}

这是代码

protected void showList(){
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray(TAG_RESULTS);

        for(int i=0;i<peoples.length();i++){
            JSONObject c = peoples.getJSONObject(i);
            String id = c.getString(TAG_ID);
            String url = c.getString(TAG_URL);
            Listitem.add(new Listitem(id,url));
        }

        adapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData());

        list.setAdapter(adapter);

的GetData()

public void getData(){     getDataJSON类扩展了AsyncTask {

    @Override
    protected String doInBackground(String... params) {
        DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httppost = new HttpPost("http://justedhak.comlu.com/get-data.php");

        // Depends on your web service
        httppost.setHeader("Content-type", "application/json");

        InputStream inputStream = null;
        String result = null;
        try {
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();
            // json is UTF-8 by default
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            result = sb.toString();
        } catch (Exception e) {
            // Oops
        }
        finally {
            try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
        }
        return result;
    }

    @Override
    protected void onPostExecute(String result){
        myJSON=result;
        showList();
    }

1 个答案:

答案 0 :(得分:1)

您的代码应该是这样的。

从服务器获取数据从您的oncreate方法调用此行

GetDataJSON .execute();

你上课就是这样。

public class GetDataJSON extends AsyncTask{
 @Override
    protected String doInBackground(String... params) {
        DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httppost = new HttpPost("http://justedhak.comlu.com/get-data.php");

        // Depends on your web service
        httppost.setHeader("Content-type", "application/json");

        InputStream inputStream = null;
        String result = null;
        try {
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();
            // json is UTF-8 by default
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            result = sb.toString();
        } catch (Exception e) {
            // Oops
        }
        finally {
            try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
        }
        return result;
    }

    @Override
    protected void onPostExecute(String result){
        myJSON=result;
        showList();
    }
}

你的方法showList就像。

protected void showList(){
        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            peoples = jsonObj.getJSONArray(TAG_RESULTS);

            for(int i=0;i<peoples.length();i++){
                JSONObject c = peoples.getJSONObject(i);
                String id = c.getString(TAG_ID);
                String url = c.getString(TAG_URL);
                Listitem.add(new Listitem(id,url));
            }

            adapter = new GridViewAdapter(this, R.layout.grid_item_layout, Listitem);

            list.setAdapter(adapter);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }