java - GridView不显示任何元素

标签 java android gridview custom-adapter

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

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

这是 fragment 中的一些代码:

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

现在这就是我在 fragment 中初始化 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;
}

}

当我启动应用程序并切换到主 fragment 时,它显示“数据加载成功”,所以我相信应用程序从数据库获取所有数据,并且 ArrayList 不是空的,但 GridView 中没有显示任何元素。哪里可能有问题? 我将感谢您的回答!我已经阅读了一些教程,观看了视频,但我的代码仍然无法运行。

最佳答案

关闭 for 循环后立即调用此代码行

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

我的意思是在这个 for 循环之后

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

关于java - GridView不显示任何元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48987100/

相关文章:

java - (作业)Java GUI : button increments/decrements by 2 instead of 1

带有子项目的 Android Gradle 构建

c# - 将字符串转换为连接到mysql的gridview中的日期

c# - GridView RowUpdating 在 PostBack 后返回旧值

c# - 使用绑定(bind)数据集中的值设置 Gridview 行背景颜色

java - 为什么 session 之外的 Hibernate 实体应该实现 equals() 和 hashcode() ?

java - 如何排除 bouncycaSTLe weblogic 12c 库

java - 读/写 long 的位

java - 如何在不同的类布局中显示呈现的对象?

android - 从 Ice Cream Sandwich 添加应用程序功能