1
So it’s complicated to ask, but anyway, that’s the logic:
[
{ "name": "http://meusite/dados1.json"},
{ "name": "http://meusite/dados2.json", },
{ "name": "http://meusite/dados3.json"}
]
So as it was seen, I don’t want to pass data directly from this main JSON, I want every piece of information from dados1.json appears in a recyclerView and the dados2.json appear in another division of recyclerView. I’ll have to capture every link contained in "name" in a recyclerView and then extract the information contained in it, i.e., I want to work with multiple JSON in just one screen. Hard work for me :/ Logic doesn’t get in your head. Here is the JSON code appearing in recyclerView, only with data written directly. This is the basis!
JSON file embedded in PHP:
<?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, the result:
Does anyone know how to do that? Thank you very much!
Solved your doubt?
– durtto