Android 向ListView添加可变数量的标记
全新的Android编程,只是闲逛 我已经建立了一个列表视图来填充帖子。我已经成功加载了JSON,可以用标题、帖子内容和时间戳填充自定义布局 问题是我不知道如何添加标签(类似于下面Zite应用程序的屏幕截图,如“波斯尼亚”、“可视化”、“安全”)。我最多要添加三个标签(位置、学校、朋友),所以我在布局中放置了三个按钮。当然,我可以用传入的JSON更改按钮文本,但不是每个项目都有全部三个标记(例如,一个项目只有位置,而另一个项目有位置和学校)。所以我希望按钮只在有文本的时候出现,我会用重力迫使它们离开 我的第一个想法是嵌套的listview。互联网告诉我这很糟糕。尽管我进行了搜索,但没有找到任何教程或建议但这感觉像是一个已知的问题,对学习资源有什么建议吗? 下面是我的相关XML和javaAndroid 向ListView添加可变数量的标记,android,xml,json,listview,tags,Android,Xml,Json,Listview,Tags,全新的Android编程,只是闲逛 我已经建立了一个列表视图来填充帖子。我已经成功加载了JSON,可以用标题、帖子内容和时间戳填充自定义布局 问题是我不知道如何添加标签(类似于下面Zite应用程序的屏幕截图,如“波斯尼亚”、“可视化”、“安全”)。我最多要添加三个标签(位置、学校、朋友),所以我在布局中放置了三个按钮。当然,我可以用传入的JSON更改按钮文本,但不是每个项目都有全部三个标记(例如,一个项目只有位置,而另一个项目有位置和学校)。所以我希望按钮只在有文本的时候出现,我会用重力迫使它们
<TextView
android:id="@+id/feed_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="20dp"
android:text="@string/fTitle"
/>
<TextView
android:id="@+id/feed_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="2dp"
android:layout_below="@+id/feed_title"
android:textSize="15dp"
android:text="@string/fContent"
/>
<TextView
android:id="@+id/feed_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="2dp"
android:layout_below="@+id/feed_content"
android:textSize="10dp"
android:text="@string/fTime"/>
<ImageView
android:id="@+id/feed_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginRight="25dp"
android:layout_below="@+id/feed_content"
android:layout_alignParentRight="true"
android:layout_alignBottom="@+id/feed_time"
android:src="@drawable/ic_launcher"
/>
<ImageView
android:id="@+id/feed_flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/feed_content"
android:src="@drawable/ic_launcher"
android:layout_alignBottom="@+id/feed_like"
android:layout_toLeftOf="@+id/feed_like" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left"
android:layout_below="@+id/feed_time"
>
<Button
android:id="@+id/feed_tag_Locale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="@string/fTagLocal"
android:textSize="15dp"
/>
<Button
android:id="@+id/feed_tag_Network"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fTagNetwork"
android:textSize="15dp"
/>
<Button
android:id="@+id/feed_tag_Friend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fTagFriend"
android:textSize="15dp"
/>
</LinearLayout>
在getView的末尾,更改以下内容:
holder.localeButton.setText(postLocale);
holder.networkButton.setText(postNetwork);
holder.friendButton.setText(postFriend);
为此:
if(!postLocale.matches("")){
holder.localeButton.setText(postLocale);
}
else{
holder.localeButton.setVisibility(View.GONE);
}
if(!postNetwork.matches("")){
holder.networkButton.setText(postNetwork);
}
else{
holder.networkButton.setVisibility(View.GONE);
}
if(!postFriend.matches("")){
holder.friendButton.setText(postFriend);
}
else{
holder.friendButton.setVisibility(View.GONE);
}
没有不同的布局,根据数据类型展开适当的布局检查这可能会有所帮助
if(!postLocale.matches("")){
holder.localeButton.setText(postLocale);
}
else{
holder.localeButton.setVisibility(View.GONE);
}
if(!postNetwork.matches("")){
holder.networkButton.setText(postNetwork);
}
else{
holder.networkButton.setVisibility(View.GONE);
}
if(!postFriend.matches("")){
holder.friendButton.setText(postFriend);
}
else{
holder.friendButton.setVisibility(View.GONE);
}