Android linelayout defines visibility in a custom listview adapter

advertisements

last time i had problem with setting radio button checked of particular row of custom adapter because of listview recycling of view after scrolling up / down and i had solved it using set tag and get tag to the radio button, but this time i am trying this for a linear layout having 2 buttons to set visibility but m ending up with crash.Reason being is i am not unable to cast integer to a linear layout, so please help me how to work with linear layout.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = View.inflate(mContext, R.layout.list_item_agents_feed, null);
        holder.agentBhkTv = (TextView) convertView.findViewById(R.id.feedBhkTv);
        holder.chatLinearLayout = (LinearLayout) convertView.findViewById(R.id.chatLayout);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    final AgentsFeedData data = agentsFeedDataList.get(position);
    holder.agentChatBtn.setTag(position);
    if (data.getUsername() != null) {
        if (data.getUsername().equals(uname)) {
            holder.chatLinearLayout = (LinearLayout) holder.agentBhkTv.getTag();
            holder.chatLinearLayout.setVisibility(View.GONE);
        }
    }

crash log is

08-03 22:09:58.603  28409-28409/in.pm.android E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: java.lang.Integer cannot be cast to android.widget.LinearLayout
        at in.pm.android.adapter.AgentsFeedAdapter.getView(AgentsFeedAdapter.java:103)
        at android.widget.AbsListView.obtainView(AbsListView.java:2627)
        at android.widget.ListView.makeAndAddView(ListView.java:1852)
        at android.widget.ListView.fillDown(ListView.java:682)
        at android.widget.ListView.fillFromTop(ListView.java:748)
        at android.widget.ListView.layoutChildren(ListView.java:1653)
        at android.widget.AbsListView.onLayout(AbsListView.java:2447)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)

the line where it crashes is

holder.chatLinearLayout = (LinearLayout) holder.agentBhkTv.getTag();


You don't need the holder.chatLinearLayout = (LinearLayout) holder.agentChatBtn.getTag(); line at all. If on the convertView == null case -> you are setting it right there, a few lines above. If on the else branch, your holder.chatLinearLayout is already being set when you created the view in the first place. Just remove that whole line.