如何将新的Listview数据附加到无限滚动ListView

时间:2014-11-10 10:34:14

标签: android android-listview android-asynctask

我正在尝试在我的应用ListView中实现无限滚动,但我得到的结果是ListView正在填充新数据而不是附加。

我正在得到noclue什么都有...请帮助我...片段

MainActivity.java

public class MainActivity extends FragmentActivity{
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_near_me);
 }
  CityListFragment cityFragment = new CityListFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, cityFragment).commit();
}

CityListFragment.java

public class CityListFragment extends Fragment {

private List<City> city;
private ListView listView;
CityListAdapter adapter;
ProgressDialog mProgressDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new MyTask().execute();
}

 public class MyTask extends AsyncTask<String, Void, List<City>> {

    @Override
    protected List<City> doInBackground(String... urls) {
        try {
            city = serviceProvider.getService().getCities();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return city;
    }

    @Override
    protected void onPostExecute(List<City> organization) {
        adapter = new CityListAdapter(getActivity(), city);
        listView.setAdapter(adapter);
        // Create an OnScrollListener
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view,
                                             int scrollState) { // TODO Auto-generated method stub
                int threshold = 1;
                int count = listView.getCount();

                if (scrollState == SCROLL_STATE_IDLE) {
                    if (listView.getLastVisiblePosition() >= count
                            - threshold) {
                        // Execute LoadMoreDataTask AsyncTask
                        new LoadMoreDataTask().execute();
                    }
                }
            }
        });
    }
}

public class LoadMoreDataTask extends AsyncTask<String, Void, List<City>> {
    @Override
    protected List<City> doInBackground(String... urls) {
        try {
            // web service request
            city = serviceProvider.getService().getNextCities();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return city;
    }

    @Override
    protected void onPostExecute(List<City> organization) {
        int position = listView.getLastVisiblePosition();
        adapter = new CityListAdapter(getActivity(), city);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        listView.setSelectionFromTop(position, 0);
        mProgressDialog.dismiss();
    }
}

}

不是将新记录附加到List中,而是删除ListView中的旧记录 我遵循了Endless listview实现的这个特定链接

http://www.androidbegin.com/tutorial/android-parse-com-listview-load-more-using-onscrolllistener-tutorial/

2 个答案:

答案 0 :(得分:3)

这是因为您没有附加上一个列表。

您的代码应如下所示

    public class MyTask extends AsyncTask> {

        @Override 
        protected void onPreExecute() { 
            super.onPreExecute(); 
        } 

        @Override 
        protected List<City> doInBackground(String... urls) { 
            try { 
                List<City> city_new = serviceProvider.getService().getCities(); 
    if(city_new  != null && city_new.size() > 0 ){
    city.addAll(city_new  )
    }
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
            return city; 
        } 

        @Override 
        protected void onPostExecute(List<City> organization) { 
            adapter = new CityListAdapter(getActivity(), city); 
            listView.setAdapter(adapter); 
            // Create an OnScrollListener 
            listView.setOnScrollListener(new AbsListView.OnScrollListener() { 

                @Override 
                public void onScrollStateChanged(AbsListView view, 
                                                 int scrollState) { // TODO Auto-generated method stub 
                    int threshold = 1; 
                    int count = listView.getCount(); 

                    if (scrollState == SCROLL_STATE_IDLE) { 
                        if (listView.getLastVisiblePosition() >= count 
                                - threshold) { 
                            // Execute LoadMoreDataTask AsyncTask 
                            new LoadMoreDataTask().execute(); 
                        } 
                    } 
                } 
            }); 
        } 
    } 

    public class LoadMoreDataTask extends AsyncTask<String, Void, List<City>> {
        @Override 
        protected List<City> doInBackground(String... urls) {
            try { 
                // web service request 
                List<City> city_new = = serviceProvider.getService().getNextCities(); 
if(city_new  != null && city_new.size() > 0 ){
city.addAll(city_new  )
}
            } catch (IOException e) {
                e.printStackTrace();
            } 
            return city; 
        } 

        @Override 
        protected void onPostExecute(List<City> organization) {
            int position = listView.getLastVisiblePosition();
            adapter = new CityListAdapter(getActivity(), city); 
            listView.setAdapter(adapter); 
            adapter.notifyDataSetChanged(); 
            listView.setSelectionFromTop(position, 0);
            mProgressDialog.dismiss(); 
        } 
    } 
    }

enter code here

答案 1 :(得分:1)

  @Override 
        protected void onPostExecute(List<City> organization) {
            int position = listView.getLastVisiblePosition();
             previousCityList.addAll(city);**//add this line**
            adapter = new CityListAdapter(getActivity(), previousCityList); 
            listView.setAdapter(adapter); 
            adapter.notifyDataSetChanged(); 
            listView.setSelectionFromTop(position, 0);
            mProgressDialog.dismiss(); 
        }