更新arraylist中的新数据

时间:2015-01-01 23:51:34

标签: android android-listview arraylist nullpointerexception

我有问题在arraylist中添加新数据。当调用LoadData对象Iteamcher有新数据但我不知道追加它。所以当我设置数据时,listview会发生NullPointerException。

我需要帮助。请编辑我的代码。

非常感谢!

public class InfoTeacherFragment extends Fragment {

    private String URL = "http://scv.udn.vn/dhdn/trdhsp/page/";
    ProgressDialog pDialog;
    Context mContext;
    private ArrayList<ItemTeacher> data;
//  ItemTeacher itemTeacher;
    // ArrayList<String> Name;
    // ArrayList<String> School;
    // ArrayList<String> Link;
    // ArrayList<String> Icon;
    ListView lvItem;
    DisplayImageOptions options;
    ImageLoader imageloader;
    View mFooterView;
    InfoTeacher adapter;
    ProgressBar loading;
    int CurrentPage = 1;
    int visibleThreshold = 3;
    int startPage = 1;
    boolean loadingMore = false;

    public InfoTeacherFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_mission)
                .showImageForEmptyUri(R.drawable.ic_empty)
                .showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
                .cacheOnDisk(true).considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565).build();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.teacherinfo_layout,
                container, false);
        mContext = container.getContext();
        lvItem = (ListView) rootView.findViewById(R.id.lvTeacher);
        data = new ArrayList<ItemTeacher>();
        adapter = new InfoTeacher(mContext, data);

        lvItem.setOnScrollListener(new EndlessScrollListener() {

            @Override
            public void onLoadMore(int page, int totalItemsCount) {
                CurrentPage++;
                customLoadMoreDataFromApi(CurrentPage);

            }
        });
        imageloader = ImageLoader.getInstance();
        imageloader.init(ImageLoaderConfiguration.createDefault(mContext));
        if (Utils.isOnline(mContext) == true
                && Utils.KEY_CHECK_SUCCESS.equals("SUCCESS")) {
            loadDataFromServer(URL + startPage);
        } else if (Utils.isOnline(mContext) == true
                && Utils.KEY_CHECK_SUCCESS.equals("FAIL")) {
            Utils.MyToast(Utils.KEYWORK_ERROR[0], R.drawable.warning_icon,
                    mContext, false);
        }
        isInitList = true;
        lvItem.setAdapter(adapter);

        lvItem.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter1, View view,
                    int position, long id) {

                Intent intent = new Intent(mContext, NewsItemView.class);
                if (!adapter.getItem(position).getLink().get(position)
                        .equalsIgnoreCase("")) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(NewsItemView.ITEM_TEACHER,
                            adapter.getItemId(position));
                    bundle.putInt(NewsItemView.POSITION, position);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });
        return rootView;
    }

    private void loadDataFromServer(String URL) {
        LoadData loadData = new LoadData() {
            @Override
            protected void onPostExecute(ArrayList<ItemTeacher> result) {
                super.onPostExecute(result);
                updateListView(result);
            }
        };
        loadData.execute(URL);

    }
    boolean isInitList = false;
    protected void updateListView(ArrayList<ItemTeacher> result) {
        if (!isInitList) {
            data.addAll(result);
        }
        isInitList = false;
//  else{
//          isInitList = false;
//          adapter.clear();
//          adapter.addAll(result);
//      }
        adapter.clear();
        adapter.addAll(result);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        AnimateFirstDisplayListener.displayedImages.clear();
    }

    class LoadData extends AsyncTask<String, Void, ArrayList<ItemTeacher>> {

        @Override
        protected void onPreExecute() {
            pDialog = ProgressDialog.show(mContext, "", Utils.KEYWORK_ERROR[2]);
            data = new ArrayList<ItemTeacher>();
            super.onPreExecute();
        }

        @Override
        protected ArrayList<ItemTeacher> doInBackground(String... params) {
            data = Utils.getInformationTeacher(params[0]);
            return data;
        }

        @Override
        protected void onPostExecute(ArrayList<ItemTeacher> result) {
            if (result != null) {
                pDialog.dismiss();
            } else if (result == null) {
                pDialog.dismiss();
                Utils.MyToast(Utils.KEYWORK_ERROR[0], R.drawable.warning_icon,
                        mContext, false);
            }
            super.onPostExecute(result);
        }

    }

    class InfoTeacher extends ArrayAdapter<ItemTeacher> {

        private LayoutInflater inflater;
        ImageLoadingListener animateFirstListener;
        List<ItemTeacher> item = new ArrayList<ItemTeacher>();

        public InfoTeacher(Context context, List<ItemTeacher> objects) {
            super(context, -1, objects);
            this.item = objects;
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            animateFirstListener = new AnimateFirstDisplayListener();

        }

        @Override
        public View getView(int position, View convertView, ViewGroup arg2) {
            ItemTeacher itemTeacher = getItem(position);
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.teacher_layout, arg2,
                        false);
                holder.Name = (TextView) convertView
                        .findViewById(R.id.tv_NameTeacher);
                holder.School = (TextView) convertView
                        .findViewById(R.id.tv_School);
                holder.ic_naviga = (ImageView) convertView
                        .findViewById(R.id.ivNaviga);
                holder.icon = (ImageView) convertView
                        .findViewById(R.id.iv_IconTeacher);
                convertView.setTag(holder);
            } else
                holder = (ViewHolder) convertView.getTag();

            holder.Name.setText(itemTeacher.getName().get(position));
            holder.School.setText(itemTeacher.getSchool().get(position));
            return convertView;
        }

        @Override
        public int getCount() {
            return item.size();
        }

    }

    static class ViewHolder {
        ImageView icon;
        TextView Name;
        TextView School;
        ImageView ic_naviga;
    }

    private static class AnimateFirstDisplayListener extends
            SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

    public void customLoadMoreDataFromApi(int page) {
        loadDataFromServer(URL + page);
    }

}

