2017-03-04 4 views
1

json 응답으로부터 항목을 채우는 RecyclerView가 있습니다.RecyclerView는 어떤 항목을 표시하지 않습니다.

public class ProgramModel { 

private String title; 
private String message; 
private int image; 
private String imageUrl; 

public ProgramModel(String title, String message, int image) { 
    this.title = title; 
    this.message = message; 
    this.image = image; 
} 

public ProgramModel(String title, String message) { 
    this.title = title; 
    this.message = message; 

} 

public ProgramModel(String title, String message, String imageUrl) { 
    this.title = title; 
    this.message = message; 
    this.imageUrl = imageUrl; 

} 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getMessage() { 
    return message; 
} 

int getImage() { 
    return image; 
} 

public void setImage(int image) { 
    this.image = image; 
} 

public String getImageUrl() { 
    return this.imageUrl; 
} 
} 

이 메인 레이아웃 코드 :

public class ProfileNewsFeedAdapter extends RecyclerView.Adapter<ProfileNewsFeedAdapter.MyViewHolder> { 

private AppCompatActivity activity; 
private ArrayList<ProgramModel> program; 
private LayoutInflater inflater; 

public ProfileNewsFeedAdapter(AppCompatActivity activity, ArrayList<ProgramModel> program) { 
    this.activity = activity; 
    inflater = activity.getLayoutInflater(); 
    this.program = program; 
} 

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
    TextView title, author; 
    ImageView bookImage; 

    MyViewHolder(View v) { 
     super(v); 
     title = (TextView) v.findViewById(R.id.bookTitleBorrowId); 
     author = (TextView) v.findViewById(R.id.bookAuthorBorrowId); 
     bookImage = (ImageView) v.findViewById(R.id.bookImageBorrowCardId); 
    } 

    void setMenuDetail(ProgramModel model, final int position) { 
     title.setText(model.getTitle()); 
     author.setText(model.getMessage()); 

     // Set text fonts 
     title.setTypeface(Typeface.createFromAsset(activity.getAssets(), "fonts/Roboto-Bold.ttf")); 
     author.setTypeface(Typeface.createFromAsset(activity.getAssets(), "fonts/Roboto-Light.ttf")); 

     Picasso.with(activity).load(model.getImageUrl()).into(bookImage); 

     bookImage.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.bookImageCardId: 
       new GetBookInfo(activity, title.getText().toString()).execute(); 
       break; 
     } 
    } 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v = inflater.inflate(R.layout.profile_cardview_item, parent, false); 
    return new MyViewHolder(v); 
} 

@Override 
public void onBindViewHolder(MyViewHolder holder, int position) { 
    ProgramModel menuModel = program.get(position); 
    holder.setMenuDetail(menuModel, position); 
} 

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

private class GetBookInfo extends AsyncTask<Void, Void, Void> { 

    private String bookTitle, bookAuthor, bookDescription, requestedBook, bookUrl, bookId; 
    private int copies; 
    private Context c; 

    GetBookInfo(Context c, String requestedBook) { 
     this.c = c; 
     this.requestedBook = requestedBook; 
    } 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected Void doInBackground(Void... voids) { 
     fetchBookInfo(); 
     return null; 
    } 

    private void fetchBookInfo() { 
     StringRequest stringRequest = new StringRequest(Request.Method.GET, AppConfig.URL_FETCH_BOOKS, new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       try { 
        JSONArray jsonArray = new JSONArray(response); 
        for (int i = 0; i < jsonArray.length(); i++) { 
         JSONObject jsonObject = jsonArray.getJSONObject(i); 
         if (jsonObject.getString("title").equals(requestedBook)) { 
          bookId = "" + jsonObject.getInt("id"); 
          bookTitle = jsonObject.getString("title"); 
          bookAuthor = jsonObject.getString("author"); 
          bookDescription = jsonObject.getString("description"); 
          copies = jsonObject.getInt("quantity"); 
          setBookUrl("http://ec2-52-39-232-168.us-west-2.compute.amazonaws.com/files/books/" + jsonObject.getString("cover")); 
          Intent intent = new Intent(c, BookActivity.class); 
          intent.putExtra("title", getBookTitle()); 
          intent.putExtra("author", getBookAuthor()); 
          intent.putExtra("bookId", getBookId()); 
          intent.putExtra("description", getBookDescription()); 
          intent.putExtra("copies", getCopies()); 
          intent.putExtra("description", getBookDescription()); 
          intent.putExtra("bookUrl", getBookUrl()); 
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          c.startActivity(intent); 
         } 
        } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
        Log.e("JSON Error: ", "" + e.getMessage()); 
       } 

      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       MyDynamicToast.errorMessage(AppController.getInstance(), "Volley Error!"); 
      } 
     }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(stringRequest); 
    } 

    String getBookTitle() { 
     return bookTitle; 
    } 

    String getBookAuthor() { 
     return bookAuthor; 
    } 

    String getBookDescription() { 
     return bookDescription; 
    } 

    String getBookId() { 
     return bookId; 
    } 

    int getCopies() { 
     return copies; 
    } 

    private void setBookUrl(String url) { 
     bookUrl = url; 
    } 

    String getBookUrl() { 
     return bookUrl; 
    } 

} 

} 

