1

I am bind the adapter with listview and at first I have fetch 10 records then when I scroll ends then again the items added in listview and it works perfect.

But when I added items after scroll end then at that time items are added successfully then the listview position set to top. I want to set that position to new added items.

Here is my codes:

onscroll function:

public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {               
    new items().execute();
}

Async function:

class items extends AsyncTask<String, String, String> {

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

    protected String doInBackground(String... args) {


        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("id", String.valueOf(id)));

        JSONObject json = jParser.makeHttpRequest(url, "GET",
                params);


        try {


            for (int i = 0; i < products.length(); i++) {

                JSONObject c = products.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_PID);
                String name = c.getString(TAG_NAME);

                Product item = new Product(name, imageurl);
                product_name.add(item);

            }

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

protected void onPostExecute(String file_url) {
    // dismiss the dialog after getting all products
    pDialog.dismiss();


    adapter = new CategoryAdapter(context, product_name);

    category_linear.setAdapter(adapter);

}

Baseadapter:

public class CategoryAdapter extends BaseAdapter {

    Context context;

    // protected List<String> m_List;
    protected List<Product> rowItems;

    public CategoryAdapter(Context context, List<Product> items) {
        this.context = context;
        this.rowItems = items;
    }

    public void addItemAll(List<Product> item) {
        //
        rowItems.addAll(item);
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return rowItems.size();
    }

    @Override
    public Object getItem(int position) {
        return rowItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

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

        // TODO Auto-generated method stub

        if (convertView == null) {
            convertView = LayoutInflater.from(getActivity()).inflate(
                    R.layout.row, null);

        }

        Product rowItem = (Product) getItem(position);

        TextView text = ViewHolderPattern.get(convertView, R.id.item_name);

        text.setText(rowItem.getTitle());

        return convertView;
    }
}

Now how can I set the position of scroll while new items is added.

currently when new items added then it will go to top of the listview.

But I want to set the position to the new items starting.

How can I do this?

madhan kumar
  • 1,560
  • 2
  • 26
  • 36
jack
  • 33
  • 6
  • Get position of item that is visible on top of list using getFirstVisiblePosition() in your onScroll call back. And set it again in onPostExecute() callback using setSelection() method. – Sachin Chandil Jul 23 '15 at 17:42

1 Answers1

0

You are creating a new instance of the adapter every time you execute the code in the AsyncTask. i.e in your onPostExecute function. Instead of doing

adapter = new CategoryAdapter(context, product_name);
category_linear.setAdapter(adapter);

You should be doing

adapter.addItemAll(product_name);
adapter.notifyDataSetChanged();

Also write the following

adapter = new CategoryAdapter(context, new List<Product>());
category_linear.setAdapter(adapter);

just after you instantiate your ListView to make it work.

The data will be updated in your list and your position will remain the same.

Cheers!

Swapnil
  • 31
  • 7
  • what's the error in logcat? Also can you try after removing notifyDataSetChanged() from addItemAll function in the adapter. – Swapnil Jul 23 '15 at 18:24
  • logcat : `fragment.CategoryFragment$items.onPostExecute(CategoryFragment.java:248) fragment.CategoryFragment$items.onPostExecute(CategoryFragment.java:1) at android.os.AsyncTask.finish(AsyncTask.java:632) at android.os.AsyncTask.access$600(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:193) ` – jack Jul 23 '15 at 18:27
  • updated the answer, please have a look if it solves the purpose. Also is it the whole error from the logcat? or you missed something from the top? – Swapnil Jul 23 '15 at 18:30
  • There should have been some exception listed somewhere like NullPointerException or something? – Swapnil Jul 23 '15 at 18:34
  • That means your adapter is coming null. Should be resolved if you instantiate your adapter in this fragment once in onViewCreated and assign that adapter to the listview. – Swapnil Jul 23 '15 at 18:41
  • You will need to update the relevant code of initialization of listview and adapter here. – Swapnil Jul 23 '15 at 19:40