eshteghel company
eshteghel company

Reputation: 471

Base Adapter Position on Scroll

I have this adapter that shows items from list retrieved from a web service, upon scrolling it's inflating the first position layout (it's considering the first visible item having position 0 which is not right), please help.

here's my adapter:

private class hwListArrayAdapter extends BaseAdapter {


    private ArrayList<HomeWork> Items;
    private final Activity context;

    public hwListArrayAdapter(Activity context, ArrayList<HomeWork> items) {

        this.context = context;
        this.Items = items;
    }

    @Override
    public int getCount() {
        return Items.size() + 1;
    }

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

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

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        View v = view;
        TextView desc, date, course;
        if (position == 0) {

            LayoutInflater vi;
            vi = LayoutInflater.from(context);
            v = vi.inflate(R.layout.homework_list_top, null);

            AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, (int) (MyApplication.screenHeight * 0.05));
            v.setLayoutParams(params);
        } else {
            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(context);
                v = vi.inflate(R.layout.homework_list_item, null);
            }
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, (int) (MyApplication.screenHeight * 0.6) / 8);
            v.setLayoutParams(params);


            desc = (TextView) v.findViewById(R.id.descriptionHW);
            desc.setTypeface(MyApplication.dinar);
            desc.setText(Items.get(position - 1).getDetails());

            course = (TextView) v.findViewById(R.id.courseHW);
            course.setTypeface(MyApplication.dinar);
            course.setText(Items.get(position - 1).getCourseName());

            date = (TextView) v.findViewById(R.id.dateHW);
            date.setTypeface(MyApplication.dinar);
            date.setText(dateConverter(Items.get(position - 1).getDueDate()));


        }
        return v;
    }
}

Upvotes: 1

Views: 401

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

you have to override getViewTypeCount and getItemViewType to get two different convertViews. E.g.

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return position == 0 ? 0 : 1;
}

getViewTypeCount says that you want to handle two different type of rows, and getItemViewType returns the actual type. Be aware that the return type of getItemViewType is used internally by android to address an array of View, so be sure to return always a value between 0 and getViewTypeCount() -1

Upvotes: 2

Related Questions