Domanda

What I want to do is to append a list item at the bottom Just Like "Load More" functionality.

What I've performed i appended an extra items with empty values into adapter for ArrayList.

It's working fine for the first time but when i load more items it's adding Load More ItemView two times after every 15 items While i didn't make any changes into original ArrayList. The Changes has been made only into adapter class's constructor.

Here's the adapter class:

public class ContactListAdaptor extends BaseAdapter {

private Context context;
private ArrayList<DirectroyDetails> items = new ArrayList<DirectroyDetails>();

private Activity activity = null;
boolean isSearch = false;
private View currentView = null;
private String categoryName = null;

private HashMap< String, String> bannerHash = new HashMap<String, String>();

ContactList ACTIVITY;
public ContactListAdaptor(Context context, final ArrayList<DirectroyDetails> items2, Activity activity, String categoryName, HashMap< String, String> bannerHash, ContactList ACTIVITY, boolean expand) {
    // super(context, textViewResourceId, items);
    items = items2;

    if(expand)
    {
        DirectroyDetails dummy = new DirectroyDetails();
        dummy.setId("-1");
        items.add(dummy);
    }

    this.context = context;
    this.activity = activity;
    this.categoryName = categoryName;
    this.bannerHash = bannerHash;
    this.ACTIVITY = ACTIVITY;
}

/*public ContactListAdaptor(Context context, Vector<Directory> items,
        Activity activity, boolean isSearch) {
    // TODO Auto-generated constructor stub
    this.items = items;
    this.context = context;
    this.activity = activity;
    this.isSearch = isSearch;
}*/

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

    final DirectroyDetails directroyDetails = items.get(position);
    System.out.println("id======="+directroyDetails.getId().toString());
    try {
        if(convertView == null)
        {
            System.out.println("##THE POSITION IS=="+position);
            LayoutInflater vi = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.item_directory, null);
            if(directroyDetails.getId().toString().equals("-1"))
                convertView.setId(-1);
            else
                convertView.setId(1);
        }else {
            currentView = convertView;  
        }

        //To fatch the values from the Event object
        TextView titleText = (TextView)convertView.findViewById(R.id.title);
        TextView nameText = (TextView)convertView.findViewById(R.id.name);
        TextView numberText = (TextView)convertView.findViewById(R.id.number);
        ImageView profilePic = (ImageView)convertView.findViewById(R.id.eventimage);

        System.out.println("the position is ==== " +position);

        titleText.setText(directroyDetails.getCompanyname());

        nameText.setText(directroyDetails.getFirstname() + " " + directroyDetails.getSurname());

        numberText.setText(directroyDetails.getMobile());

        System.out.println("id at position======="+directroyDetails.getId().toString()+"  at=="+position);
        RelativeLayout overlay = (RelativeLayout)convertView.findViewById(R.id.overlay);

        if(directroyDetails.getId().toString().equals("-1"))
        {   
            overlay.setVisibility(View.VISIBLE);
            System.out.println("at here to make the Overl;ay visible");
        }else {
            overlay.setVisibility(View.INVISIBLE);
        }

    } catch (Exception e) {
        // TODO: handle exception
        System.out.println("the exception occurs at:-" + e.getMessage());
    }


    return convertView;
}

public int getCount() {
    System.out.println("the item size====="+items.size());
    return items.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}
}

private void onScrollUpdateList() {
    // TODO Auto-generated method stub
    Handler onScrollList = new Handler();
    onScrollList.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            setContactList();
        }
    }, 200);
}

& here is my calling code:-

  if(contactDb.size()>0 && contactDb!=null)
        {
            System.out.println("theh responseData1 before new  additions==="+responseData1.size());
            responseData1 = addMoreData(contactDb,responseData1);
            System.out.println("theh  responseData1==="+responseData1.size());
            if(responseData1 != null)
            {
                if(responseData1.size()>0)
                {

                    ContactListAdaptor contactAdaptor = new ContactListAdaptor(getApplicationContext(), responseData1, this, CategoryName, bannerHash, ContactList.this, expand);
                    contactAdaptor.notifyDataSetChanged();
                    listview.setAdapter(contactAdaptor);

                    try {
                        listview.setSelection(getSelectedItems());
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println("the exception at scrolling and set :- " + e.getMessage());
                    }

                }else
                {
                    Toast.makeText(ContactList.this, "No Item found.", Toast.LENGTH_LONG).show();
                }

            }}
È stato utile?

Soluzione

Please review below link code and try to resolve your issue with load more data dynamically with list view..

http://www.developerfusion.com/article/145373/android-listviews-with-dynamic-data/

If you have any query then let me know..

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top