为什么列表视图不显示任何项目

时间:2013-12-07 07:33:14

标签: java android listview custom-lists

我需要在我的代码中提供帮助,我为自定义列表视图编写了一个代码来显示multi_cars但是列表视图没有显示我使用log-cat跟踪它的任何事情我确信数据库正在执行以及它作为insert和getAlLData的所有功能,当我跟踪代码时,我发现应用程序访问自定义列表视图类并且只打印构造函数中的日志语句,并且不再访问函数视图然后返回到viewcars类,我不知道是什么原因,所以它没有显示任何东西 以下代码适用于viewCars类:

        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

            }

        }

这是自定义列表视图的代码:

        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 个答案:

答案 0 :(得分:2)

更改此

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

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

另外

url.openConnection().getInputStream()

应该在thread或使用asynctask或使用排球

答案 1 :(得分:0)

你犯了三个错误

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

使用此代码更新您的适配器

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