Im using CardView to create a list of cards with newsletters. The basic elements are 3 TextViews + 1 ImageView. I cant inflate 2 elements properly: the title is fine, but the link text the gets the wrong string(it gets the string referring to the body text) and the body TextView dont change at all.
Aeres a picture with the general ideia:
preview on android studio (left) and app running on device(right)
This is my CardView XML code:
<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 list layout XML code:
<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"/>
Now the java classes: Here im populating the list, calling the adapter
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);
}
}
My classe to create the list object, few getters
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;
}
}
Inflating the layout
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);
}
}
}
Looks like a typo error. Try to replace your NewsRVAdapter
's onBindViewHolder
with:
@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());
}
The reason is your wrong code in 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 was assigned two times so its value was overriden.
Correct it and your code work fine :)
You have a mistake on onBindViewHolder
. You were referencing to articleLink
twice.
it should have been
@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());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.