GridView不显示任何元素

时间:2018-02-26 11:08:34

标签: java android gridview custom-adapter

我的GridView应该显示传递给CustomAdapter的元素,但根本没有条目。该应用程序从数据库中获取数据,一切正常。它获取数组中的所有元素。发送请求以接收数据的代码位于片段中。 这是获取数据的代码:

private class RetrieveData extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... voids) {
        String stringURL = "xyz_php_file";
        URL url = null;
        StringBuffer stringBuffer = new StringBuffer();

        try {
            url = new URL(stringURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";

            while((line = bufferedReader.readLine()) != null){
                stringBuffer.append(line);
            }

            bufferedReader.close();
            inputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuffer.toString();
    }

    @Override
    protected void onPostExecute(String result) {

        String username, name, description;
        ArrayList<HashMap<String, String>> location = null;
        int i;

        try {
            JSONArray data = new JSONArray(result);
            location = new ArrayList<>();
            HashMap<String, String> map;

            for(i = 0; i < data.length(); i++){
                JSONObject object = data.getJSONObject(i);

                map = new HashMap<>();
                map.put("username", object.getString("username"));
                map.put("name", object.getString("name"));
                map.put("description", object.getString("description"));
                location.add(map);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(location != null){
            for(i = 0; i < location.size(); i++){
                username = location.get(i).get("username").toString();
                name = location.get(i).get("name").toString();
                description = location.get(i).get("description").toString();

                products.add(new Product(username, name, description));
            }
        }
    }
}

以下是片段中的一些代码:

private GridView mainGridView;
private ArrayList<Product> products = new ArrayList<>();

现在这就是我在片段中初始化GridView和CustomAdapter的方式:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_home, container, false);

    new RetrieveData().execute();

    TextView data = (TextView) view.findViewById(R.id.data);
    if(products != null){
        data.setText("Data loaded successfully");
    }else{
        data.setText("Error while downloading");
    }

    mainGridView = (GridView) view.findViewById(R.id.mealsgridview);
    CustomAdapter arrayAdapter = new CustomAdapter(view.getContext(), products);
    mainGridView.setAdapter(arrayAdapter);
    return view;
}

我的CustomAdapter类:

public class CustomAdapter extends BaseAdapter {

private Context context;
private List<Product> productsList;

public CustomAdapter(Context context, ArrayList<Product> sentProduct){
    this.context = context;
    this.productsList = sentProduct;
}

@Override
public int getCount() {
    return productsList.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    convertView = layoutInflater.inflate(R.layout.main_grid_layout, parent, false);

    Product product = productsList.get(position);

    TextView name = (TextView) convertView.findViewById(R.id.name);

    TextView price = (TextView) convertView.findViewById(R.id.description);

    name.setText(product.getName());

    description.setText(product.getDescription());

    return convertView;
}

}

最后我的产品类:

public class Product {

private String username;
private String name;
private String description;

public Product(String username, String name, String description) {
    this.username = username;
    this.name = name;
    this.description = description;
}

public String getUsername() {
    return username;
}

public String getName() {
    return name;
}

public String getDescription() {
    return description;
}

}

当我启动应用程序并切换到主页片段时,它显示“数据已成功加载”,因此我相信应用程序从数据库中获取所有数据并且ArrayList不是emnpty但GridView中没有显示任何元素。哪里可能有问题? 我将非常感谢你的回答!我已经阅读了一些教程,观看了视频,但我的代码仍然不想工作。

1 个答案:

答案 0 :(得分:1)

在关闭循环

后立即调用这行代码
    mainGridView = (GridView) view.findViewById(R.id.mealsgridview);
    CustomAdapter arrayAdapter = new CustomAdapter(view.getContext(), products);
    mainGridView.setAdapter(arrayAdapter);

我的意思是在此循环之后

for(i = 0; i < location.size(); i++){
                username = location.get(i).get("username").toString();
                name = location.get(i).get("name").toString();
                description = location.get(i).get("description").toString();

                products.add(new Product(username, name, description));
            }