0

i am using a listview with baseadapter and on scroll of the listview it crashes.


My getView method of the BaseAdapter class

    @Override
public View getView(final int arg0, View arg1, ViewGroup arg2) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = activity.getLayoutInflater();
    ViewHolder holder=null;

    if(arg1 == null){

        //Login category
        if(categoryList.get(arg0).getType()==1){
            arg1 = inflater.inflate(R.layout.switch_profile_header, null);
            holder=new ViewHolder();
            holder.layout=(RelativeLayout)arg1.findViewById(R.id.switch_profile_header_view);
            holder.categoryName =(TextView)arg1.findViewById(R.id.categoryName);
            holder.isLogin=categoryList.get(arg0).getIsLogin();
            arg1.setTag(holder);
        }
        //Other category
        else if(categoryList.get(arg0).getType()==0){
            arg1 = inflater.inflate(R.layout.switch_profile_listrow, null);
            holder=new ViewHolder();
            holder.layout=(RelativeLayout)arg1.findViewById(R.id.switch_profile_listrow_view);
            holder.categoryName =(TextView)arg1.findViewById(R.id.categoryName);
            holder.isLogin=categoryList.get(arg0).getIsLogin();
            holder.isDelete=(ImageView)arg1.findViewById(R.id.categoryDelete);
            holder.login=(ImageView)arg1.findViewById(R.id.categoryLogin);
            arg1.setTag(holder);
        }


    }else {
        holder = (ViewHolder) arg1.getTag();
    }
    //Login category
    if(categoryList.get(arg0).getType()==1){
        holder.categoryName.setText(Html.fromHtml(categoryList.get(arg0).getCategoryName()));
        holder.layout.setBackgroundColor(Color.parseColor("#efefef"));
    }
    //Login category
    else if(categoryList.get(arg0).getType()==0){
        if(flag==false){
            holder.isDelete.setVisibility(View.INVISIBLE);
        }else{
            holder.isDelete.setVisibility(View.VISIBLE);
        }

        if(categoryList.get(arg0).getIsLogin().equalsIgnoreCase("1")){
            holder.isDelete.setVisibility(View.INVISIBLE);
            holder.login.setVisibility(View.INVISIBLE);
        }else if(flag) {
            holder.isDelete.setVisibility(View.VISIBLE);
            holder.login.setVisibility(View.VISIBLE);
        }

        holder.categoryName.setText(Html.fromHtml(categoryList.get(arg0).getCategoryName()));
        if(categoryList.get(arg0).getIsLogin().equalsIgnoreCase("1")){
            holder.layout.setBackgroundColor(Color.parseColor("#efefef"));
        }else{
            holder.layout.setBackgroundColor(Color.parseColor("#f0f0f0"));
        }


    return arg1;
}


My Logcat

 Process: com.Tiger.Tiger, PID: 14059
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setVisibility(int)' on a null object reference
        at com.TenderTiger.Adapter.SwitchProfileAdapter.getView(SwitchProfileAdapter.java:117)
        at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
        at android.widget.AbsListView.obtainView(AbsListView.java:2344)
        at android.widget.ListView.makeAndAddView(ListView.java:1864)
        at android.widget.ListView.fillDown(ListView.java:698)
        at android.widget.ListView.correctTooLow(ListView.java:1474)
Jeffy Lazar
  • 1,903
  • 13
  • 20
  • override `getViewTypeCount`, make it return 2 and run it again – Blackbelt May 06 '15 at 08:40
  • 1
    thanks a lot @Blackbelt, it is working fine now and also do i have to only implement the above method or anything as well ? – Jeffy Lazar May 06 '15 at 08:45
  • I don't know. For sure you have to implement the abstract methods of BaseAdapter. For the others is up to you. Do you have a specific problem? – Blackbelt May 06 '15 at 09:15
  • @Blackbelt thanks for your suggestion, it helped but i had to also implement `getItemViewType` method as well. you can check my answer below. – Jeffy Lazar May 07 '15 at 11:42

1 Answers1

0

As i had to inflate two different layouts in getView method.
As adviced by Blackbelt i had to implement getViewTypeCount method in the BaseAdapter class.
I did the above and it was working fine, but when i scroll it would force close.

we have to implement

getViewTypeCount and also getItemViewType to get it to working

1) getViewTypeCount method returns the total number of layouts
2) getItemViewTypemethod returns the which row to inflate with proper conditions.

so finally my code looks like this

private static final int TYPE_HEADER = 0;
private static final int TYPE_ROW = 1;

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

@Override
public int getItemViewType(int position) {
    return (categoryList.get(position).getType()==1)?TYPE_HEADER :TYPE_ROW;
}

and finally my getView method

    @Override
public View getView(final int position, View convertView, ViewGroup arg2) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = activity.getLayoutInflater();
    ViewHolder holder=null;
    if(convertView == null) {
        if (getItemViewType(position) == TYPE_HEADER) { // Header
            convertView = inflater.inflate(R.layout.switch_profile_header, null);
            holder = new ViewHolder();
            holder.layout = (RelativeLayout) convertView.findViewById(R.id.switch_profile_header_view);
            holder.categoryName = (TextView) convertView.findViewById(R.id.categoryName);
            holder.isLogin = categoryList.get(position).getIsLogin();
            convertView.setTag(holder);

        } else {
            //row
            convertView = inflater.inflate(R.layout.switch_profile_listrow, null);
            holder = new ViewHolder();
            holder.layout = (RelativeLayout) convertView.findViewById(R.id.switch_profile_listrow_view);
            holder.categoryName = (TextView) convertView.findViewById(R.id.categoryName);
            holder.isLogin = categoryList.get(position).getIsLogin();
            holder.isDelete = (ImageView) convertView.findViewById(R.id.categoryDelete);
            holder.login = (ImageView) convertView.findViewById(R.id.categoryLogin);
            convertView.setTag(holder);
        }
    }else {
        holder = (ViewHolder) convertView.getTag();
    }

    //Header
    if(getItemViewType(position) == TYPE_HEADER){

    }
    //row
    else if(getItemViewType(position) == TYPE_ROW){

    }

    return convertView;
}
Community
  • 1
  • 1
Jeffy Lazar
  • 1,903
  • 13
  • 20