小编Vin*_*rat的帖子

Android中的Parcelable和inheritance

我得到了Parcelable的一个实现,它适用于一个不涉及继承的单个类.在进行继承时,我在确定实现接口的最佳方法时遇到了问题.假设我得到了这个:

public abstract class A {
    private int a;
    protected A(int a) { this.a = a; }
}

public class B extends A {
    private int b;
    public B(int a, int b) { super(a); this.b = b; }
}
Run Code Online (Sandbox Code Playgroud)

问题是,这是为B实现Parcelable接口的推荐方法(在A?中都有??如何?)

java inheritance android design-patterns parcelable

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

Android自动水平滚动TextView

我正在尝试实现一个自动滚动的单行文本视图.但我不幸的是无法让它发挥作用.AutoScrollTextView在LinearLayout(width和height = fill_parent)中声明.该类基本上使用一个Handler,它调用自己按给定的量滚动.我已将代码简化为仅显示应每秒滚动5个像素的文本视图.

日志输出正确,getScrollX()方法返回相应的scrollX位置.

如果我不打电话requestLayout(),就不会有任何吸引力.invalidate()没有效果.

有人会有线索吗?

public class AutoScrollTextView extends TextView {

    public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setSingleLine();
        setEllipsize(null);
        setText("Single-line text view that scrolls automatically if the text is too long to fit in the widget");
    }

    // begin to scroll the text from the original position
    public void startScrolling() {
        scrollHandler.sendEmptyMessage(0);
    }

    private Handler scrollHandler = new Handler() {
        private static final int REFRESH_INTERVAL = 1000;

        public void handleMessage(Message …
Run Code Online (Sandbox Code Playgroud)

android scroll textview

58
推荐指数
3
解决办法
8万
查看次数

在android中记录SQL查询

我正在使用这些query函数来为我的表构建SQL查询.有没有办法看到运行的实际查询?比如把它记在某个地方?

到目前为止,我能做的最好的事情是使用断点查看游标的成员mQuery.我喜欢自动输出查询.该会员当然不公开,也没有吸气剂.


仅供记录,这是接受答案的实现.

/**
 * Implement the cursor factory in order to log the queries before returning 
 * the cursor
 * 
 * @author Vincent @ MarvinLabs
 */
public class SQLiteCursorFactory implements CursorFactory {

    private boolean debugQueries = false;

    public SQLiteCursorFactory() {
        this.debugQueries = false;
    }

    public SQLiteCursorFactory(boolean debugQueries) {
        this.debugQueries = debugQueries;
    }

    @Override
    public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery, 
                            String editTable, SQLiteQuery query) {
        if (debugQueries) {
            Log.d("SQL", query.toString());
        }
        return new SQLiteCursor(db, masterQuery, editTable, query);
    } …
Run Code Online (Sandbox Code Playgroud)

sqlite logging android

53
推荐指数
4
解决办法
3万
查看次数

用于数据加载的Android活动/片段职责

在为客户启动新应用程序时,我再次问自己应该负责加载数据的相同问题:活动或片段.我已经为各种应用程序采取了两种选择,我想知道哪种模式最适合您:

  • 限制代码复杂性.
  • 处理边缘情况(如屏幕旋转,屏幕省电,连接丢失等)

选项1 - 活动加载数据和片段仅显示它

这允许具有仅被馈送一堆对象的片段来显示.他们对加载数据以及我们如何加载数据一无所知.

另一方面,活动使用任何需要的方法加载数据(例如,最初最近的50个条目和搜索,加载搜索结果).然后它将它传递给显示它的片段.加载数据的方法可以是任何东西(来自服务,来自DB,......片段只知道POJO)

它是一种MVC架构,其中活动是控制器,片段是视图.

选项2 - 活动安排片段和片段负责获取数据

在这种模式中,片段是应用程序的自主部分.他们知道如何加载他们正在显示的数据以及如何向用户显示数据.

活动只是一种在屏幕上排列片段并协调应用程序活动之间转换的方法.

android

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

Android图像视图矩阵比例+翻译

我试图手动获取图像视图内部的图像并适合屏幕.我需要用矩阵来做(我稍后会动态地改变矩阵变换).

问题是我无法将图像置于视图中心(比例尺是合适的).这是代码:

// Compute the scale to choose (this works)
float scaleX = (float) displayWidth / (float) imageWidth;
float scaleY = (float) displayHeight / (float) imageHeight;
float minScale = Math.min(scaleX, scaleY);

// tx, ty should be the translation to take the image back to the screen center
float tx = Math.max(0, 
        0.5f * ((float) displayWidth - (minScale * imageWidth)));
float ty = Math.max(0, 
        0.5f * ((float) displayHeight - (minScale * imageHeight)));

// Compute the matrix
Matrix m = new Matrix(); …
Run Code Online (Sandbox Code Playgroud)

android transformation matrix imageview

30
推荐指数
2
解决办法
9万
查看次数

Android Studio和Gradle - 构建失败

我正在建立一个小型图书馆项目以及一个示例项目来说明其用途.我无法在Android Studio中运行该示例.我从头开始创建项目.我对Eclipse很有经验,但这是我第一次尝试使用Android Studio和Gradle.

给出的错误:

Gradle:任务':demo:dexDebug'的执行失败.

运行C:\ DevTools\Android\android-studio\sdk\build-tools\android-4.2.2\dx.bat失败.见输出

我有以下文件夹结构:

- demo
  - build
  - libs
    - android-support-v4.jar
  - src
    - main
      - java
      - res
  - build.gradle
- library
  - build
  - libs
    - android-support-v4.jar
  - src
    - main
      - java
      - res
  - build.gradle
- build.gradle
- settings.gradle
Run Code Online (Sandbox Code Playgroud)

项目根目录下的Build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
Run Code Online (Sandbox Code Playgroud)

项目根目录下的Settings.gradle:

include ':library', ':demo'
Run Code Online (Sandbox Code Playgroud)

库模块的Build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
} …
Run Code Online (Sandbox Code Playgroud)

android gradle android-studio android-gradle-plugin

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

Android分享意图选择器

我使用这样的东西在用户手机上使用可用的应用程序共享一些文本.

public void share(String subject,String text) {
     final Intent intent = new Intent(Intent.ACTION_SEND);

     intent.setType("text/plain");
     intent.putExtra(Intent.EXTRA_SUBJECT, subject);
     intent.putExtra(Intent.EXTRA_TEXT, text);

     startActivity(Intent.createChooser(intent, getString(R.string.share)));
}
Run Code Online (Sandbox Code Playgroud)

我的主要问题是,如果用户选择Twitter而不是电子邮件,我希望有一个不同的文本(短版本的短版本VS带有附加图像的全文).

如何找出用户决定使用哪个应用程序?

twitter android facebook android-intent

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

Laravel单元测试 - 根据测试方法更改配置值

我有一个用于验证帐户的系统的应用程序(注册 - >获取带有激活链接的电子邮件 - >已验证帐户).该验证流程是可选的,可以使用配置值关闭:

// config/auth.php
return [
  // ...
  'enable_verification' => true
];
Run Code Online (Sandbox Code Playgroud)

我想测试注册控制器:

  • 它应该在两种情况下都重定向到主页
  • 验证开启时,主页应显示"已发送电子邮件"消息
  • 验证关闭时,主页应显示"已创建帐户"消息
  • 等等

我的测试方法:

public function test_UserProperlyCreated_WithVerificationDisabled()
{
    $this->app['config']->set('auth.verification.enabled', false);

    $this
        ->visit(route('frontend.auth.register.form'))
        ->type('Test', 'name')
        ->type('test@example.com', 'email')
        ->type('123123', 'password')
        ->type('123123', 'password_confirmation')
        ->press('Register');

    $this
        ->seePageIs('/')
        ->see(trans('auth.registration.complete'));
}

public function test_UserProperlyCreated_WithVerificationEnabled()
{
    $this->app['config']->set('auth.verification.enabled', true);

    $this
        ->visit(route('frontend.auth.register.form'))
        ->type('Test', 'name')
        ->type('test@example.com', 'email')
        ->type('123123', 'password')
        ->type('123123', 'password_confirmation')
        ->press('Register');

    $this
        ->seePageIs('/')
        ->see(trans('auth.registration.needs_verification'));
}
Run Code Online (Sandbox Code Playgroud)

在调试时,我注意到在控制器方法内部的配置值总是设置为配置文件中的值,无论我用什么设置我的 $this->app['config']->set...

我对用户存储库本身进行了其他测试,以检查在验证为ON或OFF时它是否都有效.测试的行为符合预期.

知道为什么控制器失败以及如何解决这个问题?

php phpunit unit-testing laravel

11
推荐指数
2
解决办法
3990
查看次数

ListView中第一个和最后一个项目的不同项目模板

我需要以不同方式设置列表视图的第一个和最后一个项目的样式.为此,我开始基于该答案开发解决方案:在WPF项目控件中为最后一项使用不同的模板

基本上,我有一个自定义的ItemsTemplateSelector,它根据列表视图项中的项索引决定要应用的模板(下面的代码).

它正常工作,除了当列表更新(添加或删除项目)时,模板不会再次被选中(例如,最初,SingleItemTemplate被选中,因为有一个项目.当我添加一个项目到列表,第一项的模板没有切换到FirstItemTemplate).如何强制所有项目的模板选择?

public class FirstLastTemplateSelector : DataTemplateSelector 
{
    public DataTemplate DefaultTemplate { get; set; }
    public DataTemplate FirstItemTemplate { get; set; }
    public DataTemplate LastItemTemplate { get; set; }
    public DataTemplate SingleItemTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        ListView lv = VisualTreeHelperEx.FindParentOfType<ListView>(container);
        if (lv != null)
        {
            if (lv.Items.Count == 1)
            {
                return SingleItemTemplate;
            }

            int i = lv.Items.IndexOf(item);
            if (i == 0)
            {
                return FirstItemTemplate;
            }
            else if (i == lv.Items.Count …
Run Code Online (Sandbox Code Playgroud)

wpf listview datatemplate

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

对于使用Jackson实现Collection的类,反序列化失败

我有以下JSON:

{
  "item": [
    { "foo": 1 },
    { "foo": 2 }
  ]
} 
Run Code Online (Sandbox Code Playgroud)

这基本上是一个包含项集合的对象.

所以我做了一个课程来反序列化:

public class ItemList {
  @JsonProperty("item")
  List<Item> items;

  // Getters, setters & co.
  // ...
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切都很顺利.

现在,为了让我的生活更轻松,我决定能够迭代ItemList对象并让它实现Collection接口.

基本上我的班级成了:

public class ItemList implements Collection<Item>, Iterable<Item> {
  @JsonProperty("item")
  List<Item> items;

  // Getters, setters & co.

  // Generated all method delegates to items. For instance:
  public Item get(int position) {
    return items.get(position);
  }
}
Run Code Online (Sandbox Code Playgroud)

该实现工作正常,很好.但是,反序列化现在失败了.

杰克逊看起来很困惑:

com.fasterxml.jackson.databind.JsonMappingException:无法从START_OBJECT标记中反序列化com.example.ItemList的实例

我试图添加,@JsonDeserialize(as=ItemList.class)但它没有做到这一点.

怎么走?

java json jackson

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