And Object ItemTeacher

public class ItemTeacher {

    private ArrayList<String> name = new ArrayList<String>();
    private ArrayList<String> icon = new ArrayList<String>();
    private ArrayList<String> link = new ArrayList<String>();
    private ArrayList<String> school = new ArrayList<String>();


    public ItemTeacher(){

    }

    public ArrayList<String> getIcon() {
        return icon;
    }

    public ArrayList<String> getLink() {
        return link;
    }

    public ArrayList<String> getName() {
        return name;
    }

    public ArrayList<String> getSchool() {
        return school;
    }

    public void setIcon(ArrayList<String> icon) {
        this.icon = icon;
    }

    public void setLink(ArrayList<String> link) {
        this.link = link;
    }

    public void setName(ArrayList<String> name) {
        this.name = name;
    }

    public void setSchool(ArrayList<String> school) {
        this.school = school;
    }
}

并给我错误&#34; NullPointerException&#34;

01-02 06:26:23.105: E/InputEventReceiver(1281): Exception dispatching input event.
01-02 06:26:23.121: E/AndroidRuntime(1281): FATAL EXCEPTION: main
01-02 06:26:23.121: E/AndroidRuntime(1281): java.lang.IndexOutOfBoundsException: Invalid index 30, size is 30
01-02 06:26:23.121: E/AndroidRuntime(1281):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at java.util.ArrayList.get(ArrayList.java:304)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.readnews.main.InfoTeacherFragment$InfoTeacher.getView(InfoTeacherFragment.java:227)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.widget.AbsListView.obtainView(AbsListView.java:2143)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.widget.ListView.makeAndAddView(ListView.java:1831)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.widget.ListView.fillDown(ListView.java:674)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.widget.ListView.fillGap(ListView.java:638)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3087)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3361)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.View.dispatchTouchEvent(View.java:7246)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.View.dispatchPointerEvent(View.java:7426)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4342)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4382)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.Choreographer.doFrame(Choreographer.java:530)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.os.Handler.handleCallback(Handler.java:725)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.os.Looper.loop(Looper.java:137)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at java.lang.reflect.Method.invokeNative(Native Method)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at java.lang.reflect.Method.invoke(Method.java:511)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-02 06:26:23.121: E/AndroidRuntime(1281):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

袖手旁观,并且不确定我理解这个问题,但是,我不确定for循环应该在那里:

protected void updateListView(ArrayList<ItemTeacher> result) {
    for (int i = 0, n = result.size(); i < n; i++) {
        adapter.addAll(result);
    }
    adapter.notifyDataSetChanged();
}

我在这里更新了一个ArrayList。试试.clear():

myAdapter.clear();
myAdapter.addAll(myDisplayList);
myAdapter.notifyDataSetChanged();