简体   繁体   中英

How to get response from network if my data is in array list

I want to get response from the.network which is an array list and send that array list to next activity so that I can display it in my recyclerView.

public void sendPost(){
        Call<List<SearchModel>> call = mAPIService.sendSearch("1","2");
        call.enqueue(new Callback<List<SearchModel>>() {
            @Override
            public void onResponse(Call<List<SearchModel>> call, retrofit2.Response<List<SearchModel>> response) {
                Toast.makeText(getApplicationContext(), " Responce " +response.body(), Toast.LENGTH_SHORT).show();
                if (response.isSuccessful()){

                    SearchModel searchResponse = response.body().get(0);
                    assert searchResponse != null;

                    searchModelList.add(searchResponse);
                    Intent intent1 = new Intent(getApplicationContext(),ResultsSearch.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("mylist",searchModelList);
                    intent1.putExtras(bundle);
                    startActivity(intent1);

                }else {
                    Toast.makeText(getApplicationContext(),"Something is error",Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Call<List<SearchModel>> call, Throwable t) {
                Log.e("msg","Failed "+t);
                Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();
            }
        });
    }

And also when I send a single data by storing this value response.body().get(0) in my model,my app crashes. How can I solve these errors. I am getting the value in next activity like this:

public class ResultsSearch extends AppCompatActivity {
    Intent mIntent;
    List vehicleResponse;
    Bundle bundle;

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

        bundle = this.getIntent().getExtras();
        init();


    }
    private void init(){

        vehicleResponse = bundle.getParcelableArrayList("mylist");

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
        recyclerView.setHasFixedSize(true);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        SearchAdapter searchAdapter = new SearchAdapter(vehicleResponse,getApplicationContext());
        recyclerView.setAdapter(searchAdapter);


    }
}

Make sure that the SearchModel is parcelable. But the right way is to pass parameters of search in the Bundle and make the API call on the ResultsSearch

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