이 내가 항목의 datas를 얻을 수있는에서 ProgramModel 클래스는 다음과 같습니다

public class UserProfile extends AppCompatActivity { 

private UserData userData; 
private ProfileNewsFeedAdapter adapter; 
private ArrayList<ProgramModel> list; 
private RecyclerView profileNewsFeed; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.user_profile_activity); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    userData = new UserData(this); 
    list = new ArrayList<>(); 
    adapter = new ProfileNewsFeedAdapter(getActivity(), list); 

    initializeViews(); 
} 

private void initializeViews() { 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    toolbar.setTitle(userData.getName()); 

    TextView userEmail = (TextView) findViewById(R.id.emailPrfId); 
    TextView birthday = (TextView) findViewById(R.id.birthdayId); 
    TextView userClass = (TextView) findViewById(R.id.userClassId); 

    userEmail.setText(userData.getEmail()); 
    birthday.setText("Ditëlindja: " + userData.getBirthday()); 
    userClass.setText("Klasa: " + userData.getUserClass()); 
    setFont(userEmail); 
    setFont(birthday); 
    setFont(userClass); 

    profileNewsFeed = (RecyclerView) findViewById(R.id.profileNewsFeed); 
    profileNewsFeed.setLayoutManager(new LinearLayoutManager(this)); 
    profileNewsFeed.setItemAnimator(new DefaultItemAnimator()); 
    profileNewsFeed.setHasFixedSize(true); 
    profileNewsFeed.setAdapter(adapter); 


    GetBorrowedBooks getBorrowedBooks = new GetBorrowedBooks(); 
    getBorrowedBooks.execute(); 
} 

private void setFont(TextView textView) { 
    textView.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf")); 
} 

private AppCompatActivity getActivity() { 
    return this; 
} 


private class GetBorrowedBooks extends AsyncTask<Void, Void, Void> { 


    @Override 
    protected Void doInBackground(Void... voids) { 
     String userId = userData.getUserId(); 

     StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://ec2-52-39-232-168.us-west-2.compute.amazonaws.com/user/" + userId + "/requests", new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       try { 
        JSONArray jsonArray = new JSONArray(response); 

        if (jsonArray.length() > 0) { 
         for (int i = 0; i < jsonArray.length(); i++) { 
          JSONObject jsonObject = jsonArray.getJSONObject(i); 
          String imageName = "http://ec2-52-39-232-168.us-west-2.compute.amazonaws.com/files/books/" + jsonObject.getString("cover"); 

          list.add(new ProgramModel(jsonObject.getString("title"), jsonObject.getString("author"), imageName)); 
         } 
        } 

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

      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       MyDynamicToast.errorMessage(AppController.getInstance(), "Volley did not respond!"); 
      } 
     }); 

     AppController.getInstance().addToRequestQueue(stringRequest); 

     return null; 
    } 
} 

} 

