How to retrieve web service values through Retrofit to text view?

微笑、不失礼 提交于 2020-01-07 08:07:13

问题


I'm developing an app and in first activity it has card view layout. I'm retrieving data from a webservice and relevant data are showed in card view. It's working well. Now when a user clicks a particular card view I need to go for another activity. I'm getting relevant ID for that card view and passing it to the second activity. In second activity I need to show the content according to that unique Id. But I'm not getting any thing. This is what I tried.

Pojo class

public class PromoDetails {

String PromoId;
String PromoName;
String PromoImg;
String promoDetails;
String promoValidty;

public PromoDetails(String PromoId, String PromoName, String PromoImg , String promoDetails , String promoValidity) {
    this.PromoId = PromoId;
    this.PromoName = PromoName;
    this.PromoImg = PromoImg;
    this.promoDetails = promoDetails;
    this.promoValidty = promoValidity;
}



public String getPromoId() {
    return PromoId;
}

public void setPromoId(String promoId) {
    PromoId = promoId;
}

public String getPromoName() {
    return PromoName;
}

public void setPromoName(String promoName) {
    PromoName = promoName;
}

public String getPromoImg() {
    return PromoImg;
}

public void setPromoImg(String promoImg) {
    PromoImg = promoImg;
}

public String getPromoDetails() {
    return promoDetails;
}

public void setPromoDetails(String promoDetails) {
    this.promoDetails = promoDetails;
}

public String getPromoValidty() {
    return promoValidty;
}

public void setPromoValidty(String promoValidty) {
    this.promoValidty = promoValidty;
}}

ApiInterface

public interface ApiInterface {


@POST("ap/promotions.php")
Call<List<Promotions>> getPromotions();

@GET("test.php/promotions/{PromoId}")
Call<List<PromoDetails>> getPromotDetails(@Path("PromoId") String PromoId) ;}

New Activity class

public class PromotionsInside extends Activity {


private ApiInterface apiInterface;
private List<PromoDetails> promoDetails;
TextView prDescription;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.promo_inside);


    Bundle extras = getIntent().getExtras();
     String promoId = "";

    if (extras != null) {
        promoId = extras.getString("PROMO_ID");


      getPromotionUpdate(promoId);
    }

}


private void getPromotionUpdate(String myPromoId) {


    apiInterface = ApiClient.getApiClient().create(ApiInterface.class);


    Call<List<PromoDetails>> call = apiInterface.getPromotDetails(myPromoId);
    call.enqueue(new Callback<List<PromoDetails>>() {
        @Override
        public void onResponse(Call<List<PromoDetails>> call, Response<List<PromoDetails>> response) {
            promoDetails = response.body();

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

 prDescription = (TextView)findViewById(R.id.promoDescriptionsss) ;
 prDescription.setText(promoDetails.get(0).getPromoName());
                }
            });


        }

        @Override
        public void onFailure(Call<List<PromoDetails>> call, Throwable t) {

        }
    });


}}

回答1:


I have similar case. Try to use this to start second activity:

    Intent intent = new Intent(this, PromotionsInside.class);
//Make sure that you put String id in intent
    intent.putExtra("PROMO_ID", id);

    startActivity(intent);

And this in second activity:

if (getIntent().hasExtra("PROMO_ID")) {
        String id = getIntent().getStringExtra("PROMO_ID", null);
        //next steps that you need
    }

Hope it will help you



来源:https://stackoverflow.com/questions/46177484/how-to-retrieve-web-service-values-through-retrofit-to-text-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!