小编JMR*_*ies的帖子

正则表达式允许Java中的空格字符

嘿所有,所以我试图允许一些文本输入在发送之前通过正则表达式检查.我希望文本只包含AZ,0-9和空格""字符.这是我现在的代码:

if(!title.matches("[a-zA-Z0-9_]+") {
    //fail
}
else {
    //success
}
Run Code Online (Sandbox Code Playgroud)

然而,//fail当我输入"这是一个测试"之类的东西时,这仍然会给出结果

有任何想法吗?谢谢大家.

java regex

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

Android主屏幕小部件:RemoteViews setRemoteAdapter(...)方法不适用于API 11+

所以onUpdate方法调用

remoteViews.setRemoteAdapter(id, R.id.listview, intent)

为了将适配器应用于窗口小部件中的列表视图.

窗口小部件的标题中有一个按钮,可以更改列表视图显示的数据集(对于假设的电子邮件窗口小部件,请考虑收件箱,发件箱,已加星标等).当我单击该按钮时,它会将用户带到一个Activity,允许他们选择要显示的数据集.选择后,将运行以下代码:

Intent intent = new Intent(this, WidgetReceiver.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
intent.putExtra("notify", true);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {R.xml.widget_provider});
sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)

这成功调用了widget的AppWidgetProvider类中的onUpdate方法.但是,如果我要在不同类型的数据集之间切换,则在2-3个首选项更改之后,该setRemoteAdapter方法将停止运行.我已经广泛记录了这个过程,并且该方法应该调用一个服务,该服务反过来加载RemoteViewsService.RemoteViewsFactory类来填充小部件及其适配器,但不执行任何这些操作.您最初几次更改首选项,它按预期工作.但之后就退出了.

有没有人知道这里发生了什么?

android android-widget

9
推荐指数
1
解决办法
5115
查看次数

Android:在proguard中重复类错误

编译我的应用程序时,我收到以下错误(编辑出的敏感路径)

Execution failed for task ':app:proguardDebug'.
> java.io.IOException: Can't write [/projects/app/build/intermediates/classes-proguard/debug/classes.jar] (Can't read [/.gradle/caches/modules-2/files-2.1/commons-codec/commons-codec/1.4/4216af16d38465bbab0f3dff8efa14204f7a399a/commons-codec-1.4.jar(;;;;;;!META-INF/MANIFEST.MF)] (Duplicate zip entry [commons-codec-1.4.jar:org/apache/commons/codec/binary/Base64.class]))
Run Code Online (Sandbox Code Playgroud)

这向我表明,编译器会看到应用程序尝试使用commons.codec.binary.Base64.class作为依赖项的两个位置.我再次检查并检查了我的库,但只有一个库(Amazon AWS)正在尝试使用它.

在这个错误之上,我得到了一些其他的警告,这些警告也为我引起了一个红旗:

Warning:can't write resource [META-INF/LICENSE.txt] (Duplicate zip entry [commons-lang3-3.1.jar:META-INF/LICENSE.txt])
Warning:can't write resource [META-INF/NOTICE.txt] (Duplicate zip entry [commons-lang3-3.1.jar:META-INF/NOTICE.txt])
Warning:can't write resource [META-INF/LICENSE.txt] (Duplicate zip entry [commons-codec-1.4.jar:META-INF/LICENSE.txt])
Warning:can't write resource [META-INF/NOTICE.txt] (Duplicate zip entry [commons-codec-1.4.jar:META-INF/NOTICE.txt])
Run Code Online (Sandbox Code Playgroud)

我根本没有在我的应用程序中明确使用commons-codec-1.4或commons-lang3-3.1,以为我以后在使用lang3之前将其删除.为什么在编译日志中引用它们?我的一个maven图书馆可以使用它们吗?我将在gradle文件中包含下面的maven库列表.

这是我的proguard和gradle文件供参考:

ProGuard的

