Java listview在结束时加载更多项

Java listview在结束时加载更多项,java,android,Java,Android,我想实现一个listView,当我到达末尾时,它将加载更多的项 我实现了这个类: public class ProfileActivity extends ListActivity { protected static final int STATIC_INTEGER_VALUE = 500; protected static final int STATIC_INTEGER_GROUP = 501; private ListView lv; priv

我想实现一个listView,当我到达末尾时,它将加载更多的项

我实现了这个类:

public class ProfileActivity extends ListActivity  {

    protected static final int STATIC_INTEGER_VALUE = 500;

    protected static final int STATIC_INTEGER_GROUP = 501;


    private  ListView lv;
    private TextView profileNameText;
    private TextView emailText;
    private TextView phoneText;
    private TextView addressText;

    private Button saleBtn;
    private Button groupBtn;
    private Button deliverBtn;

    private ProfilePictureView profilePic;
    private MyCustomAdapter adapter;



    EditText edittext;

    private List<String> text = new ArrayList<String>();
    private List<Bitmap> listImages;



     int textlength = 0;

     ArrayList<String> text_sort = new ArrayList<String>();
     ArrayList<Bitmap> image_sort = new ArrayList<Bitmap>();



        public void onScroll(AbsListView view,
                int firstVisible, int visibleCount, int totalCount) {

                boolean loadMore = /* maybe add a padding */
                    firstVisible + visibleCount >= totalCount;

                if(loadMore) {
                    adapter.count += visibleCount; // or any other amount
                    adapter.notifyDataSetChanged();
                }
            }


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                  setContentView(R.layout.profile_page);


         lv = getListView();
         lv.setOnScrollListener(new OnScrollListener() {

             @Override
             public void onScrollStateChanged(AbsListView view, int scrollState) {
                 // TODO Auto-generated method stub


             }

             @Override
             public void onScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {

                 boolean loadMore = /* maybe add a padding */
                         firstVisibleItem + visibleItemCount >= totalItemCount;

                        if(loadMore) {
                            adapter.count += visibleItemCount; // or any other amount
                            adapter.notifyDataSetChanged();
                        }

             }
         });
         LayoutInflater inflater = getLayoutInflater();
        View header = inflater.inflate(R.layout.profile_header, (ViewGroup) findViewById(R.id.header_layout_root));
        lv.addHeaderView(header, null, false);


         adapter =new MyCustomAdapter(text,listImages);
          lv.setAdapter(adapter);
          edittext= (EditText) findViewById(R.id.EditText01);




    class MyCustomAdapter extends BaseAdapter{

        int count = 1;
          public   List<String> text_array = new ArrayList<String>();
          public   List<Bitmap> image_array = new ArrayList<Bitmap>();
          public int getCount(){
               return text_array.size();
          }

          MyCustomAdapter(List<String> text, List<Bitmap> image)
          {
           text_array = text;
           image_array = image;
          }
          public long getItemId(int position){
               return position;
          }
          public String getItem(int position){
               return null;
          }
          public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflate = getLayoutInflater();
            View v = inflate.inflate(R.layout.listview, parent, false);
            final ImageView image = (ImageView) v.findViewById(R.id.ImageView01);
            TextView txtUnderImage =(TextView) v.findViewById(R.id.TextView01);

             if(listImages.get(position) != null) {
                         Bitmap bitmap = image_array.get(position);
                        image.setImageBitmap(bitmap);
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        final byte[] byteArray = stream.toByteArray();

                         image.setOnClickListener(new View.OnClickListener() {

                            public void onClick(View v) {
                                // Switching to Register screen
                             Intent i = new Intent(getApplicationContext(), itemSaleActivity.class);
                             i.putExtra("picture", byteArray);
                            startActivity(i);


                                }
                        });
                         txtUnderImage.setText(text_array.get(position));
                         image.setVisibility(View.VISIBLE);
              } else {
                         image.setVisibility(View.GONE);
                         image.setImageBitmap(null);
                          image.setVisibility(View.VISIBLE);
          }
         return v;

        }
         public void addObject(String text, Bitmap bitmap) {
                text_array.add(text);
                image_array.add(bitmap);
            notifyDataSetChanged();
         } 

         public void addFirst(String text, Bitmap bitmap) {

                image_array.add(0,bitmap);
                text_array.add(0,text);

                notifyDataSetChanged();
             } 
         public void removeObject(String text,Bitmap bitmap) {

                int indexToRemove = image_array.indexOf(bitmap);
                image_array.remove(indexToRemove);
                text_array.remove(indexToRemove);
                notifyDataSetChanged();
         }
         public void deleteAll() {
             image_array.clear();
             text_array.clear();
             notifyDataSetChanged();
         }
    }
    }
正如我从logcat中看到的,我在这一行中得到一个空指针异常:

lv.setOnScrollListener(new OnScrollListener() {
我没有发现问题

这也是我的xml文件:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/linearLayout1"
        android:orientation="vertical" >

                <EditText android:id="@+id/EditText01"
                                android:layout_height="wrap_content"
                                android:layout_width="fill_parent"
                                android:hint="Search">                               
                </EditText>

                <ListView android:id="@+id/android:list"
                                android:layout_height="wrap_content"
                                android:layout_width="fill_parent">
                </ListView>

    </LinearLayout>

谢谢

在您的版面上尝试
android:id=“@android:id/list”
,而不是
android:id=“@+id/android:list”


(@Raghav Sood比我快:)

先创建适配器。然后设置onscrolllistener。

尝试
android:id=“@android:id/list”
而不是
android:id=“@+id/android:list”
我在同一行中遇到了同样的问题:“NULLpointerException”尝试向列表视图中添加更多项…可能还会有4到5项,我猜是因为onscroll()可能在listview呈现之前就已经被调用了,因此您将获得nullpointer…我将其删除,并且在同一行中仍然存在相同的问题:“NULLpointerException”,如果我删除了:“lv.setOnScrollListener(new OnScrollListener(){”一切正常
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/linearLayout1"
        android:orientation="vertical" >

                <EditText android:id="@+id/EditText01"
                                android:layout_height="wrap_content"
                                android:layout_width="fill_parent"
                                android:hint="Search">                               
                </EditText>

                <ListView android:id="@+id/android:list"
                                android:layout_height="wrap_content"
                                android:layout_width="fill_parent">
                </ListView>

    </LinearLayout>