上次我设置单选按钮的特定行的单选按钮检查有问题因为滚动上/下后的listview回收视图我用set tag解决了它并获取标签到单选按钮,但这次我我尝试这个线性布局有2个按钮来设置可见性,但最终崩溃。原因是我不能将整数转换为线性布局,所以请帮助我如何使用线性布局。
@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);
}
}
崩溃日志
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)
崩溃的行是
holder.chatLinearLayout = (LinearLayout) holder.agentBhkTv.getTag();
答案 0 :(得分:1)
根本不需要holder.chatLinearLayout = (LinearLayout) holder.agentChatBtn.getTag();
行。如果在convertView == null
情况下 - >你把它设置在那里,上面几行。如果在else分支上,则在您首先创建视图时已经设置了holder.chatLinearLayout
。只需删除整行。
答案 1 :(得分:0)
在查看其他数据如何完美显示之后找到答案。 K在研究了我设置为适配器的数据类型后,即使用带有getter和setter的Object类的arraylist然后我做了这个,希望这可以帮助其他人面对同样的问题。
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;
}
}
适配器类中代码的使用
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;
}
}