简体   繁体   中英

How to get the Value of an EditText (Layout.xml) on public interface?

I'm still learning Android Programming and I need help...
I create (from online tutorial) Weather App using Retrofit and Yahoo Weather API.
But this App is made just for one specific City.
Now I would like to Add two EditText (City, Country) and put those content into API link.

I've made activity_settings.xml

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/etCity"
    android:text="Zagreb"
    android:layout_gravity="center_horizontal" />

and WeatherAPI.java

public interface WeatherAPI{

String BASE_URL = "https://query.yahooapis.com/v1/public/";
//String Zagreb = "Zagreb";
EditText cityText = (EditText) findViewById (R.id.etCity);
String city = cityText.getText().toString();


@GET("yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22" + city + "%2C%20Hr%22)%20and%20u%3D'c'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")
Call<Weather> getWeather();

class Factory {

    public static WeatherAPI service;

    public static WeatherAPI getIstance() {

        if (service == null) {

            Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(BASE_URL)
                    .build();


            service = retrofit.create(WeatherAPI.class);
            return service;
        } else {
            return service;
        }
    }
}

As you can see I've add

EditText cityText = (EditText) findViewById (R.id.etCity);
String city = cityText.getText().toString();

and put city into @GET("......")

I get errors:

Error:(20, 42) error: cannot find symbol method findViewById(int) Error:(24, 145) error: attribute value must be constant

What my friend here tried to say, is that as long as you are outside the VIEW object you can call findViewById() easily, however once you are inside that element and you are looking for the findViewById() you need to write before the VIEW object before like this:

Context.findViewById ()  

or in your case

WeatherAPI.findViewById ()

Calling findViewById() on the Activity object will only work if the current Activity layout is set by setContentView . If you add a layout through some other means, then you need the View object of the layout and call findViewById() on it.

like this..

View v = inflater.inflate(id_layout);
View innerView = v.findViewById(R.id.etCity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM