在内部类之外声明一个 int 值

huangapple go评论78阅读模式
英文:

Declaring an int value outside of inner class

问题

如何在其内部类之外的地方获取 int count 的值以在 setText 中声明

model.getSearch().observe(this, new Observer<List<Sight>>() {
    @Override
    public void onChanged(@Nullable List<Sight> searchList) {
        adaptersearch = new SearchAdapter(SearchResults.this, searchList);
        searchhList.setAdapter(adaptersearch);
        int count = 0;
        if (adaptersearch != null) {
            count = adaptersearch.getItemCount();
        }
        
        // 将这里的 count 存储在外部变量中,以便在内部类之外使用
        // 例如:将 count 声明在外部类的范围内,然后在下面的 apptxt.setText 中使用
    }
});

// 在这里无法直接访问 count,需要将 count 定义在能被整个方法块访问的范围内
// apptxt.setText(count + name + "items");
// 此处目前会出现错误,无法解析 count。

注意:因为内部类和外部类作用域的限制,无法直接在内部类的代码块外部直接访问内部类中的局部变量(如 count)。您需要将 count 声明在能被整个方法块访问的范围内,以便在内部类之外使用。

英文:

How do i get the value of the int count to be declare in a setText outside of its inner class?

    model.getSearch().observe(this, new Observer&lt;List&lt;Sight&gt;&gt;() {
        @Override
        public void onChanged(@Nullable List&lt;Sight&gt; searchList) {
            adaptersearch = new SearchAdapter(SearchResults.this, searchList);
            searchhList.setAdapter(adaptersearch);
            int count = 0;
            if (adaptersearch != null) {
                count = adaptersearch.getItemCount();

            }


        }
    });  
    apptxt.setText(count +name+ &quot;items&quot;);

At the minute it just comes up with the error cannot resolve count.

答案1

得分: 2

你无法在内部类中更改已在外部类中初始化的变量(更改变量的值)。

有一种解决方法是创建一个带有一个值的 final int 数组,然后您可以在内部类中访问它。

请注意,这并不是最佳的解决方法。

英文:

You can't write to a variable (change the value of the variable ) that has been initialized in outer class from an inner class.

there is a workaround to do it is to create a final array of int with one value, then you can access it in your inner class.

keep in mind that this is not the best way to do it.

答案2

得分: 2

如果这真的是您寻找的情况,其中一种方法是使用int包装器或者使用AtomicInteger,这在涉及线程的情况下会很有帮助:

final AtomicInteger count = new AtomicInteger(0);
model.getSearch().observe(this, new Observer<List<Sight>>() {
        @Override
        public void onChanged(@Nullable List<Sight> searchList) {
            adaptersearch = new SearchAdapter(SearchResults.this, searchList);
            searchhList.setAdapter(adaptersearch);
            if (adaptersearch != null) {
                count.set(adaptersearch.getItemCount());
            }
        }
    });  
    apptxt.setText(count.get() + name + "items");

您所经历的问题是,在这种情况下引用应该是final的,因此您必须更改对象内部的值,而不能更改引用。

英文:

If this is really the scenario you are looking for one way of doing it is using an int wrapper or using AtomicInteger which is helpful should there be threads involved there:

final AtomicInteger count = new AtomicInteger(0);
model.getSearch().observe(this, new Observer&lt;List&lt;Sight&gt;&gt;() {
        @Override
        public void onChanged(@Nullable List&lt;Sight&gt; searchList) {
            adaptersearch = new SearchAdapter(SearchResults.this, searchList);
            searchhList.setAdapter(adaptersearch);
            if (adaptersearch != null) {
                count.set(adaptersearch.getItemCount());

            }


        }
    });  
    apptxt.setText(count.get() +name+ &quot;items&quot;);

What you are experiencing is that the reference should be final in that case and therefore you must change the value inside the object as you cannot change the reference.

huangapple
  • 本文由 发表于 2020年5月4日 04:10:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61580968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定