java - 为什么 ListView 不显示任何项目

标签 java android listview custom-lists

我的代码需要帮助,我编写了自定义 ListView 的代码来显示 multi_cars,但 ListView 没有显示我使用 log-cat 跟踪它的任何内容,并且我确信数据库正在执行操作好吧,它的功能与 insert 和 getAlLData 一样,当我跟踪代码时,我发现应用程序访问自定义 ListView 类并仅打印构造函数中的日志语句,并且不再访问函数 View ,然后返回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

            }

        }

这是自定义 ListView 的代码:

        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;



            }

        }

最佳答案

改变这个

@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()

应该在线程中或使用asynctask或使用volley

关于java - 为什么 ListView 不显示任何项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20438923/

相关文章:

java - 从路径中检索上一级目录名称

java - 在 Java 中存储数据

java - 无法解析方法 subscribe(rx.Scheduler)

java - 如果有子键,如何直接从根获取子节点的值

java - Libgdx 用图像填充屏幕

javascript - 如何在剑道 ListView 中更改数据源

c++ - 如果目标应用程序在 C++ 中崩溃,则无法使用 LVM_GETITEMTEXT

java - 如果前面没有其他内容,则用于匹配某些内容的正则表达式

java - 将 java 日期传递到剩余查询参数中

java - Listview 的 onclick 事件问题