【发布时间】:2015-03-22 13:55:58
【问题描述】:
我正在尝试根据列表视图中的特定条件设置按钮的可见性。
上下文:列表视图具有响应帖子的参数。它包含响应的标题、描述等以及投票按钮。只有作为父帖子所有者的用户才能看到该按钮,以便他可以对响应进行投票。
我试图设置按钮可见性的代码的 java 部分:
adapter= new SimpleAdapter(MainActivity.this, list,
R.layout.response_list, columns, mapping); //response_list is the xml layout file where response parameters are defined.
ListView listView = (ListView) findViewById(R.id.listallresponses); //listallresponses is the id of response_list layout file.
if (!parent.equals(userLoggedin)) { //"parent" is the userid of the parent post. "userLoggedin" is the current user who is viewing the parent post and its responses.
LayoutInflater li = LayoutInflater.from(this);
View v = li.inflate(R.layout.response_list, null, false);
Button upVoteButton = (Button) v
.findViewById(R.id.upvoteButton); //upvoteButton is the one whose visibility we are talking about.
upVoteButton.setVisibility(View.GONE);
}
listView.setAdapter(adapter);
我在其中定义响应参数的 response_list.xml 如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/responseList"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<!-- Other views are present here-->
<Button
android:id="@+id/upvoteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="upVoteResponse"
android:text="VoteUp"/>
问题:upvoteButton 在响应列表中始终可见,即使登录的用户不等于父帖子的所有者。想知道我怎样才能让它工作!提前致谢。
注意:我对 Android 的熟悉只有五个月。我已经搜索了很多以弄清楚如何使它工作,但直到现在才成功。
【问题讨论】:
-
在适配器本身中做同样的事情,在
getView内 -
您是否处理过视图回收?好像不是这样!
-
@Skynet :你的意思是 adapter.notifyDataSetChanged(); ?我试过了,如果是的话。
-
@MurtazaHussain :将在 getView 上阅读并尝试实施。很快就会更新帖子。
-
尝试自定义您的适配器并包含适配器的生命周期方法here 是一个很好的起点。
标签: android listview button android-listview visibility