gpt4 book ai didi

java - 回收站 View + 卡片 View : Inflating Wrong Data

转载 作者:行者123 更新时间:2023-11-29 19:45:33 24 4
gpt4 key购买 nike

我正在使用 CardView 创建带有时事通讯的卡片列表。基本元素是 3 个 TextView + 1 个 ImageView。我无法正确膨胀 2 个元素:标题很好,但是链接文本得到了错误的字符串(它得到了引用正文文本的字符串)并且正文 TextView 根本没有改变。

Aeres 一张具有一般想法的图片:

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类:这里我正在填充列表,调用适配器

 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);
}
}

我创建列表对象的类,很少有 getter

    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;
}
}

inflated 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);
}
}
}

最佳答案

看起来像是打字错误。尝试将您的 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());
}

关于java - 回收站 View + 卡片 View : Inflating Wrong Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37802619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com