告诉Android onCreate()在方向改变时不打电话我遇到了麻烦.我已经添加android:configChanges="orientation"到我的清单中但仍然onCreate()调用了方向更改.这是我的代码.
AndroidManifest.xml中
<activity android:name="SearchMenuActivity" android:theme="@android:style/Theme.NoTitleBar" android:configChanges="orientation"></activity>
Run Code Online (Sandbox Code Playgroud)
SearchMenuActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the current layout to the search_menu
setContentView(R.layout.search_menu_activity);
Log.d(TAG, "onCreate() Called");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
//don't reload the current page when the orientation is changed
Log.d(TAG, "onConfigurationChanged() Called");
super.onConfigurationChanged(newConfig);
}
Run Code Online (Sandbox Code Playgroud)
和我的LogCat输出
06-23 12:33:20.327: DEBUG/APP(2905): onCreate() Called
//Orientation Changes
06-23 12:33:23.842: DEBUG/APP(2905): onCreate() Called
Run Code Online (Sandbox Code Playgroud)
有谁知道我做错了什么?谢谢.
嗨,我是Ruby on Rails的新手.我正在尝试创建一个小型博客网站.我有两张桌子帖子和评论.每篇博文都会有很多评论.我使用这些命令生成表.
rails g scaffold Post title:string body:text author:string
rails g scaffold Comment body:string author:string
Run Code Online (Sandbox Code Playgroud)
现在我想将关系添加到模型类中.我添加has_many :comments到Post类和belongs_to :postComment类.但是,当我尝试调用时,post.comments我得到一个运行时错误说SQLException: no such column: comments.post_id.我应该创建一个迁移并在Comment下添加post_id,还是有办法在脚手架中实现这个目的?
我正在尝试在Objective C中编写一个类方法.当我声明方法时,项目构建正常.但是每当我尝试调用该方法时,构建都会失败.这是我的代码.
头文件
#import <UIKit/UIKit.h>
@interface LoginViewController : UIViewController {
//Declare Vars
}
- (IBAction) login: (id) sender;
+ (NSString *) md5Hash:(NSString *)str;
@end
Run Code Online (Sandbox Code Playgroud)
源文件
+ (NSString *) md5Hash:(NSString *)str {
const char *cStr = [str UTF8String];
unsigned char result[16];
CC_MD5( cStr, strlen(cStr), result );
return [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
- (IBAction) login: (id) sender {
//Call the class method
[self md5Hash:@"Test"];
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个列表理解语句,如果它当前未包含在列表中,则只会添加一个项目.有没有办法检查当前正在构建的列表中的当前项?这是一个简短的例子:
输入
{
"Stefan" : ["running", "engineering", "dancing"],
"Bob" : ["dancing", "art", "theatre"],
"Julia" : ["running", "music", "art"]
}
Run Code Online (Sandbox Code Playgroud)
产量
["running", "engineering", "dancing", "art", "theatre", "music"]
Run Code Online (Sandbox Code Playgroud)
代码不使用列表理解
output = []
for name, hobbies in input.items():
for hobby in hobbies:
if hobby not in output:
output.append(hobby)
Run Code Online (Sandbox Code Playgroud)
我的尝试
[hobby for name, hobbies in input.items() for hobby in hobbies if hobby not in ???]
Run Code Online (Sandbox Code Playgroud) 我正在开发一个允许用户编辑实体列表的项目.我将这些实体映射到视图模型并使用编辑器字段显示它们.当用户按下提交按钮时,我会浏览每个模型并进行更新,如下所示:
foreach (var viewModel in viewModels)
{
//Find the database model and set the value and update
var entity = unit.EntityRepository.GetByID(fieldModel.ID);
entity.Value = viewModel.Value;
unit.EntityRepository.Update(entity);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码可以工作,但是你可以看到我们需要为每个实体命中两次数据库(一次检索,另一次更新).使用Entity Framework有更有效的方法吗?我注意到每个更新都会生成一个单独的SQL语句.循环结束后是否有提交所有更新的方法?
问题
我有一个用户可以编辑的字段列表.提交模型时,我想检查这些项是否有效.我不能使用数据表示法,因为每个字段都有不同的验证过程,直到运行时才会知道.如果验证失败,我使用ModelState.AddModelError(string key, string error)其中键是要添加错误消息的html元素的名称.由于有一个字段列表,Razor为html项生成的名称就像Fields[0].DisplayName.我的问题是有一种方法或方法从视图模型中获取生成的html名称的密钥?
试图解决方案
我toString()没有运气就尝试了密钥的方法.我也看了整HtmlHelper课,但没有看到任何有用的方法.
代码片段
查看模型
public class CreateFieldsModel
{
public TemplateCreateFieldsModel()
{
FreeFields = new List<FieldModel>();
}
[HiddenInput(DisplayValue=false)]
public int ID { get; set; }
public IList<TemplateFieldModel> FreeFields { get; set; }
public class TemplateFieldModel
{
[Display(Name="Dispay Name")]
public string DisplayName { get; set; }
[Required]
[Display(Name="Field")]
public int FieldTypeID { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
调节器
public ActionResult CreateFields(CreateFieldsModel model)
{
if (!ModelState.IsValid)
{
//Where do I …Run Code Online (Sandbox Code Playgroud) 我有一个调用的控制器方法Edit,用户可以编辑他们创建的数据,如此...
public ActionResult Edit(int id)
{
Submission submission = unit.SubmissionRepository.GetByID(id);
User user = unit.UserRepository.GetByUsername(User.Identity.Name);
//Make sure the submission belongs to the user
if (submission.UserID != user.UserID)
{
throw new SecurityException("Unauthorized access!");
}
//Carry out method
}
Run Code Online (Sandbox Code Playgroud)
这种方法工作正常,但是放入每个控制器的Edit方法都有点乱.每个表总是有一个,UserID所以我想知道是否有一种更简单的方法来通过[Authorize]属性或其他机制自动化这个代码更清洁.
我试图将我对SQL语句中的变量的所有引用移动到SqlParameter类,但由于某种原因,此查询失败.
string orderBy = Request.QueryString["OrderBy"];
//Fix up the get vars
if (orderBy == null)
orderBy = "name ASC";
string selectCommand = "SELECT cat_id AS id, cat_name AS name FROM table_name ORDER BY @OrderBy";
SqlCommand cmd = new SqlCommand(selectCommand, dataConnection);
cmd.Parameters.Add(new SqlParameter("@OrderBy", orderBy));
//Create the SQLDataAdapter instance
SqlDataAdapter dataCommand = new SqlDataAdapter(cmd);
//Create the DataSet instance
DataSet ds = new DataSet();
//Get data from a server and fill the DataSet
dataCommand.Fill(ds);
Run Code Online (Sandbox Code Playgroud)
这是错误
System.Data.SqlClient.SqlException:由ORDER BY编号1标识的SELECT项包含一个变量,作为标识列位置的表达式的一部分.只有在引用列名的表达式进行排序时,才允许使用变量.
它失败了.
dataCommand.Fill(ds);
Run Code Online (Sandbox Code Playgroud) 我希望在显示动画时禁用所有触摸屏交互.我不希望setClickable()在动画的开头或结尾处的按钮上使用该方法,因为有大量按钮.有什么建议?
c# ×4
asp.net-mvc ×3
android ×2
.net ×1
asp.net ×1
class-method ×1
clickable ×1
dictionary ×1
entity ×1
git ×1
git-svn ×1
java ×1
list ×1
methods ×1
modelstate ×1
objective-c ×1
orientation ×1
python ×1
ruby ×1
scaffolding ×1
security ×1
sql ×1
sql-server ×1