小编Sur*_*jaj的帖子

更改Android ListActivity onListItemClick中项目的背景颜色

我知道这听起来很简单,对此有疑问.但它都不能解决我的问题.所以我们走了:

我希望ListActivity在用户点击它时更改列表项的背景颜色,并在用户再次单击时将其更改回原始颜色(即选择/取消选择项目排序)

我尝试使用getChildAt,如果我在一个屏幕上看到所有项目而不必滚动,它就能很好地工作.

码:

getListView().getChildAt(position).setBackgroundColor(Color.CYAN);
Run Code Online (Sandbox Code Playgroud)

当我在列表中有更多项目并且用户必须滚动它们时,问题就开始了.一旦项目的背景发生变化,当我滚动时,背景颜色会显示在新显示的项目上.此外,再次单击该项目时getChildAt(position)返回null(以及因此a NullPointerException).

任何人都可以帮我一个简单的代码,帮助我改变列表项的背景颜色?

提前致谢!

android listview listactivity

10
推荐指数
1
解决办法
1万
查看次数

Java Regex:如何匹配一个或多个空格字符

如何在Java正则表达式中匹配多个空格字符?

我有一个正在尝试匹配的正则表达式.当我有两个或更多空格字符时,正则表达式失败.

public static void main(String[] args) { 
    String pattern = "\\b(fruit)\\s+([^a]+\\w+)\\b"; //Match 'fruit' not followed by a word that begins with 'a'
    String str = "fruit apple"; //One space character will not be matched
    String str_fail = "fruit  apple"; //Two space characters will be matched
    System.out.println(preg_match(pattern,str)); //False (Thats what I want)
    System.out.println(preg_match(pattern,str_fail)); //True (Regex fail)
}

public static boolean preg_match(String pattern,String subject) {
    Pattern regex = Pattern.compile(pattern);
    Matcher regexMatcher = regex.matcher(subject);
    return regexMatcher.find();
}
Run Code Online (Sandbox Code Playgroud)

java regex

7
推荐指数
1
解决办法
4万
查看次数

将android holo主题中的默认蓝色更改为橙​​色(或其他任何内容)

我在我的应用程序中使用holo主题:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" />
Run Code Online (Sandbox Code Playgroud)

我想将默认的蓝色更改为橙​​色.我觉得这很简单.然而,经过一整天的谷歌搜索,我仍然没有一个适合我的解决方案.

(注意:这也应该适用于对话框.默认情况下,对话框标题和行是蓝色.)

假如默认是这样的:

来自蓝色

我希望是这样的:

到橙色

请帮忙.提前致谢!

编辑

我忘了提到我在http://jgilfelt.github.com/android-actionbarstylegenerator/上了解了风格生成器. 这就是我得到这些图像的地方.但我不想要所有这些,它有很多图像和其他东西,使应用程序的大小更大.如果有办法调整默认主题,请告诉我.

android themes styles

6
推荐指数
1
解决办法
1万
查看次数

Spring REST Controller的单元测试'Location'标题

在Spring REST Controller中创建资源后,我将在标题中返回它的位置,如下所示.

@RequestMapping(..., method = RequestMethod.POST)
public ResponseEntity<Void> createResource(..., UriComponentsBuilder ucb) {

    ...

    URI locationUri = ucb.path("/the/resources/")
        .path(someId)
        .build()
        .toUri();

    return ResponseEntity.created(locationUri).build();
}
Run Code Online (Sandbox Code Playgroud)

在单元测试中,我正在检查其位置如下.

