0

を飛ばし添付ませアダプタは私のMusicItemクラスではありません。ここRecyclerView:ここではレイアウト

public class MusicItem { 

int id; 
String producer; 
String farsi_name; 
String photo; 
int rate; 
String date_published; 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String getProducer() { 
    return producer; 
} 

public void setProducer(String producer) { 
    this.producer = producer; 
} 

public String getFarsi_name() { 
    return farsi_name; 
} 

public void setFarsi_name(String farsi_name) { 
    this.farsi_name = farsi_name; 
} 

public String getPhoto() { 
    return photo; 
} 

public void setPhoto(String photo) { 
    this.photo = photo; 
} 

public int getRate() { 
    return rate; 
} 

public void setRate(int rate) { 
    this.rate = rate; 
} 

public String getDate_published() { 
    return date_published; 
} 

public void setDate_published(String date_published) { 
    this.date_published = date_published; 
} 
} 

は私のMusicFeedクラスである:ここ

public class MusicFeed { 
    List<MusicItem> list; 
    int counter; 

    public MusicFeed() { 
     list=new Vector<MusicItem>(); 
    } 

    public void addAll(MusicItem... musicItem){ 
     for (int i=0; i<musicItem.length; i++){ 
      list.add(musicItem[i]); 
      counter++; 
     } 
    } 

    public int getCounter(){ 
     return counter; 
    } 

    public MusicItem getItem(int position){ 
     return list.get(position); 
    } 
} 

は私MainActivityです:

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

    Button button; 

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

     button=(Button) findViewById(R.id.button); 
    } 

    @Override 
    public void onClick(View view) { 
     Intent intent=new Intent(MainActivity.this, MusicPage.class); 
     startActivity(intent); 
    } 
} 

、ここで私のMusicPageです:

public class MusicPage extends AppCompatActivity { 

    RecyclerView recyclerView; 

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

     recyclerView=(RecyclerView) findViewById(R.id.rec); 
     RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); 
     recyclerView.setLayoutManager(layoutManager); 

     async as=new async(new MusicFeed()); 
     as.execute(); 

    } 
    public class VVHH extends RecyclerView.ViewHolder{ 

     TextView farsi_name; 
     TextView producer; 
     TextView date_published; 
     TextView rate; 
     ImageView image; 

     public VVHH(View itemView) { 
      super(itemView); 
      farsi_name=(TextView) itemView.findViewById(R.id.farsiname); 
      producer=(TextView) itemView.findViewById(R.id.producer); 
      date_published=(TextView) itemView.findViewById(R.id.datepublished); 
      rate=(TextView) itemView.findViewById(R.id.rate); 
      image=(ImageView) itemView.findViewById(R.id.image); 
     } 
    } 
    public class MyAdapter extends RecyclerView.Adapter<VVHH>{ 


     MusicFeed feed; 

     public MyAdapter(MusicFeed feed) { 
      this.feed = feed; 
     } 

     @Override 
     public VVHH onCreateViewHolder(ViewGroup parent, int viewType) { 
      View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false); 
      return new VVHH(view); 
     } 

     @Override 
     public void onBindViewHolder(VVHH holder, int position) { 

      MusicItem musicitem=feed.getItem(position); 

      holder.farsi_name.setText(musicitem.getFarsi_name()); 
      holder.producer.setText(musicitem.getProducer()); 
      holder.date_published.setText(musicitem.getDate_published()); 
      holder.rate.setText(Integer.toString(musicitem.getRate())); 
      Glide.with(MusicPage.this).load(musicitem.getPhoto()).into(holder.image); 


     } 

     @Override 
     public int getItemCount() { 
      return feed.getCounter(); 
     } 
    } 
    public class async extends android.os.AsyncTask<Void, Void, MusicFeed>{ 

     MusicFeed musicFeed; 

     public async(MusicFeed musicFeed) { 
      this.musicFeed = musicFeed; 
     } 

     @Override 
     protected MusicFeed doInBackground(Void... voids) { 
      String URL = "http://79.175.176.229:8060/mcirbt/0/homepage/?format=json"; 
      OkHttpClient client = new OkHttpClient(); 
      Request request = new Request.Builder().url(URL).build(); 
      okhttp3.Call call = client.newCall(request); 


      call.enqueue(new Callback() { 
       @Override 
       public void onFailure(Call call, IOException e) { 

       } 

       @Override 
       public void onResponse(Call call, Response response) throws IOException { 

        final String JsonData = response.body().string(); 
        try { 

         parseJson(JsonData); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 

       private void parseJson(String jsonData) throws JSONException { 
        JSONObject jsonObject = new JSONObject(jsonData); 
        JSONArray jsonArray = jsonObject.getJSONArray(Keys.KEY_POPULAR_ALBUMS); 
        final String url = "http://79.175.176.229:8060"; 

        for (int i = 0; i < jsonArray.length(); i++) { 
         JSONObject jsonObject1 = jsonArray.getJSONObject(i); 
         MusicItem musicItem = new MusicItem(); 
         musicItem.setRate(jsonObject1.getInt(Keys.KEY_RATE)); 
         musicItem.setDate_published(jsonObject1.getString(Keys.KEY_DATE_PUBLISHED)); 
         musicItem.setFarsi_name(jsonObject1.getString(Keys.KEY_FARSI_NAME)); 
         musicItem.setProducer(jsonObject1.getString(Keys.KEY_PRODUCER)); 
         musicItem.setPhoto(url + jsonObject1.getString(Keys.KEY_PHOTO)); 
         musicFeed.addAll(musicItem); 

         Log.d("000000000000000000000", jsonObject1.getString(Keys.KEY_PRODUCER)); 
        } 
       } 
      }); 
      return musicFeed; 
     } 



     @Override 
     protected void onPostExecute(MusicFeed musicFeed) { 
      recyclerView.setAdapter(new MyAdapter(musicFeed)); 
     } 
    } 
} 

また、onPostexecuteメソッドでアダプタを接続しましたが、アダプタが接続されていないと言われました。何も表示されません!

+0

logcatを使用して、データがonBindViewHolderに存在している場合は見てみ – FishStix

答えて

0

あなたのrecyclerviewは、作成後にアダプターを設定するため、これを示しています。アダプターは、作成時にメインスレッドに設定する必要があります。

public class MusicPage extends AppCompatActivity { 
MusicFeed musicFeed; 
MyAdapter recyclerAdapter; 

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

    recyclerView=(RecyclerView) findViewById(R.id.rec); 
    RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); 
    recyclerView.setLayoutManager(layoutManager); 
recyclerAdapter = new MyAdapater(musicFeed); 
recyclerAdapter= new MyAdapter(musicFeed); 
recyclerView.setAdapter(recyclerAdapter); 

    async as=new async(musicFeed); 
    as.execute(); 

} 


@Override 
    protected void onPostExecute(MusicFeed musicFeed) { 
     recyclerAdapter.notifyDataSetChanged(); 
    } 
+0

「)notifyDataSetChanged(」その後、onPostExecuteに設定されたアダプタのデータを更新して呼び出し、のonCreateですぐにアダプタを設定してみてください。 musicitem.getFarsi_name()などです。 – GeorgeHz

関連する問題