小编Use*_*er3的帖子

无法理解战略模式

我从这里开始关注战略模式的一个例子

教程中的所有内容都很清楚但是:

public class Context {
   private Strategy strategy;

   public Context(Strategy strategy){
      this.strategy = strategy;
   }

   public int executeStrategy(int num1, int num2){
      return strategy.doOperation(num1, num2);
   }
}
Run Code Online (Sandbox Code Playgroud)

因此Context类在其构造函数中需要一个Strategy参数.

战略的定义是:

public interface Strategy {
   public int doOperation(int num1, int num2);
}
Run Code Online (Sandbox Code Playgroud)

上面是一个接口,Context Class需要一个Strategy类型的对象.在StrategyPatternDemo类中,我们执行以下操作:

public class StrategyPatternDemo {
   public static void main(String[] args) {
      Context context = new Context(new OperationAdd());        
      System.out.println("10 + 5 = " + context.executeStrategy(10, 5));

      context = new Context(new OperationSubstract());      
      System.out.println("10 - 5 = " + context.executeStrategy(10, …
Run Code Online (Sandbox Code Playgroud)

java design-patterns

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

日期转换代码在不同机器上表现不同

我有以下代码:

package main

import (
    "fmt"
    "time"
)

func main() {
    loc := "Jan 06"
    date := "2020-12-31 16:00:00 +0000 UTC"
    layout := "2006-01-02 15:04:05 -0700 MST"
    t, err := time.Parse(layout, date)
    if err != nil {
        fmt.Print("Error")
    }

    fmt.Println(t)
    GetFormattedDate(t, loc)
}

func GetFormattedDate(date time.Time, layout string) (string, error) {
    
    fmt.Println("\n\n====================================================")
    fmt.Println("time.Now(): ", time.Now())
    fmt.Println("time.Now().Location(): ", time.Now().Location())
    fmt.Println("Converting: ", date)
    configLocale := "Singapore"
    fmt.Println("configLocale: ", configLocale)

    loc, err := time.LoadLocation(configLocale)
    fmt.Println("loc: ", loc)
    if err != nil {
        return …
Run Code Online (Sandbox Code Playgroud)

linux date go docker

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

尝试将日期YYYY-MM-DD转换为DD-MMM-YYYY,但该函数始终将1月作为月份返回.

我有以下函数来转换日期:

public String dateConvert(String D){

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-DD");
        SimpleDateFormat format2 = new SimpleDateFormat("dd-MMMM-yyyy");
        Date date = null;
        try {
            date = format1.parse(D);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String dateString = format2.format(date);
        dateString = dateString.replace("-", " "); 
        System.out.println(dateString);
        return ((dateString));
    }
Run Code Online (Sandbox Code Playgroud)

然而,无论何时,我都会将这个月转换为1月.我不明白我哪里错了!

java date

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

如何绘制自定义背景

我必须设计一个三角形并在其中显示一些角度为45的文本,我必须将文本视图放在三角形的边界之外以显示其他文本.它就像一面旗帜.但是,当我使用相对布局并放置三角形背景时,它仍然充当矩形,遮挡了我的文本视图.以下是我使用的代码:

<RelativeLayout
        android:id="@+id/relative"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/image_sticker" >

        <com.example.AngledTextView
            android:id="@+id/textViewx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:rotation="52"
            android:textColor="#FFFFFF" />
    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我的AngledTextView类:

public class AngledTextView extends TextView  {  

    private int mWidth;
    private int mHeight;


    public AngledTextView(Context context, AttributeSet attrs)  {  
        super(context, attrs);  

    }  



    @Override  
    protected void onDraw(Canvas canvas) {  
        canvas.save();  
        /*Paint textPaint = new Paint(); 
        int xPos = (canvas.getWidth() / 2);
        int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ; 

            canvas.rotate(45, xPos,yPos);   */

        super.onDraw(canvas);  
        canvas.restore();  
    }  
} …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-xml

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

GCM令牌刷新以及何时将令牌发送到服务器

我正在关注官方指南中的GCM-Android集成示例.

特别是我对上面链接类中的以下行感到困惑:

// You should store a boolean that indicates whether the generated token has been
// sent to your server. If the boolean is false, send the token to your server,
// otherwise your server should have already received the token.
Run Code Online (Sandbox Code Playgroud)

现在我每次启动主活动时都会调用intent服务,并且我相信instanceID负责启动令牌刷新.

每次从主要活动发起此GCM注册意图时,我应该检查共享首选项值吗?但是,在这种情况下刷新将失败,因为在初始令牌获取之后,条件将始终为真.

我应该丢弃共享的prefs逻辑 - 这样每次都会向我的服务器发送一个新的令牌.这样做的正确方法是什么?令牌如何刷新worrk以及何时刷新?

android google-cloud-messaging

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

为什么除非使用--packages,否则spark-submit找不到kafka数据源?

我正在尝试将 Kafka 集成到我的 Spark 应用程序中,这是我的 POM 文件所需的条目:

<dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-streaming-kafka-0-10_2.11</artifactId>
        <version>${spark.stream.kafka.version}</version>
</dependency>
<dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka_2.11</artifactId>
        <version>${kafka.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

对应的工件版本是:

<kafka.version>0.10.2.0</kafka.version>
<spark.stream.kafka.version>2.2.0</spark.stream.kafka.version>
Run Code Online (Sandbox Code Playgroud)

我一直摸不着头脑:

Exception in thread "main" java.lang.ClassNotFoundException: Failed to find data source: kafka. Please find packages at http://spark.apache.org/third-party-projects.html
Run Code Online (Sandbox Code Playgroud)

我也尝试为 jar 提供--jars参数,但是它没有帮助。我在这里缺少什么?

代码:

Exception in thread "main" java.lang.ClassNotFoundException: Failed to find data source: kafka. Please find packages at http://spark.apache.org/third-party-projects.html
Run Code Online (Sandbox Code Playgroud)

_spark 定义为:

_spark = SparkSession
                .builder()
                .appName(_properties.getProperty("app.name"))
                .config("spark.master", _properties.getProperty("master"))
                .config("spark.es.nodes", _properties.getProperty("es.hosts"))
                .config("spark.es.port", _properties.getProperty("es.port"))
                .config("spark.es.index.auto.create", "true")
                .config("es.net.http.auth.user", _properties.getProperty("es.net.http.auth.user"))
                .config("es.net.http.auth.pass", _properties.getProperty("es.net.http.auth.pass"))
                .getOrCreate();
Run Code Online (Sandbox Code Playgroud)

我的进口是: …

maven apache-kafka apache-spark apache-spark-sql spark-structured-streaming

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

Joda Time - 计算两个日期之间的秒数会引发异常.

我使用以下代码来计算两个日期之间的秒数差异:

long secondsBetween = (Seconds.secondsBetween(new LocalDate("1901-01-01"), new LocalDate()).getSeconds());
Run Code Online (Sandbox Code Playgroud)

但是我得到以下异常:

08-08 18:21:27.345: E/AndroidRuntime(6972): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testbdr/com.testbdr.MainActivity}: java.lang.ArithmeticException: Value cannot fit in an int: 3584908800
08-08 18:21:27.345: E/AndroidRuntime(6972):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2189)
08-08 18:21:27.345: E/AndroidRuntime(6972):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2216)
08-08 18:21:27.345: E/AndroidRuntime(6972):     at android.app.ActivityThread.access$600(ActivityThread.java:149)
08-08 18:21:27.345: E/AndroidRuntime(6972):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
08-08 18:21:27.345: E/AndroidRuntime(6972):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 18:21:27.345: E/AndroidRuntime(6972):     at android.os.Looper.loop(Looper.java:153)
08-08 18:21:27.345: E/AndroidRuntime(6972):     at android.app.ActivityThread.main(ActivityThread.java:5000)
08-08 18:21:27.345: E/AndroidRuntime(6972):     at java.lang.reflect.Method.invokeNative(Native Method)
08-08 18:21:27.345: E/AndroidRuntime(6972):     at java.lang.reflect.Method.invoke(Method.java:511)
08-08 18:21:27.345: E/AndroidRuntime(6972):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-08 18:21:27.345: E/AndroidRuntime(6972): …
Run Code Online (Sandbox Code Playgroud)

java datetime android jodatime

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

编辑不将背景视为透明的文本

我将以下内容设置为bg的编辑文本:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#A4A4A4" />
        </shape>
    </item>

    <!-- main color -->
    <item
        android:bottom="1dp"
        android:left="0dp"
        android:right="0dp">
        <shape>
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

我的编辑文本嵌套在android.support.design.widget.TextInputLayout:

<android.support.design.widget.TextInputLayout
        android:id="@+id/wrapper_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">
  <EditText
          android:id="@+id/email"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:background="@drawable/bg_edit_txt"
          android:hint="@string/reg_email"
          android:imeOptions="actionNext"
          android:inputType="textEmailAddress"
          android:paddingBottom="10dp"
          android:paddingEnd="10dp"
          android:paddingLeft="10dp"
          android:paddingRight="10dp"
          android:textCursorDrawable="@drawable/edit_text_cursor"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

这呈现如下:

在此输入图像描述

所需的行为是仅在底部渲染一条线,该线覆盖此元素的整个宽度.

在此输入图像描述

java android

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

什么是数据库规范化和功能依赖性?

我正在阅读数据库规范化教程,我很难理解以下内容:

函数依赖性表示如果两个元组具有相同的属性A1,A2,...,An的值,那么这两个元组必须具有相同的属性B1,B2,...,Bn的值.

功能依赖性由箭头符号(→)表示,即X→Y,其中X在功能上确定Y.

以上两个是指什么?"功能决定"是什么意思?

我可以有一个元组,其中A1,A2,A3相同,但B1,B2,B3是不同的.

sql database normalization database-normalization

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

导航抽屉的背景图像大小(轮廓零件-第一行)

我试图按照样式设置导航抽屉 官方材料设计指南来设计。此处定义了所有内容,尽管第一行(配置文件部分)的背景图像大小。我所拥有的是:

在此处输入图片说明

但是如您所见,图像看起来像张拉了。我将其设置如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/mat2" >
    <com.blackB.RoundedImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        android:background="@drawable/icon_default"
        android:contentDescription="@string/imageviewContactus"
        android:scaleType="centerCrop"
        app:riv_border_color="#BDBDBD"
        app:riv_border_width="0dip"
        app:riv_corner_radius="38dip"
        app:riv_mutate_background="true"
        app:riv_oval="true" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="72dp" 
        android:gravity="left|center">

        <TextView
            android:id="@+id/counter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/title"
            android:layout_below="@+id/title"
            android:text="Profile completion"
            android:textColor="#FFFFFF"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mr.Right"
            android:textColor="#FFFFFF"
            android:textSize="14sp" />
    </RelativeLayout>

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

我也尝试过使用Imageview作为背景,但仍然可以拉伸。截至目前,我的图片规格为800x600px,与 google在iosched中使用图像并保存在drawable-nodpi内,就像Google所做的那样。

我在这里想念什么吗?请提出建议。谢谢 :)

android navigation-drawer material-design

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