Search code examples
androidlistlistviewretrofit2custom-adapter

App get crashed when trying to display Data through listview


I'm trying to get data using retrofit2 and display those data using a list passing through as a parameter of Custom adapter. When I store data in a List in onResponse() method, in onResponse() method list have some value. But in oncreate() method its give me null. Though, I declared List as global. When I run the app sometimes its display nothing and sometimes app get crash. I know it's sounds like crazy. But it's happen. so, I want to know, what's wrong with my Code? how can I display data in listview?

Forgive me if something wrong with my question pattern yet this my maiden question at this site.

MainActivity`

public class LaboratoryValues extends AppCompatActivity {

    public List<Data> productList = null;
    List<Data>arrayList = null;
    int size;
    String st;

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

        //productList = new ArrayList<Data>();
        getInvestigation();
        for(int i =0; i < size; i++){

            st = arrayList.get(i).getName();


        }
        System.out.println("Name : "+st);//here print Name : null

        ListView lview = (ListView) findViewById(R.id.listview);
        ListviewAdapter adapter = new ListviewAdapter(this, arrayList);
        lview.setAdapter(adapter);



    }


    private void getInvestigation() {

       /* final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setCancelable(false); // set cancelable to false
        progressDialog.setMessage("Please Wait"); // set body
        progressDialog.show(); // show progress dialog*/


        ApiInterface apiService =
                Api.getClient(ApiInterface.BASE_URL).create(ApiInterface.class);


        Call<Investigation> investigationCall = apiService.getInvestigation();
        investigationCall.enqueue(new Callback<Investigation>() {
            @Override
            public void onResponse(Call<Investigation> call, Response<Investigation> response) {

                arrayList = response.body().getData();
              //productList.addAll(arrayList);
                size = response.body().getData().size();
                for (int i = 0; i < size; i++) {

                    System.out.println("Name : " + arrayList.get(i).getName());//here printing Name is ok


                }

            }
            @Override
            public void onFailure(Call<Investigation> call, Throwable t) {
                Toast.makeText(getApplicationContext(),"data list is empty",Toast.LENGTH_LONG).show();

            }
        });




    }


}

Custom Adapter (listviewAdapter)

public class ListviewAdapter extends BaseAdapter {


        public List<Data> productList;
        Activity activity;
        //Context mContext;

        public ListviewAdapter(Activity activity, List<Data> productList) {
            super();
            this.activity = activity;
            this.productList = productList;
        }

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

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

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

        private class ViewHolder {
            TextView name;
            TextView normal_finding;
            TextView increased;
            TextView decreased;
            TextView others;
        }

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

            ViewHolder holder;
            LayoutInflater inflater = activity.getLayoutInflater();


            if (convertView == null) {
                convertView = inflater.inflate(R.layout.listview_row, null);
                holder = new ViewHolder();
             holder.name = convertView.findViewById(R.id.name);
             holder.normal_finding =convertView.findViewById(R.id.normal_finding);
             holder.increased = convertView.findViewById(R.id.increased);
             holder.decreased = convertView.findViewById(R.id.decreased);
             holder.others =convertView.findViewById(R.id.others);
             convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Data item = productList.get(position) ;
            holder.name.setText(item.getName());
            System.out.println("holderName : "+item.getName() );
            holder.normal_finding.setText(item.getNormal_finding());
            System.out.println("holderName : "+item.getNormal_finding() );
            holder.increased.setText(item.getIncreased());
            holder.decreased.setText(item.getDecreased());
            holder.others.setText(item.getOthers());

            return convertView;


        }

    }

Solution

  • It's perfectly normal that it dosent work. putting your method getInvistigation() before the loop does not mean that the response of your request was done Calling a webservice creates another thread that waits for the server to send the response, sometimes the response takes time depends from the server and the latency of your internet connection.

    you simply need to place the treatment (the loop and adapter) inside getInvistagion after getting the data.