@Test
public void testCreateResource(...) {
    ...
    MockHttpServletRequestBuilder request = post("...")
        .content(...)
        .contentType(MediaType.APPLICATION_JSON)
        .accept(MediaType.APPLICATION_JSON);

    request.session(sessionMocked);

    mvc.perform(request)
        .andExpect(status().isCreated())
        .andExpect(header().string("Location", "/the/resources" + id);
}
Run Code Online (Sandbox Code Playgroud)

此结果案例失败,并显示以下消息.

java.lang.AssertionError: Response header Location expected:</the/resources/123456> but was:<http://localhost/the/resources/123456>
Run Code Online (Sandbox Code Playgroud)

好像我必须为http://localhost期望的Location头提供上下文前缀.

  • 硬编码上下文是否安全?如果是这样,为什么?
  • 如果没有,为测试用例正确生成它的正确方法是什么?

rest testng spring mockmvc

6
推荐指数
2
解决办法
2615
查看次数

具有多个相等条件的 Java 集

我有一个特殊的要求,我需要根据相等标准的组合对对象列表进行重复数据删除。

例如,两个Student对象在以下情况下相等:
1. firstName 和 id 相同或 2. lastName、class 和 emailId 相同

我打算使用 aSet来删除重复项。但是,有一个问题:
我可以覆盖该equals方法,但该hashCode方法可能不会为两个相等的对象返回相同的哈希码。

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;

    Student other = (Student) obj;
    if ((firstName.equals(other.firstName) && id==other.id) ||
            (lastName.equals(other.lastName) && class==other.class && emailId.equals(other.emailId ))
        return true;        
    return false;
}
Run Code Online (Sandbox Code Playgroud)

现在,我无法hashCode以某种方式覆盖方法,即根据此equals方法为两个相等的对象返回相同的哈希码。

有没有办法根据多个平等标准进行重复数据删除?我考虑使用 aList然后使用该contains方法来检查元素是否已经存在,但这会增加复杂性,因为包含在 O(n) …

java hash set hashcode deduplication

5
推荐指数
1
解决办法
1139
查看次数

Spring @DateTimeFormat 正在更改时区,同时将 @PathVariable 转换为 Date

@PathVariable使用@DateTimeFormat. 自动转换完成,但由于某种原因,日期被转换为我过去减去四小时的日期(复活节夏令时,服务器和客户端都位于那里)。

例子:

URL:.../something/04-10-2016/somename将产生值为 2016/10/03 20:00:00 的 someDate 对象(即前一天的晚上 8 点,比我通过的日期晚 4 小时。)

如何避免这种自动时区转换。我不需要任何时区信息,这种转换打破了我预期的行为。

这是我的代码:

@RequestMapping(value = "/something/{someDate}/{name}", method = RequestMethod.GET, headers = "Accept=application/json")
@ResponseBody
public SomeObject getDetails(@PathVariable @DateTimeFormat(pattern = "dd-MM-yyyy") Date someDate,
    @PathVariable String name) {
    // date here comes as GMT - 4
    // more code here
    // return someObject;
}
Run Code Online (Sandbox Code Playgroud)

现在,我通过将路径变量作为字符串读取并使用 SimpleDateFormatter 手动将字符串转换为日期来解决这个问题。

关于如何在没有时区转换的情况下进行自动转换工作的任何想法?

java rest spring spring-mvc datetime-format

5
推荐指数
2
解决办法
9518
查看次数

将表示二进制的字符串转换为二进制python

我无法完全找到解决方案。

到目前为止,我所做的基本上是创建一个字符串,该字符串表示填充以显示所有 8 位字符的 x 数量的二进制版本。

例如,如果 x = 2 那么我有 0101100110010001 所以总共 8 位数字。现在我有 2 个相同长度的字符串,我想将它们 XOR 在一起,但是 python 一直认为它是一个字符串而不是二进制。如果我使用 bin() 那么它会抛出一个不稳定的想法,认为它是一个字符串。因此,如果我转换为 int,它将删除前导 0。

所以我已经得到了我所追求的二进制表示,我只需要让 python 知道它是二进制的,有什么建议吗?

我用来创建二进制字符串的当前函数在这里

for i in origAsci:
    origBin = origBin + '{0:08b}'.format(i)
Run Code Online (Sandbox Code Playgroud)

提前致谢!

python binary

3
推荐指数
1
解决办法
7614
查看次数

如何确定我的数组是否为空

我需要帮助来检查我的阵列是否已满或者我是否可以在那里更多.我以为我可以通过执行以下操作来检查其中一个位置是否为空:

    for (int i =0; i <array.length; i++){
        if(array[i] == null){
            return false;
        }else{
            return true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

为了测试这个,我使用int创建了am数组并得到了错误:对于参数类型int,操作符==是未定义的,null是否有另外一种方法可以执行此操作?在我的原始任务中,我将使用对象而不是int来执行此操作,这是否意味着它将使用对象而不是int?

java arrays primitive-types

2
推荐指数
1
解决办法
1093
查看次数