-keep class org.w3c.dom.bootstrap.** { *; }
-keep class org.joda.time.** { *; }
-keep class com.facebook.** { *; }
-keep class org.apache.commons.** { *; } …
Run Code Online (Sandbox Code Playgroud)

android proguard gradle

9
推荐指数
1
解决办法
5140
查看次数

Android AppWidgetManager方法updateAppWidget无法设置意图,加载数据.它随机发生

我的小部件由2个按钮和一个显示数据的列表视图组成.大多数情况下,当调用窗口小部件提供程序的onUpdate方法时,一切都正常加载,每个人都很高兴.

但是我注意到有时在调用update方法之后,widget完全无法加载其数据.列表视图为空,并且所有按钮都没有响应.就像我将布局初始化到窗口小部件中一样,但没有设置挂起的意图和列表的适配器.

我记录了一切,发现事实并非如此.每次都会创建挂起的意图,以及列表适配器,包括失败时的随机时间.很长一段时间以来,我认为它与列表数据如何填充到适配器中有关,但每次都看到它工作,再加上按钮意图不能正常工作的事实让我相信方法updateAppWidget (ComponentName,RemoteViews)是失败的.但是,没有错误堆栈可以帮助我确认这一点.

以下是在AppWidgetProvider的onUpdate方法调用的单独服务中运行的代码:

