• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Scroll adapter

Hi,

I have this data:
A
Abdas
Acdsa
B
Bdsa
Bfasdf
F
Fhgk
Fljgp
Z
Zjdh
Zpdh

letter(A,B,F,Z) are header_layout and other are item_layout
i have this adapter:
Code:
public class MyAdapter extends
        BaseAdapter {

    private Context mContext;
    private List<MyAdapterItem> mData;
    private LayoutInflater mInflater;


    public MyAdapter(Activity context) {
        this.mData = new ArrayList<MyAdapterItem>();
        this.mContext = context;
        mInflater = LayoutInflater.from(context);

    }

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

    public void addHeader(String title) {
        mData.add(new MyAdapterItem("", title, null, 0, true));

    }

    public void addItem(String id, String title, String subTitle, int icon) {
        mData.add(new MyAdapterItem(id, title, subTitle, icon, false));
        // add(new MyAdapterItem(id, title,
        // subTitle, icon, false));
    }

    public void addItem(MyAdapterItem itemModel) {
        mData.add(itemModel);
    }

    @Override
    public MyAdapterItem getItem(int position) {
        // TODO Auto-generated method stub
        return mData.get(position);
    }

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

    @Override
    public int getItemViewType(int position) {
        if (getItem(position).isHeader == true) {
            return 1;
        } else {
            return 0;
        }
    }

    public void setRows(List<MyAdapterItem> data)
    {
        this.mData = data;
    }

    public static class ViewHolder {
        ImageView mIcon;
        TextView mTitle;
        TextView mSubTitle;

    }

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

        ViewHolder holder = new ViewHolder();

        View view = convertView;

        if (view == null) {
            

            int layout = 0;
             if (getItemViewType(position) == 1){
                layout = R.layout.header_layout;
                view = mInflater.inflate(layout, parent, false);
                TextView txttitle = (TextView) view.findViewById(R.id.txt_title);
                holder.mTitle = txttitle;
            }
            else{
                layout = R.layout.item_layout;
                view = mInflater.inflate(layout, parent, false);
                TextView txttitle = (TextView) view.findViewById(R.id.txt_title);
                TextView txtSubTitle = (TextView) view.findViewById(R.id.txt_subtitle);
                ImageView imgIcon = (ImageView) view.findViewById(R.id.img_item);
                holder.mTitle = txttitle;
                holder.mSubTitle = txtSubTitle;
                holder.mIcon = imgIcon;
            }
            view.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) view.getTag();
        }

        MyAdapterItem item = getItem(position);
        if(item != null){
            holder.mTitle.setText(item.title);
            if (holder.mSubTitle != null) {
                holder.mSubTitle.setText(item.subTitle);
            }
            if (holder.mIcon != null) {
                if (item.icon != 0) {
                    holder.mIcon.setVisibility(View.VISIBLE);
                    holder.mIcon.setImageResource(item.icon);
                } else {
                    holder.mIcon.setVisibility(View.GONE);
                }
            }
        }
    
        return view;
    }

}
it it listed fine, but when i scroll my listview the layout of some item go to header_layout and some headers go to item_layout.

I have same error in my adapter?

thanks
 
Back
Top Bottom