Search controls in a separate class from the main activity

Asked

Viewed 81 times

1

I need to search the controls to change values in the layout. But passing the context by parameter to the class it does not find the controls declared in xml returning null for each cast made.

The class where I am trying to recover this data extends Asynctask:

public class WSCatalogo91 extends AsyncTask<Void, ProgressAux, Boolean> 

private ProgressBar mProgressBar;
private TextView mProgressText;

public WSCatalogo91(WSCatalogo91Listener listener) throws SQLException {
        this.listener = listener;
        dh = new DatabaseHelper((Activity) listener);
        confDao = new ConfiguracaoDao(dh.getConnectionSource());
        pDao = new ProdutoDao(dh.getConnectionSource());

        mProgressBar = (ProgressBar) ((Activity) listener).findViewById(R.id.progressBar);
        mProgressText = (TextView) ((Activity) listener).findViewById(R.id.progressBarTextView);
        auxProgresso = new ProgressAux();
    }

mProgressBar and mProgressText are null, but my context there is not null, so much so that it is being used on the top lines perfectly. Follow the xml code where I declare the ids.

<TextView android:text="@string/progressBar" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/progressBarTextView"
        android:maxWidth="400dp"
        android:maxHeight="400dp"/>

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:indeterminate="false"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="165dp"
        android:minWidth="300dp" />
  • tried to use mProgressBar = (ProgressBar) MainActivity.findViewById(R.id.progressBar)?

  • @Pauloroberto this class is not declared within the Activity, I believe it would not be a good practice.

  • because this class is not declared within Activity? why do you think it would not be good practice? is that on android I usually use Imports or searching from Mainactivity as I told you to find xml elements

  • In order for you to find these elements, you must call the Activity that is with the this.setContentView(R.layout.nomedoseuLayout) set in the onCreate() and uses it as a namespace instead of MainActivity in the example I showed you

  • @Pauloroberto Put as an answer, if that is what I already put as a right answer and you earn your points. I await the answer.

  • The correct thing is to test if it worked first because I didn’t have time to test, it might be?

  • @Pauloroberto I don’t understand very well what you mean, in onCreate() that is within the Activity the setContentView is working normally. But when I pass the context to my other class, I’ll have to do it again in the same process?

  • It depends, whether "your other class" for an Activity, you can do it yes, it will work. But if it’s not, then you have to import your Activity and use its name as namespace SuaActivityQueTemOSetContentViewFuncionando.findViewById(R.id.progressBar)

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.