I want to get 2 api url from json to bind them into one listview in android

3873 views android
1

I want to get 2 api url from json to bind them into one listview in android. One api read data detail and another read image so both of them into the same listview to show detail information.

Here is my first api to get text detail

public class NationalNewsFragment extends Fragment
{
SwipeRefreshLayout swipeRefreshLayout;
private ProgressDialog progressDialog;
ListView listView;
ImageView frontbanner;

//NEWS API
RequestQueue queue;
ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, 
String>>();
static final String KEY_TITLE = "Title";
static final String KEY_INFO1 = "info1";
static final String KEY_C_DATE = "C_date";
static final String KEY_News_ID = "News_ID";
static final String KEY_C_BY = "C_by";
static final String KEY_C_VIEWS = "C_views";

//FrontBanner API
RequestQueue requestQueue;
static final String FRONT_BANNER = "Adv";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_national_news, container, 
false);

    listView = (ListView)view.findViewById(R.id.list_source);
    swipeRefreshLayout = 
(SwipeRefreshLayout)view.findViewById(R.id.refresh_layout);
    //NEWS API
    queue = Volley.newRequestQueue(getContext());
    requestQueue = Volley.newRequestQueue(getContext());

    progressDialog = new ProgressDialog(getContext());
    progressDialog.setMessage("Loading...");
    progressDialog.setCanceledOnTouchOutside(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.show();
    String url = "my api url here";
    JsonArrayRequest jsonArrayRequest = new 
 JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray>()
            {
                @Override
                public void onResponse(JSONArray response)
                {

                    for (int i = 0; i < response.length(); i++)
                    {
                        try
                        {
                            JSONObject news = response.getJSONObject(i);

                            HashMap<String, String> map = new 
HashMap<String, String>();

                            map.put(KEY_TITLE, 
news.optString(KEY_TITLE).toString());
                            map.put(KEY_INFO1, 
news.optString(KEY_INFO1).toString());
                            map.put(KEY_C_DATE, 
news.optString(KEY_C_DATE).toString());
                            map.put(KEY_News_ID, 
news.optString(KEY_News_ID).toString());
                            map.put(KEY_C_BY, 
news.optString(KEY_C_BY).toString());
                            map.put(KEY_C_VIEWS, 
news.optString(KEY_C_VIEWS).toString());


//System.out.println(news.optString(KEY_TITLE).toString());
                            dataList.add(map);


                            ListNewsAdapter adapter = new 
ListNewsAdapter(getActivity(), dataList);
                            listView.setAdapter(adapter);

                        } catch (JSONException e)
                        {
                            e.printStackTrace();
                        }
                    }
                    progressDialog.cancel();
                }
            }, new Response.ErrorListener()
    {
        @Override
        public void onErrorResponse(VolleyError error)
        {
            error.printStackTrace();
        }
    });

    queue.add(jsonArrayRequest);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Intent i = new Intent(getActivity(), 
FreshNewsDetailActivity.class);
            i.putExtra("News_ID", dataList.get(position).get(KEY_News_ID));
            startActivity(i);
            //Toast.makeText(MainActivity.this, 
dataList.get(position).get(KEY_News_ID), Toast.LENGTH_SHORT).show();
        }
    });

    //Refresh layout
    swipeRefreshLayout.setOnRefreshListener(new 
SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeRefreshLayout.setRefreshing(false);
            progressDialog = new ProgressDialog(getContext());
            progressDialog.setMessage("Loading...");
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.show();
            String url = "my api url here";
            JsonArrayRequest jsonArrayRequest = new 
            JsonArrayRequest(Request.Method.GET, url, null,
                    new Response.Listener<JSONArray>()
                    {
                        @Override
                        public void onResponse(JSONArray response)
                        {

               

answered question

Please add your JSON response as well. And if you have control over your rest API then you can merge both APIs itself from the server (IF APPLICABLE)

1 Answer

0

Should use one adapter. With complex data source (ex: need to fetch from multiple APIs) you should do some research about Reactive Programming. If apply Reactive Java + Android, you can do it by a few lines of code.

posted this

Have an answer?

JD

Please login first before posting an answer.