Capture links in JSON for each recyclerView and appear data from the link that was captured which is another JSON (complicated)

1

So it's complicated to ask, but anyway, this is the logic:

[ 
{ "name": "http://meusite/dados1.json"}, 
{ "name": "http://meusite/dados2.json", }, 
{ "name": "http://meusite/dados3.json"}
 ]

So as it was seen, I do not want to pass data straight from this main JSON, I want every data1.json information to appear in a recyclerView and data2.json appears in another division of the recyclerView. I will have to capture every link contained in "name" in a recyclerView and then extract the information contained in it, I want to work with multiple JSONs in just one screen. Hard work for me: / Logic does not enter the head. Here is the JSON code appearing in the recyclerView, but with data written directly. This is the basis!

PHP-embedded JSON file:

<?php echo json_encode(Array(Array( "song_name" => "Hero", "song_id" => "1990", "artist_name" => "Enrique"), Array("song_name" => "African Queen", "song_id" => "2004", "artist_name" => "Tuface"), Array("song_name" => "Ifunanyi", "song_id" => "2012", "artist_name" => "PSquare"))); ?>

ItemObject.java

import com.google.gson.annotations.SerializedName;

public class ItemObject {

    @SerializedName("song_name")
    private String songTitle;
    @SerializedName("song_id")
    private String songYear;
    @SerializedName("artist_name")
    private String songAuthor;

    public ItemObject(String songTitle, String songYear, String songAuthor) {
        this.songTitle = songTitle;
        this.songYear = songYear;
        this.songAuthor = songAuthor;
    }

    public String getSongTitle() {
        return songTitle;
    }

    public String getSongYear() {
        return songYear;
    }

    public String getSongAuthor() {
        return songAuthor;
    }
}

RecyclerViewAdapter.java

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {

    private List<ItemObject> itemList;
    private Context context;

    public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
        RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
        return rcv;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolders holder, int position) {
        holder.songTitle.setText("Song Title: " + itemList.get(position).getSongTitle());
        holder.songYear.setText("Song Year: " + itemList.get(position).getSongYear());
        holder.songAuthor.setText("Song Author: " + itemList.get(position).getSongAuthor());
    }

    @Override
    public int getItemCount() {
        return this.itemList.size();
    }
}

RecyclerViewHolders.java

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{

    public TextView songTitle;
    public TextView songYear;
    public TextView songAuthor;

    public RecyclerViewHolders(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        songTitle = (TextView)itemView.findViewById(R.id.song_title);
        songYear = (TextView)itemView.findViewById(R.id.song_year);
        songAuthor = (TextView)itemView.findViewById(R.id.song_author);
    }

    @Override
    public void onClick(View view) {

    }
}

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private final String TAG = "MainActivity";

    private RecyclerView recyclerView;

    private LinearLayoutManager layoutManager;

    private RecyclerViewAdapter adapter;

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

        recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
        layoutManager = new LinearLayoutManager(MainActivity.this);
        recyclerView.setLayoutManager(layoutManager);

        requestJsonObject();
    }

    private void requestJsonObject(){

        RequestQueue queue = Volley.newRequestQueue(this);
        String url ="http://toscanyacademy.com/blog/mp.php";

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, "Response " + response);
                        GsonBuilder builder = new GsonBuilder();
                        Gson mGson = builder.create();

                        List<ItemObject> posts = new ArrayList<ItemObject>();
                        posts = Arrays.asList(mGson.fromJson(response, ItemObject[].class));

                        adapter = new RecyclerViewAdapter(MainActivity.this, posts);
                        recyclerView.setAdapter(adapter);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG, "Error " + error.getMessage());
            }
        });
        queue.add(stringRequest);
    }
}

Finally, result:

Does anyone know how to do this? Many thanks!

    
asked by anonymous 04.10.2016 / 00:07

0 answers