我想实现一个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:
03-15 15:12:57.885: E/AndroidRuntime(2027): FATAL EXCEPTION: main
03-15 15:12:57.885: E/AndroidRuntime(2027): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sale/com.example.sale.TabMainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sale/com.example.sale.ProfileActivity}: java.lang.NullPointerException
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.os.Looper.loop(Looper.java:137)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-15 15:12:57.885: E/AndroidRuntime(2027): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 15:12:57.885: E/AndroidRuntime(2027): at java.lang.reflect.Method.invoke(Method.java:511)
03-15 15:12:57.885: E/AndroidRuntime(2027): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-15 15:12:57.885: E/AndroidRuntime(2027): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-15 15:12:57.885: E/AndroidRuntime(2027): at dalvik.system.NativeStart.main(Native Method)
03-15 15:12:57.885: E/AndroidRuntime(2027): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sale/com.example.sale.ProfileActivity}: java.lang.NullPointerException
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.widget.TabHost.setCurrentTab(TabHost.java:413)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.widget.TabHost.addTab(TabHost.java:240)
03-15 15:12:57.885: E/AndroidRuntime(2027): at com.example.sale.TabMainActivity.addTab(TabMainActivity.java:82)
03-15 15:12:57.885: E/AndroidRuntime(2027): at com.example.sale.TabMainActivity.setTabs(TabMainActivity.java:57)
03-15 15:12:57.885: E/AndroidRuntime(2027): at com.example.sale.TabMainActivity.onCreate(TabMainActivity.java:53)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.Activity.performCreate(Activity.java:5104)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-15 15:12:57.885: E/AndroidRuntime(2027): ... 11 more
03-15 15:12:57.885: E/AndroidRuntime(2027): Caused by: java.lang.NullPointerException
03-15 15:12:57.885: E/AndroidRuntime(2027): at com.example.sale.ProfileActivity$3.onScroll(ProfileActivity.java:323)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1340)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.widget.AbsListView.setOnScrollListener(AbsListView.java:1329)
03-15 15:12:57.885: E/AndroidRuntime(2027): at com.example.sale.ProfileActivity.onCreate(ProfileActivity.java:306)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.Activity.performCreate(Activity.java:5104)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-15 15:12:57.885: E/AndroidRuntime(2027): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-15 15:12:57.885: E/AndroidRuntime(2027): ... 23 more
正如我从 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>
谢谢