I make a custom adapter, that should call the custom arraylist java class, so that it will show 3 seperate string array. However, if i put the String[] in the custom arraylist java class, it will not work with the custom adapter, as getitem(position) will need int type of getArabic. So how do i make it ?
here is my custom arraylist java
public class Perrow {
String[] Arabic;
String[] transliteration;
String[] translation;
public Perrow(String[] arabic, String[] transliteration, String[] translation) {
Arabic = arabic;
this.transliteration = transliteration;
this.translation = translation;
}
public String[] getArabic() {
return Arabic;
}
public void setArabic(String[] arabic) {
Arabic = arabic;
}
public String[] getTransliteration() {
return transliteration;
}
public void setTransliteration(String[] transliteration) {
this.transliteration = transliteration;
}
public String[] getTranslation() {
return translation;
}
public void setTranslation(String[] translation) {
this.translation = translation;
}
}
and my custom adapter
public class PerrowAdapter extends ArrayAdapter<Perrow> {
private Context mContext;
private int mResource;
public PerrowAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Perrow> objects) {
super(context, resource, objects);
this.mContext = context;
this.mResource = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(mResource, parent, false);
TextView arabictext = convertView.findViewById(R.id.arabictext);
TextView transliteration = convertView.findViewById(R.id.transliteration);
TextView translation = convertView.findViewById(R.id.translation);
String[] arabica = getContext().getResources().getStringArray(R.array.fontsize);
arabictext.setText(getItem(position).getArabic(arabica));
transliteration.setText(getItem(position).getTransliteration());
translation.setText(getItem(position).getTranslation());
return convertView;
}
}
really appreciate help thank you everyone
I think your problem is trying to set
arrayas a text toTextViewlike below code.If you want to get String for a specific index you can just use it's index. For example:
Or If you want to get all values from
String[]as oneStringand set it to yourTextViewyou can create a method which gets anString[]and returnsString.So inside your adapter define a method like above and whenever you need a
StringfromString[]use that method.EDIT
You should change your
Perrowclass and instead ofString[]useStringand populate yourArrayListwith each elements ofString[].Perrow.java (use
Stringinstead ofString[])PerrowAdapter.java (
get()methods fromPerrowclass now returnStringso you can directly use them forTextViews)I don't know how do you fill your
String[]but I assume all threeString[]have same length (arrays contain same numberStrings), so you can add allStringsin toArrayListwith aforloop and use thatArrayListfor your adapter.Your Activiy (where do you call
PerrowAdapter)