@SuppressWarnings("deprecation")
private void updateWidget(int[] ids) {

    AppWidgetManager widgetManager = AppWidgetManager.getInstance(this);
    int[] widgetIds = widgetManager.getAppWidgetIds(new ComponentName(this, WidgetReceiver.class));

    for (int currentWidgetId : widgetIds) {

        RemoteViews widget = new RemoteViews(this.getPackageName(), R.layout.widget_layout);

        Intent intent = new Intent(this, WidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, currentWidgetId);
        intent.putExtra("random", randomNumber);
        randomNumber++;
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

        widget.setRemoteAdapter(currentWidgetId, android.R.id.list, intent);

        Intent clickIntent = new Intent(this, DetailsActivity.class);
        PendingIntent pending = PendingIntent.getActivity(this, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        widget.setPendingIntentTemplate(android.R.id.list, pending);

        Intent settingsIntent = new Intent(this, WidgetSettingsActivity.class);
        settingsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent settingsPending = PendingIntent.getActivity(this, 0, settingsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        widget.setOnClickPendingIntent(R.id.widget_settings, settingsPending); …
Run Code Online (Sandbox Code Playgroud)

android android-appwidget

8
推荐指数
1
解决办法
4529
查看次数

新列的 SQLite 升级时列名重复

当用户在更新应用程序时升级到本地 SQLite 数据库的新版本时,我遇到了不一致的、不可重现的崩溃。

Fatal Exception: android.database.sqlite.SQLiteException: duplicate column name: upload_pending (code 1): , while compiling: ALTER TABLE purchases ADD COLUMN upload_pending TINYINT DEFAULT 0
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
    (duplicate column name: upload_pending (code 1): , while compiling: ALTER TABLE purchases ADD COLUMN upload_pending TINYINT DEFAULT 0)
#################################################################
Run Code Online (Sandbox Code Playgroud)

该列是该应用程序版本的新内容,它告诉我最可能的错误是 SQLiteOpenHelper 的 onUpgrade 方法被调用了两次。以下是处理升级的逻辑:

@Override   
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    for(int currentUpgrade = oldVersion + 1; currentUpgrade <= …
Run Code Online (Sandbox Code Playgroud)

sqlite android android-sqlite

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

Android:单击搜索时,ActionBar图标和导航下拉菜单之间的边距会发生变化

我有以下设置:

在发布时

因此,您可以单击操作栏的搜索图标以转到搜索栏.很标准:

搜索

因此,如果您已完成搜索,可以单击"主页"图标以使该栏消失:

回复

但是当你回去时,导航下拉菜单会向右移动很多:

错误!

我无法弄清楚这里发生了什么.我做的第一件事是创建一个全新的应用程序并重新创建它(这是你在下面看到的),以确保它不是我在主应用程序中使用的样式或主题的问题.这没有帮助解决问题.我正在使用ActionBarSherlock,所以我接下来要做的是使用Google的官方Action Bar API重建项目.这也没有什么不同.

有没有人有任何想法?我会提供你认为可以帮助的任何代码,但我从Google的文档中直接了解了它.谢谢!

编辑:添加一些带有布局边框的前后图像(4.1+中的开发选项功能)

之前:

好

后:

坏

我还包括菜单XML:

<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)

<item android:id="@+id/abm_search"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="always|collapseActionView"
    android:actionViewClass="android.widget.SearchView" />

<item android:id="@+id/abm_location"
    android:title="Stuff"
    android:showAsAction="never" />

<item android:id="@+id/abm_radius"
    android:title="More Stuff"
    android:showAsAction="never" />

<item android:id="@+id/abm_settings"
    android:title="Other Stuff"
    android:showAsAction="never"/>
Run Code Online (Sandbox Code Playgroud)

android android-actionbar

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

Android AppWidgetManager方法getAppWidgetIds返回空数组

我的应用程序在服务中更新,并首先执行此操作以获取小部件的ID:

AppWidgetManager widgetManager = AppWidgetManager.getInstance(this);
int[] widgetIds = widgetManager.getAppWidgetIds(new ComponentName(this, WidgetReceiver.class));
Run Code Online (Sandbox Code Playgroud)

有时,即使窗口小部件仍在主屏幕上,此方法也将返回一个空数组.我四处搜索,找不到任何相关信息.问题是它并不总是发生,只是有时候(尽管一旦看起来它在后续的更新尝试中不会成功的话就会发生).

有没有人碰到这个,更好的是找到了解决方案?非常感激.

android android-appwidget

6
推荐指数
0
解决办法
1517
查看次数

Android RecyclerView适配器:索引0处的notifyItemInserted和notifyItemMoved不起作用

我有一个带有水平线性布局管理器的RecyclerView,声明如下:

RecyclerView graph = (RecyclerView) findViewById(R.id.graph);

RecyclerView.LayoutManager classManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
graph.setLayoutManager(classManager);
graph.addItemDecoration(new ComponentDecorator(this)); //Just sets a margin around each item
Run Code Online (Sandbox Code Playgroud)

我有一个方法,将占位符视图插入到RecyclerView中,如下所示:

private void insertPlaceholder(int index) {
    int placeholderIndex = getIndexOfPlaceholder(); //returns index of existing placeholder, -1 if none

    //No need to do anything
    if(placeholderIndex == index)
        return;

    if(placeholderIndex == -1) {
        ClassGraphItem placeholder = new ClassGraphItem();
        placeholder.setType(ClassGraphItem.PLACEHOLDER);

        mItems.add(index, placeholder);
        Print.log("notify item inserted at index", index);
        notifyItemInserted(index);
    }
    else {
        ClassGraphItem placeholder = mItems.get(placeholderIndex);
        mItems.remove(placeholderIndex);
        mItems.add(index, placeholder);

        notifyItemMoved(placeholderIndex, …
Run Code Online (Sandbox Code Playgroud)

android

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

将.csvs插入Java中的SQLite数据库

大家好.所以我有一个csv文件的集合,我想在我的Java程序中作为表插入到sqlite DB中.我搜索了搜索SO,但我找不到任何关于如何将这些csv文件插入sqlite DB的教程.我正在使用这个sqlite包装器:Zentus SQLiteJDBC.

所以我们假设我有一个csv文件,其中包含以下内容:

1,John,Doe,5.0
2,Jane,Smith,5.0

我怎么能创建一个表并将这些值插入其中?

谁能指出我正确的方向?谢谢!

更新:好的所以我找到了这个指南:http://www.sqlite.org/cvstrac/wiki? p =进口文件,但语法让我有点困惑.特别:

sqlite> create table test (id integer, datatype_id integer, level integer, meaning text);    
sqlite> .separator ","    
sqlite> .import no_yes.csv test
Run Code Online (Sandbox Code Playgroud)

我理解这是做什么的,它说分隔符将是一个逗号,并将文件no_yes.csv导入表测试.但是,这就是我按照JDBC指南执行sql语句的方式:

Statement stat = conn.createStatement();
stat.executeUpdate("drop table if exists test;");
stat.executeUpdate("create table test (id, name, value);");
Run Code Online (Sandbox Code Playgroud)

我试着这样做来表示分隔线:

stat.executeUpdate("separator ','");
Run Code Online (Sandbox Code Playgroud)

stat.executeUpdate(".separator ','");
Run Code Online (Sandbox Code Playgroud)

但两个都给我一个错误.我该怎么做呢?谢谢!

java csv sqlite

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

动画ViewPager在Android中填充屏幕

所以我有一个看起来像这样的视图:

在此输入图像描述

我想要实现的是当用户在GridView上向下滚动时,整个视图向上移动以隐藏顶部的大图像.基本上,如果他们想要查看更多的gridview,则布局会向上滚动以增加gridview的大小.

但是当我真正这样做时,会发生以下情况:

在此输入图像描述

它工作正常,除了GridView不会增加大小以填充屏幕中的新空间,因此窗口的下半部分全部为白色,如您所见.

以下是我到目前为止所做的事情:

private void hideHeader(boolean animate) {
    if(animate) {
        AnimatorSet set = new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(mImageHeader, "translationY", -mImageHeader.getHeight()),
                ObjectAnimator.ofFloat(mShareButton, "translationY", -mImageHeader.getHeight()),
                ObjectAnimator.ofFloat(mIndicator, "translationY", -mImageHeader.getHeight()),
                ObjectAnimator.ofFloat(mViewPager, "translationY", -mImageHeader.getHeight())
        );

        set.setDuration(700l);
        set.start();
    }
    else {
        //TODO finish
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以从代码中看出,我正在使用ViewPager.在这种情况下,GridView实际上是一个片段,这是我第一次认为我的问题来自的地方.但是,不是ViewPager正在填充屏幕并且其内部的片段不是这样的情况,而是ViewPager本身不是垂直填充的:

在此输入图像描述

这是父片段的XML(包含大图像,Share按钮和viewpager +指示符):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:background="@android:color/white"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <include android:id="@+id/fp_abf"
             layout="@layout/action_bar_footer"/>

    <include android:id="@+id/fp_image_header"
             layout="@layout/widget_image_header"
             android:layout_below="@id/fp_abf"/>

    <TextView android:id="@+id/fp_share_button"
              android:layout_width="match_parent"
              android:layout_height="60dp"
              android:layout_below="@id/fp_image_header"
              android:gravity="center"
              android:background="@drawable/selectable_action_button_grey_to_bit_green"
              android:textSize="14dp"
              android:textColor="@android:color/white"
              android:text="@string/share_caps"/>

    <com.viewpagerindicator.TabPageIndicator android:id="@+id/fp_indicator"
                                             android:layout_width="match_parent"
                                             android:layout_height="wrap_content"
                                             android:layout_below="@+id/fp_share_button"
                                             android:background="@color/header_background"/>

    <android.support.v4.view.ViewPager android:id="@+id/fp_pager"
                                       android:layout_width="match_parent"
                                       android:layout_height="match_parent"
                                       android:layout_below="@id/fp_indicator"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我最初将它作为LinearLayout,但试图将其切换为相对,看看是否会产生任何差异(它没有).

正如您所看到的,ViewPager的layout_height为"match_parent",我认为它足以填充它可用的所有空间.我认为正在发生的事情是视图没有被告知它有更多的空间可以使用并因此重新绘制它的界限,但我不确定如何触发它自己. …

android android-animation

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