Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7514907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:38:05+00:00 2026-05-30T00:38:05+00:00

Currently I have a custom view: public class MyView extends View { public MyView(Context

  • 0

Currently I have a custom view:

public class MyView extends View {

  public MyView(Context c, AttributeSet attrs) {
    super(context, attrs);

  }
    ....
    ....
}

Now, I want to access an object that is not Android related, say a database connection, how do i do that?
My current solution is to do the following

public class MyView extends View {
  DBConnection connection;

  public MyView(Context c, AttributeSet attrs) {
    super(context, attrs);

  }
    ....
    ....
  public void setDBConnection(DBConnection c)
       this.connection = c;
  } 

  public void onDraw(Canvas canvas) {
    this.connection.ensureUser();
     ...
     ....
  }

My question is that is there a better way to do this? Currently I need to set (via setter) to ensure that the object that is going to be used in the MyView class is set, above code snippet works but I don’t know if this is the elegant / popular approach?

I am thinking to pass the object via the constructor, but I can’t do that, because Android only calls the MyView(Context c, AttributeSets attrs) constructor, it won’t call the version if I have MyView(Context c, AttributeSets attrs, DBConnection db) one.

please help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:38:05+00:00Added an answer on May 30, 2026 at 12:38 am

    When I come across situations like this, I tend to take the approach of injecting the displayable data after the view is created. So, if your view needed a string of text to display, you inject that string or the object the string is pulled from during your activities creation by looking up the View using findViewById and then calling the appropriate setter.

    As to “is there a better way to do this”, I would be nervous about passing a dbconnection into a view class. It doesn’t pass the “smell” test and seems like you are leaking your logic into your view. It would make more sense to me to say, give your view a Cursor or some object you’ve filled in inside your Activity. This prevents building custom views that are tightly coupled to a very specific use/scenario.

    On the other hand, you’ll have to decide how reusable you want the view to be. It might be that quick and dirty is the appropriate tact for this particular project.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently on a view I have a ImageView and A custom class that draws
I currently have a custom session handler class which simply builds on php's session
I have custom classes that I currently instantiate within App.xaml as resources. I want
I currently have a need for a custom ListViewItem class - let's call it
Currently, I have an NSStatusItem that, when clicked, shows a custom view below it.
I have a Canvas with custom UserControls in it. Now I want to be
We have a screen and its view model: public class ScreenViewModel : BaseViewModel {
I have a view model which uses custom attributes such as public int Id
I'm looking into some XSS prevention in my Java application. I currently have custom
I currently have a custom TabItem which has a custom header, which is defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.