我有一个线性布局,想动态添加可变数量的列表项。 (不能是recycler view,因为这个list已经在recycler view了,假设是一个小list。)但是不知为什么,除了最后一项,它没有保存我设置的文本。如果我添加了另一个标题不同的项目,那么将显示最后添加的项目,而其他项目将显示“默认”。有谁知道发生了什么事?这真的很奇怪。谢谢!
代码如下:
设置项.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#00000000"
android:text="Default"
android:layout_weight="1"/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end|center_vertical"
android:layout_weight="0"/>
</LinearLayout>
root_settings_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="5dp">
</LinearLayout>
设置 View .java:
public class SettingsView extends LinearLayout {
//Views
private View view;
/**
* Constructor for android tools to use.
*/
public SettingsView(Context context) {
super(context);
}
/**
*
* @param parent
*/
public SettingsView(ViewGroup parent) {
super(parent.getContext());
//Get the inflated layout
LinearLayout view = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.root_settings_view, parent, false);
View itemView = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView.findViewById(R.id.title)).setText("Hi");
((CheckBox) itemView.findViewById(R.id.checkbox)).setChecked(true);
View itemView2 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView2.findViewById(R.id.title)).setText("Hello");
((CheckBox) itemView2.findViewById(R.id.checkbox)).setChecked(false);
View itemView3 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView3.findViewById(R.id.title)).setText("Bye");
((CheckBox) itemView3.findViewById(R.id.checkbox)).setChecked(true);
View itemView4 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView4.findViewById(R.id.title)).setText("Test");
((CheckBox) itemView4.findViewById(R.id.checkbox)).setChecked(false);
addView(view);
}
最佳答案
感谢@Rod_Algonquin 的评论,这让我确信我必须为此制作一个小部件。幸运的是我以前做过小部件,所以我知道该怎么做。我只是想避免它,但我想如果我们不使用小部件,它就会出现重复 ID 的问题。
下面的代码让它工作:
添加了新类,SettingItemView.java:
/**
* A item view with the title and a checkbox.
*/
public class SettingItemView extends LinearLayout {
private TextView mTitle;
private CheckBox mCheckBox;
public SettingItemView(Context context) {
super(context);
inflate(context, R.layout.settings_item, this);
mTitle = (TextView) findViewById(R.id.title);
mCheckBox = (CheckBox) findViewById(R.id.checkbox);
}
public void setTitle(String title) {
mTitle.setText(title);
}
public void setCheckbox(boolean checked) {
mCheckBox.setChecked(checked);
}
}
改变了这个构造方法:
/**
*
* @param parent
*/
public SettingsView(ViewGroup parent) {
super(parent.getContext());
//Get the inflated layout.
LinearLayout view = LayoutInflater.from(getContext()).inflate(R.layout.root_settings_view, parent, false);
SettingItemView itemView = new SettingItemView(parent.getContext());
itemView.setTitle("Hi");
itemView.setCheckbox(true);
view.addView(itemView);
SettingItemView itemView2 = new SettingItemView(parent.getContext());
itemView2.setTitle("Hello");
itemView2.setCheckbox(true);
view.addView(itemView2);
SettingItemView itemView3 = new SettingItemView(parent.getContext());
itemView3.setTitle("Bye");
itemView3.setCheckbox(true);
view.addView(itemView3);
addView(view);
}
关于android - 以编程方式将 View 添加到 LinearLayout,而不是在除一个以外的所有 View 上设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41945677/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数