어댑터 코드
<?xml version="1.0" encoding="utf-8"?> 
 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:background="@color/white" 
 
    android:fitsSystemWindows="true" 
 
    tools:context="com.libraryhf.libraryharryfultz.activity.UserProfile"> 
 

 
    <android.support.design.widget.AppBarLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:theme="@style/AppTheme.AppBarOverlay"> 
 

 
     <android.support.v7.widget.Toolbar 
 
      android:id="@+id/toolbar" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="?attr/actionBarSize" 
 
      android:background="?attr/colorPrimary" 
 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 
 

 
    </android.support.design.widget.AppBarLayout> 
 

 
    <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
 

 

 
     <LinearLayout 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:orientation="horizontal" 
 
      android:weightSum="4"> 
 

 
      <android.support.v7.widget.AppCompatImageView 
 
       android:id="@+id/userImageId" 
 
       android:layout_width="0dp" 
 
       android:layout_height="80dp" 
 
       android:layout_marginTop="3dp" 
 
       android:layout_weight="1" 
 
       android:src="@drawable/prf_image" /> 
 

 
      <LinearLayout 
 
       android:layout_width="0dp" 
 
       android:layout_height="wrap_content" 
 
       android:layout_marginTop="3dp" 
 
       android:layout_weight="3" 
 
       android:orientation="vertical" 
 
       android:padding="7dp"> 
 

 
       <android.support.v7.widget.AppCompatTextView 
 
        android:id="@+id/emailPrfId" 
 
        android:layout_width="wrap_content" 
 
        android:layout_height="wrap_content" /> 
 

 
       <android.support.v7.widget.AppCompatTextView 
 
        android:id="@+id/birthdayId" 
 
        android:layout_width="match_parent" 
 
        android:layout_height="wrap_content" 
 
        android:background="?attr/selectableItemBackground" 
 
        android:clickable="true" 
 
        android:paddingTop="7dp" /> 
 

 
       <android.support.v7.widget.AppCompatTextView 
 
        android:id="@+id/userClassId" 
 
        android:layout_width="match_parent" 
 
        android:layout_height="wrap_content" 
 
        android:background="?attr/selectableItemBackground" 
 
        android:clickable="true" 
 
        android:paddingTop="7dp" /> 
 

 
      </LinearLayout> 
 

 
     </LinearLayout> 
 

 
    </android.support.v4.widget.NestedScrollView> 
 

 
    <android.support.v7.widget.RecyclerView 
 
     android:id="@+id/profileNewsFeed" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     android:layout_marginTop="150dp" 
 
     android:scrollbars="none" /> 
 

 
    </android.support.design.widget.CoordinatorLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/colorAccent"> 

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/item_cardView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     card_view:cardBackgroundColor="@color/colorAccent" 
     card_view:cardCornerRadius="4dp" 
     card_view:cardElevation="5dp" 
     card_view:contentPadding="7dp"> 

     <android.support.v7.widget.LinearLayoutCompat 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <TextView 
       android:id="@+id/bookTitleBorrowId" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="start" 
       android:layout_margin="5dp" 
       android:text="@string/dummyText" 
       android:textColor="@color/white" 
       android:textSize="20sp" /> 


      <android.support.v7.widget.LinearLayoutCompat 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_marginStart="9dp" 
       android:layout_weight="7" 
       android:orientation="horizontal"> 

       <android.support.v7.widget.AppCompatImageView 
        android:layout_width="16dp" 
        android:layout_height="16dp" 
        android:layout_marginTop="1dp" 
        android:layout_weight="1" 
        app:srcCompat="@drawable/writer_icon" /> 

       <TextView 
        android:id="@+id/bookAuthorBorrowId" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginStart="10dp" 
        android:layout_weight="6" 
        android:text="@string/dummyText" 
        android:textColor="@color/white" 
        android:textSize="13sp" /> 

      </android.support.v7.widget.LinearLayoutCompat> 


      <ImageView 
       android:id="@+id/bookImageBorrowCardId" 
       android:layout_width="match_parent" 
       android:layout_height="200dp" 
       android:layout_centerHorizontal="true" 
       android:layout_marginBottom="10dp" 
       tools:ignore="ContentDescription" /> 

     </android.support.v7.widget.LinearLayoutCompat> 




    </android.support.v7.widget.CardView> 
    </LinearLayout> 

는 불행하게도 내 항목이 화면에 표시되지 않습니다 : (210)

이 단일 항목 레이아웃입니다. 나는 위아래로 애니메이션을 볼 수 있기 때문에 recyclerview가 보여졌지만 항목은 거기에 없다는 것을 알고 있습니다. 내가 어디서 잘못 된거야? 고맙습니다.

답변

0

새 요소를 목록에 추가 한 후에 adapter.notifyDataSetChanged();을 추가하십시오. 어댑터가 목록이 변경되었음을 알리지 않으면 데이터가 항상 시작 부분에 선언 된 빈 목록이되도록 recyclerview를 업데이트하지 않습니다.

+0

방금 ​​시도했지만 아직 항목이 표시되지 않습니다. –

+0

감사합니다. 그것이 문제였습니다. 왜 다른 활동에서 작동하는지 모르지만 헤이, IT 작동! –

0

doInBackground에서보기에 액세스하고 있으며 다른 스레드에서 doInBackground가 호출됩니다. onPostExecute(), ui 스레드에서 호출되는 AsyncTask 메서드에서 해당 뷰에 액세스해야합니다.

+0

나는 다른 활동에서 또 다른 recyclerview에서 똑같은 일을했으며 작동한다. 왜 여기 있니? –

관련 문제