How do I make Activity/ArrayAdapter sparse on Android?

Asked 2 years ago, Updated 2 years ago, 121 views

Hello,

As the title says, "How do I make Activity/ArrayAdapter sparse on Android?"
I tried writing the code where Activity and ArrayAdapter are calling each other's methods.

The fact that Activity and ArrayAdapter are in circular reference may be a design error.
However, I think there are one or two places in the application that have circular references.

By the way, I have been on Android for 2-3 days, so I don't really understand Android programming orz
How is the program written when it comes to circular browsing on Android?

You can change the ArrayAdapter to Fragment.

android java cyclic-reference

2022-09-30 20:34

2 Answers

(As a prerequisite for the answer, I understand that the "circular reference" in the question text refers to the method of Adapter within Activity and to the method of Activity within Adapter.)

ArrayAdapter (the class from which you inherit BaseAdapter) can be registered with a listener who will be notified when management data (that is, what you want to see) changes.

BaseAdapter#registerDataSetObserver(DataSetObserver)

In the current code, by moving the circular reference process implemented in the Adapter into the DataSetObserver, would it be possible to correct what you feel is a problem?

For example, when you press a button, it is rare to inherit the Button class and implement it there, usually implemented in OnClickListener and register it as a listener in the button.
I think it's easy to understand if you think it's the same thing.

(*If there is a specific code and what part of the problem you feel is the problem, I think we can draw a more specific answer.)


2022-09-30 20:34

Circular references occur normally.
Object orientation must be discarded to avoid circular references completely.
It's unrealistic in this environment.

On the other hand, cross-coupling should ideally be avoided ideally.
However, there is also a degree of problem.In the case of the previous Activity/ArrayAdapter, even if you try hard to sparsely combine, it will not pay much.

On the contrary, Activity/Fragment is that should maintain
Or rather, Activity and Fragment are available for that.

There are several ways to achieve sparse coupling.
·Interface
Write the interface by hand.

·DI Container
Dependency injection pattern.
For Android, Dagger and AndroidAnnotations are famous.

·Pub/Sub
When a topic is published by the publisher, the subscriber is notified.
EventBus is famous.

Choose flexibly according to your TPO.


2022-09-30 20:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.