я просто следовал exapmple: https://www.learn2crack.com/2016/02/recyclerview-json-parsing.html его рабочих большие ..Я хочу разобратьAndroid дооснащения разбор
[
{
"song_name": "Hero",
"song_id": "1990",
"artist_name": "Enrique"
},{
"song_name": "African Queen",
"song_id": "2004",
"artist_name": "Tuface"
}, {
"song_name": "Ifunanyi",
"song_id": "2012",
"artist_name": "PSquare"
}
]
Вот мой код я получаю blank..Am новое андроид Дайте некоторые идеи ничего другого я want..Any один, пожалуйста, руководство меня
общественного класса MainActivity расширяет AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<ItemObject> data;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
loadJSON();
}
private void loadJSON(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://toscanyacademy.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface request = retrofit.create(RequestInterface.class);
Call<JSONResponse> call = request.getJSON();
call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
JSONResponse jsonResponse = response.body();
data = new ArrayList<>(Arrays.asList(jsonResponse.getAndroid()));
adapter = new MyAdapter(data);
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d("Error",t.getMessage());
}
});
}
}
Мой интерфейс Запрос:
public interface RequestInterface {
@GET("blog/mp.php")
Call<JSONResponse> getJSON();
}
Json Ответ:
public class JSONResponse {
private ItemObject[] obj;
public ItemObject[] getAndroid() {
return obj;
}
}
public class ItemObject {
public String getSong_name() {
return song_name;
}
public String getSong_id() {
return song_id;
}
public String getArtist_name() {
return artist_name;
}
private String song_name,song_id,artist_name;
}
Вот мой адаптер:
общественного класса MyAdapter расширяет RecyclerView.Adapter {
private ArrayList<ItemObject> users;
public MyAdapter(ArrayList<ItemObject> first) {
this.users = first;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) {
viewHolder.song_title.setText(users.get(i).getsong_name());
viewHolder.song_id.setText(users.get(i).getsong_id());
viewHolder.song_author.setText(users.get(i).getartist_name());
}
@Override
public int getItemCount() {
return users.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView song_title, song_id, song_author;
public ViewHolder(View view) {
super(view);
song_title = (TextView) view.findViewById(R.id.song_name);
song_id = (TextView) view.findViewById(R.id.song_id);
song_author=(TextView) view.findViewById(R.id.artist_name);
}
}
}
Row Layout:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/song_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="#000"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/song_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="#000"
/>
<TextView
android:id="@+id/artist_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="#000" />
Покажите свой полный код. – Drv
Привет, я только что обновил свой код .. –
ваш json - это массив, а не объект. Вам не нужно 'JSONResponse', вы можете объявить интерфейс напрямую, как' Call> getJSON(); ' –
njzk2