Question

When end items in a ListView, i upload new, and after update adapter:

ListView lvMain = (ListView) findViewById(R.id.list);
boxAdapter = null;
boxAdapter = new BoxAdapter(this, products);
lvMain.setAdapter(boxAdapter);

But after this, elements are loaded but the scroll position the top. Ie the position of ListView is lost, and look again at the beginning of all

How fix it?

BoxAdapter code:

public class BoxAdapter extends BaseAdapter {
      private final Context ctx;
      private final LayoutInflater lInflater;
      private final ArrayList<ItemInfo> objects;
      private final int loadCount = 10;
      private int count = 10;
      private String name, desc;

      BoxAdapter(Context context, ArrayList<ItemInfo> products) {

        this.ctx = context;
        this.objects = products;
        this.lInflater = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }



      // кол-во элементов
      @Override
      public int getCount() {
        //return objects.size();
          return this.count;
      }

      // элемент по позиции
      @Override
      public ItemInfo getItem(int position) {
          return objects.get(position);
      }

      // id по позиции
      @Override
      public long getItemId(int position) {
        return position;
      }

      public void loadAdditionalItems() {
            this.count += this.loadCount;
            if (this.count > this.objects.size()) {
                this.count = this.objects.size();
            }
            notifyDataSetChanged();
      }



      @Override
      public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        view = lInflater.inflate(R.layout.item, parent, false);

        ItemInfo p = getItem(position);
        TextView desc_id = (TextView) view.findViewById(R.id.desc);

        if (p.username.contains("null"))
        {
            name = "Автор: Неизвестен";
        }
        else
        {
           name = "Автор: " + p.username;
        }

        if(!p.description.contains("null"))
        {
            desc = p.description.replaceAll("<br />", "");
            desc = desc.replaceAll("&quot;", "");
        }
        else
        {
            desc = "";
            desc_id.setVisibility(View.GONE);
        }


        ((TextView) view.findViewById(R.id.name)).setText(name);
        ((TextView) view.findViewById(R.id.desc)).setText(desc);
        return view;
      }

}

P.S setOnScrollListener code:

lvMain.setOnScrollListener(new OnScrollListener()
        {

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                    ListView lvMain = (ListView) findViewById(R.id.list);
                    if(firstVisibleItem + visibleItemCount >= totalItemCount) {
                        boxAdapter.loadAdditionalItems();
                        loading = false;
                    }
                    if (!loading && (lvMain.getLastVisiblePosition() + 10) >= (60))
                    {
                        new LoadLastestPost().execute();
                        loading = true;
                    }
            }

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

        });
Was it helpful?

Solution

The best solution would be to create a setProducts method in your boxAdapter and then just call boxAdapter.notifyDataSetChanged(). For example:

boxAdapter.setProducts(products);
boxAdapter.notifyDataSetChanged();

If you implement this method, there is no need to call lvMain.setAdapter(boxAdapter) more than once.

To add the setProducts() method to your adapter:

public BoxAdapter extends BaseAdapter {

    Context mContext;
    ArrayList<ItemInfo> objects;

    public BoxAdapter(Context context, ArrayList<ItemInfo> products) {
        mContext = context;
        objects = products;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // inflate and adjust view
    }

    public int getCount() {
        return objects.size();
    }

    public Object getItem(int position) {
        return objects.get(position);
    }

    public void setProducts(ArrayList<ItemInfo> newData) {
        objects = newData;
    }
}

Also, I wouldn't use a count variable. I would just use the size method in the ArrayList. I would remove count altogether.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top