Skip to content

Adding Static Header View with list view sections #42

@karwagaurav

Description

@karwagaurav

Hi,

I have successfully implemented Section header with Listview. Now i want to add a static listview to this list using listview.setHeaderVIew( headerview). [ Outstanding [green] and Advance [red] Part in screen shot]

screen shot 2017-06-05 at 2 24 41 pm

Code Snippet - ` Listview fragment -

@OverRide
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

    if (rootview != null) {
        ViewGroup parent = (ViewGroup) rootview.getParent();
        if (parent != null)
            parent.removeView(rootview);
    }
    try {
        rootview = inflater.inflate(R.layout.fragment_customer_list, container, false);
    } catch (InflateException e) {
    /* map is already there, just return view as it is  */
    }


    mCustomerListHeaderAdapter = new CustomerListHeaderAdapter(getActivity(), null);
   mUserListView = (ListView) rootview.findViewById(R.id.lv_user);
    View emptyview = rootview.findViewById(R.id.fcl_tv_empty_list);

    View headerView = inflater.inflate(R.layout.customer_list_header, mUserListView, false);
    mUserListView.addHeaderView(headerView , null, false);

    mUserListView.setEmptyView(emptyview);
    mUserListView.setAdapter(mCustomerListHeaderAdapter);

    mUserListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            int cursorPosition ;
          
               CustomerListHeaderAdapter adapter = (CustomerListHeaderAdapter) ((HeaderViewListAdapter) adapterView.getAdapter()).getWrappedAdapter();
        /*   This is what I do without Listview header
 CustomerListHeaderAdapter adapter = (CustomerListHeaderAdapter)  adapterView.getAdapter();

*/
cursorPosition = adapter.getCursorPositionWithoutSections(position);

            if (cursorPosition != SectionCursorAdapter.NO_CURSOR_POSITION) {
                // Handle the cursor item being clicked on.
                //      Log.i("SCA", "CursorPosition: " + cursorPosition);

                Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);
                if (cursor != null) {

                    Intent intent = new Intent(getActivity(), CustomerDetail.class);
                    intent.putExtra(Utility.CUSTOMER_EXTRA_TAG, cursor.getInt(COL_CUSTOMER_ID));
                    startActivity(intent);
                }

                mPosition = position;

}

CustomerListHeaderAdapter -

Context mContext ;
public CustomerListHeaderAdapter(Context context, Cursor cursor) {

    super(context, cursor, 0, R.layout.list_item_header_group, R.layout.list_item_customer);
    mContext = context ;
}

@Override
protected String getSectionFromCursor(Cursor cursor) {
    String name =  cursor.getString(CustomerListFragment.COL_FULL_NAME) ;
    return  name.substring(0, 1).toUpperCase();
}

@Override
protected SectionViewHolder createSectionViewHolder(View sectionView, String section) {
    return new SectionViewHolder(sectionView);
}

@Override
protected void bindSectionViewHolder(int position, SectionViewHolder sectionViewHolder, ViewGroup parent, String section) {
    sectionViewHolder.textHeaderView.setText(section);
}


@Override
protected CustomerListItemViewHolder createItemViewHolder(Cursor cursor, View itemView) {
    return new CustomerListItemViewHolder(itemView);
  //  return new CustomerListItemViewHolder(itemView);
}

@Override
protected void bindItemViewHolder(CustomerListItemViewHolder customerListItemViewHolder, Cursor cursor, ViewGroup parent) {

// Context context = customerListItemViewHolder.rootView.getContext();

   /* Double billed_amount = cursor.getDouble(CustomerListFragment.COL_BILLED_AMOUNT);
    customerListItemViewHolder.billAmtView.setText(Utility.format_amount_in_cur(mContext, billed_amount));

*/
String fullname = cursor.getString(CustomerListFragment.COL_FULL_NAME);
customerListItemViewHolder.nameView.setText(fullname);

/*
Double payment_amount = cursor.getDouble(CustomerListFragment.COL_PAYMENT_AMOUNT);
customerListItemViewHolder.paymentAmtView.setText(Utility.format_amount_in_cur(mContext, payment_amount));
*/

    //    Double balance_amount = billed_amount - payment_amount ;
    Double balance_amount = cursor.getDouble(CustomerListFragment.COL_BALANCE_AMOUNT);
    customerListItemViewHolder.balanceAmtView.setText(Utility.format_amount_in_cur(mContext, balance_amount));

    customerListItemViewHolder.balanceAmtView.setTextColor( Utility.get_color_for_tv(balance_amount , mContext));

    TextDrawable drawable = Utility.getIconResourceForName(fullname);
    customerListItemViewHolder.iconView.setImageDrawable(drawable);

}


@Override
protected int getMaxIndexerLength() {
    return 1;
}

`

But adding this, I am not able to click the item above section header [ List item - 40/a Dher ke Bal]
and if i click section header, app is crashing. [ Section Header - A ]

java.lang.ClassCastException: java.lang.String cannot be cast to android.database.Cursor at com.merapaper.merapaper.CustomerListFragment$1.onItemClick(CustomerListFragment.java:159)

( In my view, This is happening because SectionCursorAdapter.NO_CURSOR_POSITION is giving in correct result after adding static header view).

Could you please tell how to fix it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions