why the list view doesn't show any item

1000 views Asked by At

I need a help in my code, i wrote a code for custom list view to show multi_cars but the list view doesn't show any thing i trace it using the log-cat and i'm sure that the database is doing well and all it's function as insert and getAlLData , when i trace the code i find that the application visit the custom list view class and print only the log statement which is in the constructor and doesn't visit the function view anymore and then return to the viewcars class ,,i don't know what the reason so it doesn't show any thing the following code is for the viewCars class:

        package com.example.rentalcarsproject;

        import java.util.ArrayList;
        import java.util.List;
        import com.example.rentalcarsproject.my_database.connection ;
        import com.example.rentalcarsproject.CustomListView ;
        import android.app.Activity;
        import android.content.Intent;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.Menu;
        import android.view.View;
        import android.widget.AdapterView;
        import android.widget.ArrayAdapter;
        import android.widget.Toast;
        import android.widget.AdapterView.OnItemClickListener;
        import android.widget.AdapterView.OnItemSelectedListener;
        import android.widget.ListView;

        public class viewcars  extends Activity implements OnItemSelectedListener{

            ListView listView;

            connection connectionInstance = null;
            ArrayList<cars> userArrayList=new ArrayList<cars>();
            ArrayAdapter<cars> userArrayAdapter;


            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.viewcars);
                Log.d("viewcars on create ", "okkkkk");
                listView=(ListView) findViewById(R.id.listView1);

                 listView.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> adapter, View view, int position,
                                long ID) {
                            // TODO Auto-generated method stub


                            cars temp=(cars) adapter.getItemAtPosition(position);
                            Intent i1=new Intent(getApplicationContext(),act1cars.class);
                            i1.putExtra("car_key", String.valueOf(temp.getCar_key()));
                            i1.putExtra("car_name", temp.getCar_name().toString());
                            i1.putExtra("car_model",temp.getCar_model().toString());
                            i1.putExtra("price", temp.getPrice().toString());
                            i1.putExtra("location", temp.getLocation().toString());
                            i1.putExtra("description",temp.getDescription().toString());
                            i1.putExtra("picture", temp.getPicture().toString());
                            startActivity(i1);



                        }


                    });
                 connectionInstance=new connection(getApplicationContext());

                 Log.d("before array", "ok");
                    ArrayList<cars> retrieveArrayList=new ArrayList<cars>();
                     Log.d("test1", "ok");

                    retrieveArrayList=connectionInstance.GetAllData();
                     Log.d("test2", "ok");
                     Log.d("Array Size", String.valueOf(retrieveArrayList.size()));
                    connectionInstance.close();
                     Log.d("after connection", "ok");
                    userArrayList=new ArrayList<cars>();//used in custom list view
                    for(int i=0;i<retrieveArrayList.size();i++)
                    {

                         Log.d("inside for loop ", "ok");
                        cars show=new cars();
                        show.setCar_name(retrieveArrayList.get(i).getCar_name());
                        Log.d("CAR_NAME  ", retrieveArrayList.get(i).getCar_name());
                        show.setCar_model(retrieveArrayList.get(i).getCar_model());
                        show.setPrice(retrieveArrayList.get(i).getPrice());
                        show.setLocation(retrieveArrayList.get(i).getLocation());
                        show.setDescription(retrieveArrayList.get(i).getDescription());
                        show.setPicture(retrieveArrayList.get(i).getPicture());
                        userArrayList.add(show);

                    }
                     Log.d("arrat_for list view  Size", String.valueOf(userArrayList.size()));
                     Log.d("before setadapter", "ok");
                    listView.setAdapter(new CustomListView(viewcars.this, userArrayList));
                    // Log.d("after setadapter", "ok");



            }



            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        }

And this is the code for the custom list view :

        package com.example.rentalcarsproject;

        import java.io.IOException;
        import java.net.MalformedURLException;
        import java.net.URL;
        import java.util.ArrayList;
        import junit.framework.Test;
        import android.content.Context;
        import android.content.Intent;
        import android.graphics.Bitmap;
        import android.graphics.BitmapFactory;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.view.View.OnClickListener;
        import android.widget.BaseAdapter;
        import android.widget.ImageView;
        import android.widget.TextView;

        public class CustomListView extends BaseAdapter {
            ImageView picture;
            TextView name;
            cars attributes;
            Context context;
            ArrayList<cars> arrayList=new ArrayList<cars>();
            public CustomListView(Context _context,ArrayList<cars> _arrayList) {
                // TODO Auto-generated constructor stub
                context=_context;
                arrayList=_arrayList;
                Log.d("constructor", "ok");
            }


            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public Object getItem(int arg0) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public long getItemId(int arg0) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public View getView(int arg0, View arg1, ViewGroup arg2) {
                // TODO Auto-generated method stub
                View view=arg1;
                Log.d("inside cutom list view ", "ok");
                Log.i("name      ", arrayList.get(arg0).getCar_name());
                if(view==null)
                {
                    LayoutInflater layoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view=layoutInflater.inflate(R.layout.customlistview,null); // add this **.xml layout to the custom listView
                }
                (view.findViewById(R.id.button1))
                .setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {
                        Log.d("onclick1 ", "ok ");
                        Intent intent = new Intent(context, test.class);

                        context.startActivity(intent);

                    }
                });


                name=(TextView) view.findViewById(R.id.textView1);
                picture=(ImageView) view.findViewById(R.id.imageView1);


                attributes=arrayList.get(arg0);
                name.setText(String.valueOf(attributes.getCar_name()));
                URL url = null;
                try {
                    url = new URL(attributes.getPicture());
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Bitmap bmp = null;
                try {
                    bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                picture.setImageBitmap(bmp);
                /******/

                return view;



            }

        }
2

There are 2 answers

0
Raghunandan On

Change this

@Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 0;
        }

to

@Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arrayList.size();
        }

Also

url.openConnection().getInputStream()

should be in a thread or use asynctask or use volley

2
Imtiyaz Khalani On

you made three mistake

  ArrayList<cars> arrayList=new ArrayList<cars>();
        public CustomListView(Context _context,ArrayList<cars> _arrayList) {
            // TODO Auto-generated constructor stub
            context=_context;
            arrayList=_arrayList;
            Log.d("constructor", "ok");
        }


        @Override
        public int getCount() {
            // return list size

            return 0;
        }

        @Override
        public Object getItem(int arg0) {
            //return object form your list
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // return same argument in your case return  arg0;
            return 0;
        }

update your adapter with this code

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arrayList.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return arrayList.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }