D. B.
D. B.

Reputation: 498

GridView doesn't show any elements

My GridView is supposed to show elements passed to CustomAdapter but there are no entries at all. The app fetches data from database and there everything works fine. It gets all elements in the array. The code that sends a request to recieve data is in a fragment. That is the code to fetch data:

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

Here is some code from fragment:

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

And now that is the way I initialized GridView and CustomAdapter in the fragment:

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

My CustomAdapter class:

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

}

And finally my Product Class:

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

}

When I launch application and switch to home fragment it shows "Data loaded successfully", so I believe the app gets all data from database and ArrayList isn't emnpty but there is no elements shown in GridView. Where might be a problem? I will appreciate your answers! I've read some tutorials, watched videos but my code still doesn't want to work.

Upvotes: 0

Views: 69

Answers (2)

Bhavya Gandhi
Bhavya Gandhi

Reputation: 507

AsyncTask class allows you to perform background operations and publish results on the UI thread.

Try this one,

onCreateView

 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);
new RetrieveData().execute();

And in onPostExecute,

arrayAdapter .notifyDataSetChanged();

Upvotes: 1

Sunil P
Sunil P

Reputation: 3838

call this lines of code right after closing for loop

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

I mean after this for loop

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

Upvotes: 1

Related Questions