Amit Tumkur
Amit Tumkur

Reputation: 2835

Android linear layout set visibility in a listview custom adapter

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();

Upvotes: 0

Views: 1123

Answers (2)

Amit Tumkur
Amit Tumkur

Reputation: 2835

Found answer after looking into how other data was perfectly getting displayed. K after studying the type of data i was setting to adapter ie using arraylist with an Object class having getter and setter then i did this, hope this might help others facing same problem.

public class AgentsFeedData {
    private String bhk;
    private boolean visible = true;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getBhk() {
        return bhk;
    }

    public void setBhk(String bhk) {
        this.bhk = bhk;
    }

    public boolean isVisible() {
        return visibile;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }
}

the usage of code in adapter class

public class AgentsFeedAdapter extends BaseAdapter {
    private List<AgentsFeedData> agentsFeedDataList = new ArrayList<>();
    private Context mContext;

public AgentsFeedAdapter(Context mContext, List<AgentsFeedData> agentsFeedDataList) {
    this.mContext = mContext;
    this.agentsFeedDataList = agentsFeedDataList;
}

@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.agentChatBtn = (Button) convertView.findViewById(R.id.agentChatBtn);
        holder.agentConnectBtn = (Button) convertView.findViewById(R.id.agentConnectBtn);
        holder.chatLinearLayout = (LinearLayout) convertView.findViewById(R.id.chatLayout);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    final AgentsFeedData data = agentsFeedDataList.get(position);
    String uname = mContext.getSharedPreferences(Constansts.PREFERENCE_NAME, 0)
            .getString(Constansts.USER_ID, "");
    if (data.getUsername() != null) {
        if (data.getUsername().equals(uname)) {
            data.setVisible(false);
        }
    }

    if (!data.isVisible()){
        holder.chatLinearLayout.setVisibility(View.GONE);
    } else {
        holder.chatLinearLayout.setVisibility(View.VISIBLE);
    }
    return convertView;
}

public void swapItems(List<AgentsFeedData> agentsFeedDataList) {
    this.agentsFeedDataList = agentsFeedDataList;
    notifyDataSetChanged();
}

private class ViewHolder {
    TextView agentBhkTv;
    Button agentChatBtn;
    Button agentConnectBtn;
    LinearLayout chatLinearLayout;

    }
}

Upvotes: 0

N.T.
N.T.

Reputation: 2611

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.

Upvotes: 1

Related Questions