0
Im跟在this tutorial後,我仔細地按照指示操作,但遇到了一些問題。在我的ItemView控件的構造函數,cardview和照片是 「無法解析的符號」:在RecyclerView/CardView中解析2個符號並添加元素
ItemViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cardview);
title = (TextView)itemView.findViewById(R.id.title);
cover = (ImageView)itemView.findViewById(R.id.photo);
}
這裏是整個適配器類:
package com.example.aaronpries.myfinal;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder>{
private Context context;
private List<Item> books;
ItemAdapter(List<Item> books){
this.books = books;
}
@Override
public int getItemCount() {
return books.size();
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_cal, viewGroup, false);
ItemViewHolder bvh = new ItemViewHolder(v);
return bvh;
}
@Override
public void onBindViewHolder(ItemViewHolder bookViewHolder, int i) {
bookViewHolder.title.setText(books.get(i).text);
bookViewHolder.cover.setImageResource(books.get(i).imgID);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class ItemViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView title;
ImageView cover;
ItemViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cardview);
title = (TextView)itemView.findViewById(R.id.title);
cover = (ImageView)itemView.findViewById(R.id.photo);
}
}
}
這是我的物品類別:
package com.example.aaronpries.myfinal;
import java.io.Serializable;
public class Item implements Serializable{
public int id;
public String text;
public int imgID;
}
這裏是recyclerview xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerview"
/>
</LinearLayout>
該活動的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="16dp"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cardview"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/photo"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:layout_toRightOf="@+id/photo"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
最後Activity類:
public class CalActivity extends AppCompatActivity {
String[] events = {"A","B","C","D","E","F","G"};
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cal);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView recycler = (RecyclerView)findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
recycler.setLayoutManager(layoutManager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
我試圖按照有關如何獲得這個RecyclerView工作了幾個不同的教程,但其中大部分唐最終沒有顯示完整的代碼,而且因爲我是如此的新,所以我有時候很難接受。我也想添加一個圖像和標題文本到RecyclerView,所以如果你有建議,我會很感激。我是否一次又一次地使用Item.add(「text」,imgID)來填充數據?
'@ + id/photo'將使您可以在佈局中查找'R.id.photo'或引用。 'android:id'不是必需的屬性,雖然 –
另外,當你有'layout_toRightOf =「@ + id/photo」'時,它應該只是'layout_toRightOf =「@id/photo」'。沒有加號 –