javaandroidarraylistandroid-listviewcustom-adapter

How to use String[] in custom adapter?


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


Solution

  • I think your problem is trying to set array as a text to TextView like below code.

    arabictext.setText(getItem(position).getArabic(arabica));
    transliteration.setText(getItem(position).getTransliteration());
    translation.setText(getItem(position).getTranslation());
    

    If you want to get String for a specific index you can just use it's index. For example:

    arabictext.setText(getItem(position).getArabic()[0]);
    transliteration.setText(getItem(position).getTransliteration()[0]);
    translation.setText(getItem(position).getTranslation()[0]);
    

    Or If you want to get all values from String[] as one String and set it to your TextView you can create a method which gets an String[] and returns String.

    private String getString(String[] array){
        String text = "";
        for (String s : array) {
            text += s + ", ";
        }
        return text;
    }
    

    So inside your adapter define a method like above and whenever you need a String from String[] use that method.

    public class PerrowAdapter extends ArrayAdapter<Perrow> {
    
        private Context mContext;
        private int mResource;
        private ArrayList<Perrow> objects;
    
        public PerrowAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Perrow> objects) {
            super(context, resource, objects);
            this.mContext = context;
            this.mResource = resource;
            this.objects = objects;
        }
    
        private String getString(String[] array){
            String text = "";
            for (String s : array) {
                text += s + ", ";
            }
            return text;
        }
    
        @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 tvArabic = convertView.findViewById(R.id.arabictext);
            TextView tvTransliteration = convertView.findViewById(R.id.transliteration);
            TextView tvTranslation = convertView.findViewById(R.id.translation);
    
            Perrow perrow = objects.get(position);
            String[] arabica = perrow.getArabic();
            String[] transliteration = perrow.getTransliteration();
            String[] translation = perrow.getTranslation();
    
            tvArabic.setText(getString(arabica));
            tvTransliteration.setText(getString(transliteration));
            tvTranslation.setText(getString(translation));
    
            return convertView;
        }
    }
    

    EDIT

    i want as per row of the listview, show each item in the string array, >accordingly. how can this be achieved ?

    You should change your Perrow class and instead of String[] use String and populate your ArrayList with each elements of String[].

    Perrow.java (use String instead of String[])

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

    PerrowAdapter.java (get() methods from Perrow class now return String so you can directly use them for TextViews)

    public class PerrowAdapter extends ArrayAdapter<Perrow> {
    
        private Context mContext;
        private int mResource;
        private ArrayList<Perrow> objects
    
        public PerrowAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Perrow> objects) {
            super(context, resource, objects);
            this.mContext = context;
            this.mResource = resource;
            this.objects = objects;
        }
    
        @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 tvArabic = convertView.findViewById(R.id.arabictext);
            TextView tvTransliteration = convertView.findViewById(R.id.transliteration);
            TextView tvTranslation = convertView.findViewById(R.id.translation);
    
            Perrow perrow = objects.get(position);
    
            tvArabic.setText(perrow.getArabic());
            tvTransliteration.setText(perrow.getTransliteration());
            tvTranslation.setText(perrow.getTranslation());
    
            return convertView;
        }
    }
    

    I don't know how do you fill your String[] but I assume all three String[] have same length (arrays contain same number Strings), so you can add all Strings in to ArrayList with a for loop and use that ArrayList for your adapter.

    Your Activiy (where do you call PerrowAdapter)

    String[] arabica = getContext().getResources().getStringArray(R.array.fontsize);
    String[] trasliteration = ... //Get your trasliteration array
    String[] translation = ... //Get your translation array
    
    ArrayList<Perrow> list = new ArrayList<>();
    for(int i = 0; i < arabica.length; i++){
        list.add(new Perrow(arabica[i], transliteration[i], translation[i]));
    }
    
    PerrowAdapter adapter = new PerrowAdapter(context, resource, list);