Android GridView无法从json加载数据

时间:2014-11-25 13:54:03

标签: android json gridview android-volley

当我从json填充数据时,我遇到了网格视图的问题。 这是我的代码

public class Guest extends Activity {

    private static String link_data = "http:some_url";
    ArrayList<String> stringArrayList = new ArrayList<String>();
    private static String TAG = Guest.class.getSimpleName();

    private String[] guest = new String[]{};

    private GridView gridView;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guest);

        makeJsonArrayRequest();

        gridView = (GridView) findViewById(R.id.gridView1);

        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, guest);

        bindingData();

        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

            }
        });
    }

    private void bindingData(){
        adapter.notifyDataSetChanged();
        gridView.invalidateViews();     
        gridView.setAdapter(adapter);

    }

    private void makeJsonArrayRequest() {

        JsonArrayRequest req = new JsonArrayRequest(link_data,
                new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.d(TAG, response.toString());
                try {
                    for (int i = 0; i < response.length(); i++) {
                        guest = new String [response.length()];
                        JSONObject data = (JSONObject) response
                                .get(i);

                        String name = data.getString("name");

                        guest[i] = name;
                        Log.d(TAG, guest[i].toString());
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(req);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.guest, menu);
        return true;
    }

}

这是我的XML文件

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:numColumns="2"
    android:gravity="center"
    android:columnWidth="50dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</GridView>

当我登录日志时,我在该阵列中找到了所有数据,但gridview没有显示数据..

有人可以帮帮我吗? 我的错误在哪里? 感谢

2 个答案:

答案 0 :(得分:0)

您过早致电bindingData()。可能你用空guest数组设置了适配器。在bindingData

中致电onResponse

答案 1 :(得分:0)

将这两行放在onResponse中。

  adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, guest);

        bindingData();

如下所示

private void makeJsonArrayRequest() {

        JsonArrayRequest req = new JsonArrayRequest(link_data,
                new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.d(TAG, response.toString());
                try {
                    for (int i = 0; i < response.length(); i++) {
                        guest = new String [response.length()];
                        JSONObject data = (JSONObject) response
                                .get(i);

                        String name = data.getString("name");

                        guest[i] = name;
                        Log.d(TAG, guest[i].toString());
                    }
adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, guest);

        bindingData();
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(req);
    }