简体   繁体   English

Android通过ArrayAdapter刷新ListView

[英]Android Refresh ListView by ArrayAdapter

in simple ListView in my application i want to add items end of list when scrolling down is finished and refresh that. 在我的应用程序中的简单ListView中,我想在向下滚动完成时在列表末尾添加项目并刷新它。 my code only work to pass data into ListView and it does not working to add to end of list and refresh that. 我的代码只能将数据传递到ListView中,而无法添加到列表末尾并刷新它。 please help me to resolve this problem. 请帮助我解决此问题。 Thanks. 谢谢。

private ItemAdapter adapter;

public class ReceivedFragment extends ListFragment implements AbsListView.OnScrollListener {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        items = new ArrayList<Item>();

        for ( int i=0; i<=10; i++){
             String a = String.format("title %s", i);
             String b = String.format("description %s", i);
             Item item = new Item(a , b , String.valueOf(i) );
             items.add(item);
        }
        adapter = new ItemAdapter(getActivity(), items);
        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
     if (prevVisibleItem != firstVisibleItem) {
        if ((firstVisibleItem + visibleItemCount) == totalItemCount) {

            Toast.makeText(getActivity(), R.string.please_wait_to_response_from_server, Toast.LENGTH_LONG).show();

            Thread thread = new Thread() {
                @Override
                public void run() {
                    getActivity().runOnUiThread(new Runnable() {
                        public void run() {
                            for (int i = (int) getLastID(); i <= getLastID() + 10; i++) {
                                String a = String.format("title %s", i);
                                String b = String.format("description %s", i);
                                Item item = new Item(a, b, String.valueOf(i));
                                items.add(item);
                            }
                            adapter.notifyDataSetChanged();
                        }
                    });
                }
            };
        }
    }
}

    @Override
    public void onListItemClick(ListView l, View v, int position, long thisID) {
        super.onListItemClick(l, v, position, thisID);
    }
    public long getLastID() {
        return Long.parseLong(items.get(items.size() - 1).getmLastID());
    }
}

ArrayAdapter: ArrayAdapter:

public class ItemAdapter extends ArrayAdapter<Item> {

    public ItemAdapter(Context c, List<Item> items) {
        super(c,0,items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemView itemView = (ItemView)convertView;
        if (null == itemView)
            itemView = ItemView.inflate(parent);

        itemView.setItem(getItem(position));

        itemView.setBackgroundColor(((position % 2)!=0) ? Color.rgb(229, 229, 229) : Color.WHITE);
        return itemView;
    }

}
    ArrayAdapter adapter = (ArrayAdapter)yourListView.getAdapter();

  ArrayList<Item>  items = new ArrayList<Item>();
                 for ( int i= (int) getLastID(); i<= getLastID() + 10; i++){

            String a = String.format("title %s", i);
            String b = String.format("description %s", i);

                    Item item = new Item(a , b , String.valueOf(i) );
                    items.add(item);

                }
        adapter.addAll(items);
        adapter.notifyDataSetChanged();

To load your ListView with new rows, you should call to setAdapter after your list changes. 要用新行加载ListView ,您应该在列表更改后调用setAdapter

Instead of : 代替 :

new ItemAdapter(getActivity(), items).notifyDataSetChanged();

I would try 我会尝试

my_listview.setAdapter(new ItemAdapter(getActivity(), items); //items must contains the old and the new items

Or just 要不就

setAdapter(new ItemAdapter(getActivity(), items); 

As you are using a ListFragment. 当您使用ListFragment时。 It is possible that you may need also to call notifyDataSetChanged() , but I am not completely sure 您可能还需要调用notifyDataSetChanged() ,但是我不确定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM