无法将字符串数组显示到自定义Recycler View?

时间:2016-08-14 15:27:07

标签: android android-studio android-xml

当我在网站上看到很多教程时,他们大多数都在java中定义String数据,如String [] data = {“value”,“value2”,“value3”};但它不是实时应用中的正确方法我想。在我的应用程序中,我也将使用其他语言,所以我需要在xml中定义。无论我使用下面的代码调用它们:

onCreateView

 String[] eArray=getActivity().getResources().getStringArray(R.array.english_names);

并将其称为

 names.add(new Name(eArray));

但当我将鼠标悬停在eArray上时,它显示的错误如Name中的Name(java.lang.String)无法应用于(java.lang.String [])

我知道我必须将其他值传递给数组但是如何?我改变了错误行,它不断显示其他和其他。我谷歌很多tuts网站,但所有这些都是务实地定义字符串。

我制作了一个自定义的recyclerview,下面是它的代码


cardview.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="@android:color/transparent"
android:elevation="0dp">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:paddingLeft="10dp"
    android:textColor="#4928ef"
    android:textStyle="italic"/>
</android.support.v7.widget.CardView>

recyclerview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearlayout"
android:background="@drawable/big_img_boy_wp">
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rv">
</android.support.v7.widget.RecyclerView>

</LinearLayout>

cardview.java

public class cardView extends Fragment {
TextView textView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.card_view,container,false);
    textView= (TextView) view.findViewById(R.id.textView);
   // Typeface typeface=Typeface.createFromAsset(getActivity().getAssets(),"fonts/Cavorting.otf");
  //  textView.setTypeface(typeface);
    return  view;

}
}

Name.java

public class Name {
String textView;
Name(String textView){
    this.textView=textView;
}
}

recycler.java

public class Recycler extends Fragment {
private List<Name> names;
RecyclerView rv;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.recyclerview,container,false);
    rv= (RecyclerView) view.findViewById(R.id.rv);
    String[] eArray=getActivity().getResources().getStringArray(R.array.english_names);
    LinearLayoutManager llm=new LinearLayoutManager(getActivity());
    rv.setLayoutManager(llm);
    rv.setHasFixedSize(true);
    initializeData();
    initializeAdapter();
    return view;
}
private void initializeAdapter() {
 rvadapter adapter=new rvadapter(names);
    rv.setAdapter(adapter);
}

public void initializeData() {
    names=new ArrayList<>();
    names.add(new Name("Anish"));
    names.add(new Name(eArray));
}

}

rvadapter.java

public class rvadapter extends RecyclerView.Adapter<rvadapter.NameViewHolder> {
public static  class NameViewHolder extends RecyclerView.ViewHolder{
    CardView cv;
    TextView textView;

    public NameViewHolder(View itemView) {
        super(itemView);
        cv= (CardView) itemView.findViewById(R.id.cardview);
        textView= (TextView) itemView.findViewById(R.id.textView);

    }
}
List<Name> names;
public rvadapter(List<Name> names) {
    this.names=names;


}

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

@Override
public NameViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false);
    NameViewHolder nvh=new NameViewHolder(v);
    return nvh;
}

@Override
public void onBindViewHolder(NameViewHolder holder, int position) {
    holder.textView.setText(names.get(position).textView);

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

还请看看我的cardview.java,我把评论,这个字体也没有改变。我把字体放在资产然后字体文件夹。这是我在stackoverflow中的第二个问题,但我的第一个问题是没有正确回答,如果你可以请看看Click here to to look by another problem too.提前致谢!!

1 个答案:

答案 0 :(得分:0)

        for (int i = 0; i < eArray.length; i++) {
            names.add(new Name(eArray[i]));
        }