1

My custom listview does not show any data, I have tried re-coding the adapters and everything else but no dice.

It does not even show any error, and that's what drives me crazy haha. Can anyone help me debug my code.

This is my activity:

private ListView listView;


private ArrayList<String> orderName;

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

    orderName = new ArrayList<>();

    ListView listView = (ListView) findViewById(R.id.listViewOrder);

    viewOrderAdapter adapter = new viewOrderAdapter(viewOrdersPage.this, orderName);
    listView.setAdapter(adapter);

    getBoxInformation();

}

  private void getBoxInformation() {
    final ProgressDialog loading = ProgressDialog.show(this, "GETTING BOX INFROMATION", "Processing Request...", false, false);
    String url = DATA_URL + textUsername.getText().toString().trim();
    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            showJSON(response);
            loading.dismiss();
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(viewOrdersPage.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}
private void showJSON(String response) {
  String name = "";

    try {
        jsonObject = new JSONObject(response);
        result = jsonObject.getJSONArray(JSON_ARRAY);
        order = result.getJSONObject(0);
        name = order.getString(TAG_ORDERNAME);

        JSONArray jsonarray = new JSONArray(name);
        for (int i = 0; i < jsonarray.length(); i++) {
            JSONObject jsonobject = jsonarray.getJSONObject(i);
            names = jsonobject.getString("name");


            //THE TOAST DISPLAYS THE TEXT SO I KNOW THE CODE WORKS, EXCEPT THE LISTVIEW
            Toast.makeText(viewOrdersPage.this, names, Toast.LENGTH_LONG).show();

     orderName.add(names);

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

This is the Adapter class:

public class viewOrderAdapter extends BaseAdapter {

private Context context;

private ArrayList<String> orderName;


public viewOrderAdapter(Context context, ArrayList<String> orderName){
    this.context = context;
    this.orderName = orderName;

}

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

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

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


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

    final View listView;
    final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listView = inflater.inflate(R.layout.view_orders_item, null);

    TextView name = (TextView) listView.findViewById(R.id.orderNames);


    name.setText(orderName.get(position));

    return listView;
}

}

Thanks for any help in advance!

2 Answers 2

2

Issue : Network request will take time but you are creating the adapter when there is no data in the list so you need to tell the adapter that there is a change in the data

move adapter reference outside onCreate

private ArrayList<String> orderName;
private viewOrderAdapter adapter ; // adapter reference

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_orders_page);    
    orderName = new ArrayList<>();
    ListView listView = (ListView) findViewById(R.id.listViewOrder);    
    adapter = new viewOrderAdapter(viewOrdersPage.this, orderName);
    listView.setAdapter(adapter);    
    getBoxInformation();

}

Then add adapter.notifyDataSetChanged() at the end of showJSON function

or

you can create and set adapter on the listview at the end of showJSON function

Sign up to request clarification or add additional context in comments.

1 Comment

Yes sir you are correct, let me just wait five minutes to accept your answer haha. I guess coding for 8 hours straight really hits the brain haha. Thanks sir!
1

This is activity:

    private ListView listView;


    private ArrayList<String> orderName;

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

        orderName = new ArrayList<>();

        ListView listView = (ListView) findViewById(R.id.listViewOrder);



        getBoxInformation();

    }

    private void setDataInList(){
    viewOrderAdapter adapter = new viewOrderAdapter(viewOrdersPage.this, orderName);
        listView.setAdapter(adapter);
    }

      private void getBoxInformation() {
        final ProgressDialog loading = ProgressDialog.show(this, "GETTING BOX INFROMATION", "Processing Request...", false, false);
        String url = DATA_URL + textUsername.getText().toString().trim();
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                showJSON(response);
                loading.dismiss();
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(viewOrdersPage.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                    }
                });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
    private void showJSON(String response) {
      String name = "";

        try {
            jsonObject = new JSONObject(response);
            result = jsonObject.getJSONArray(JSON_ARRAY);
            order = result.getJSONObject(0);
            name = order.getString(TAG_ORDERNAME);

            JSONArray jsonarray = new JSONArray(name);
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                names = jsonobject.getString("name");


                //THE TOAST DISPLAYS THE TEXT SO I KNOW THE CODE WORKS, EXCEPT THE LISTVIEW
                Toast.makeText(viewOrdersPage.this, names, Toast.LENGTH_LONG).show();

         orderName.add(names);

            }
if(orderName.size()>0){
setDataInList();
}
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

This is the Adapter class:

public class viewOrderAdapter extends BaseAdapter {

private Context context;

private ArrayList<String> orderName;


public viewOrderAdapter(Context context, ArrayList<String> orderName){
    this.context = context;
    this.orderName = orderName;

}

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

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

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


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

    View listView;
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listView = inflater.inflate(R.layout.view_orders_item, null);

    TextView name = (TextView) listView.findViewById(R.id.orderNames);


    name.setText(orderName.get(position));

    return listView;
}

1 Comment

Hey sir I got the answer already, I was missing an adapter.notifydatasetchanged haha. Anyways thanks for the help sir you deserve and upvote!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.