1

我正在使用CardView創建帶通訊的卡片列表。基本元素是3個TextViews + 1 ImageView。我不能正確膨脹2個元素:標題是好的,但鏈接文本獲取錯誤的字符串(它獲取字符串引用正文)和正文TextView根本不會改變。RecyclerView + CardView:給錯誤的數據填充

AERES與一般IDEIA圖片:

preview on android studio (left) and app running on device(right)

這是我CardView XML代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
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:orientation="vertical" 
tools:context=".News"> 

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card1_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardCornerRadius="4dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/news_title" 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:paddingLeft="8dp" 
      android:text="articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle " 
      android:textColor="#000000" 
      android:textSize="16sp" 
      android:textStyle="bold" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="150dp" 
      android:orientation="horizontal"> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="6" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/news_link" 
        android:layout_width="match_parent" 
        android:layout_height="25dp" 
        android:gravity="center_vertical" 
        android:paddingLeft="8dp" 
        android:text="http://www.articleLink.com/" /> 

       <TextView 
        android:id="@+id/news_body" 
        android:layout_width="match_parent" 
        android:layout_height="120dp" 
        android:paddingLeft="8dp" 
        android:text="Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body ..." 
        android:textColor="#000000" /> 
      </LinearLayout> 

      <ImageView 
       android:id="@+id/news_photoId" 
       android:layout_width="0dp" 
       android:layout_height="145dp" 
       android:layout_weight="5" 
       android:src="@drawable/enfnotum" /> 

     </LinearLayout> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="20dp" 
      android:gravity="end" 
      android:paddingEnd="6dp" 
      android:paddingRight="6dp" 
      android:text="Coments Share" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="#bab7b7" /> 

    </LinearLayout> 


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

RecycleView列表佈局XML代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".News"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/rv" 
    android:scrollbars="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

現在的Java類: 這裏IM填充列表,呼叫轉接

public class News extends AppCompatActivity { 

    private List<Article> articles = new ArrayList<>(); 
    private RecyclerView rv; 

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

     rv = (RecyclerView) findViewById(R.id.rv); 
     LinearLayoutManager llm = new LinearLayoutManager(this); 
     rv.setLayoutManager(llm); 

     initializeData(); 
     initializeAdapter(); 
    } 

    private void initializeData() { 
     articles.add(new Article("0title", "0link", "0body", R.drawable.enfnotum)); 
     articles.add(new Article("1title", "1link", "1body", R.drawable.enfnoticias)); 
     articles.add(new Article("2title", "2link", "2body", R.drawable.image1)); 
     articles.add(new Article("3title", "3link", "3body", R.drawable.image2)); 
     articles.add(new Article("4title", "4link", "4body", R.drawable.image3)); 
     articles.add(new Article("5title", "5link", "5body", R.drawable.enfnotum)); 
     articles.add(new Article("6title", "6link", "6body", R.drawable.image3)); 
     articles.add(new Article("7title", "7link", "7body", R.drawable.enfnoticias)); 
     //TODO: database 
    } 

    private void initializeAdapter() { 
     NewsRVAdapter adapter = new NewsRVAdapter(articles); 
     rv.setAdapter(adapter); 
    } 
} 

我CLASSE創建列表對象,幾個干將

public class Article { 

    private String articleTitle; 
    private String articleLink; 
    private String articleBody; 
    private int articlePhotoId; 

    public Article(String articleTitle, String articleLink, String articleBody, int articlePhotoId) { 
     this.articleTitle = articleTitle; 
     this.articleLink = articleLink; 
     this.articleBody = articleBody; 
     this.articlePhotoId = articlePhotoId; 
    } 

    public String getArticleTitle() { 
     return articleTitle; 
    } 

    public String getArticleLink() { 
     return articleLink; 
    } 

    public String getArticleBody() { 
     return articleBody; 
    } 

    public int getArticlePhotoId() { 
     return articlePhotoId; 
    } 
} 

充氣佈局

public class NewsRVAdapter extends RecyclerView.Adapter<NewsRVAdapter.ArticleViewHolder> { 

    List<Article> articles; 

    NewsRVAdapter(List<Article> articles) { 
     this.articles = articles; 
    } 

    @Override 
    public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
     super.onAttachedToRecyclerView(recyclerView); 
    } 

    @Override 
    public ArticleViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) { 
     View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item_list, viewGroup, false); 
     return new ArticleViewHolder(v); 
    } 

    @Override 
    public void onBindViewHolder(ArticleViewHolder ArticleViewHolder, int position) { 
     ArticleViewHolder.articleTitle.setText(articles.get(position).getArticleTitle()); 
     ArticleViewHolder.articleLink.setText(articles.get(position).getArticleLink()); 
     ArticleViewHolder.articleLink.setText(articles.get(position).getArticleBody()); 
     ArticleViewHolder.articlePhotoID.setImageResource(articles.get(position).getArticlePhotoId()); 
    } 

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

    public static class ArticleViewHolder extends RecyclerView.ViewHolder { 

     CardView cv; 
     TextView articleTitle; 
     TextView articleLink; 
     TextView articleBody; 
     ImageView articlePhotoID; 

     ArticleViewHolder(View itemView) { 
      super(itemView); 
      cv = (CardView) itemView.findViewById(R.id.card1_view); 
      articleTitle = (TextView) itemView.findViewById(R.id.news_title); 
      articleLink = (TextView) itemView.findViewById(R.id.news_link); 
      articleBody = (TextView) itemView.findViewById(R.id.news_body); 
      articlePhotoID = (ImageView) itemView.findViewById(R.id.news_photoId); 
     } 
    } 
} 

回答

1

Look就像一個錯字錯誤。嘗試更換您的NewsRVAdapteronBindViewHolder有:

@Override 
public void onBindViewHolder(ArticleViewHolder ArticleViewHolder, int position) { 
ArticleViewHolder.articleTitle.setText(articles.get(position).getArticleTitle()); 
ArticleViewHolder.articleLink.setText(articles.get(position).getArticleLink()); 
ArticleViewHolder.articleBody.setText(articles.get(position).getArticleBody()); 
ArticleViewHolder.articlePhotoID.setImageResource(articles.get(position).getArticlePhotoId()); 
} 
+0

我不能相信...太多了 – RCO

0

您對onBindViewHolder一個錯誤。您引用了articleLink兩次。

它應該是

@Override 
public void onBindViewHolder(ArticleViewHolder ArticleViewHolder, int position){ 
    ArticleViewHolder.articleTitle.setText(articles.get(position).getArticleTitle()); 
    ArticleViewHolder.articleLink.setText(articles.get(position).getArticleLink()); 
    ArticleViewHolder.articleBody.setText(articles.get(position).getArticleBody()); 
    ArticleViewHolder.articlePhotoID.setImageResource(articles.get(position).getArticlePhotoId()); 
} 
1

原因是在你的錯誤代碼onBindViewHolder ^^

@Override 
public void onBindViewHolder(ArticleViewHolder ArticleViewHolder, int position) { 
    ArticleViewHolder.articleTitle.setText(articles.get(position).getArticleTitle()); 
    ArticleViewHolder.**articleLink**.setText(articles.get(position).getArticleLink()); 
    ArticleViewHolder.**articleLink**.setText(articles.get(position).getArticleBody()); 
    ArticleViewHolder.articlePhotoID.setImageResource(articles.get(position).getArticlePhotoId()); 
} 

ArticleViewHolder。 articleLink被分配了兩次,所以它的值被覆蓋。

改正它和你的代碼工作正常:)