javaandroidarraylistandroid-listviewcustom-arrayadapter

Listview show only last item over and over


I am new to android development. I enrolled Udacity course and I get to where I have to use a costume arrayAdapter to display a listView of data (text) I nailed most of it but i have a problem with data displayed which is : when I launch my activity listView displays last item on may arraylist i have tried almost everything out their nothing worked for me

My Data

/**
 * Displays text to the user.
 */
public class Numbers {


    //First we Set the Stat of our Class :

    // English Translation
    public static String englishTranslation;

    //Tamazight Translation
    public static String tamazightTranslation;

    /**
     * Create a new Numbers Object :
     *
     * @param englishTranslation   is for ENGLISH NUMBERS
     * @param tamazightTranslation is for TAMAZIGHT NUMBERS
     */

    //Constructor
    public Numbers(String englishTranslation, String tamazightTranslation) {


        this.englishTranslation = englishTranslation;
        this.tamazightTranslation = tamazightTranslation;

    }

    //Getter
    public static String getEnglishTranslation() {
        return englishTranslation;
    }


    public static String getTamazightTranslation() {
        return tamazightTranslation;
    }


}


My ArrayList :

public class ActivityNumbers extends AppCompatActivity {

    //Add English numbers to The ARRAYList
    ArrayList<Numbers> englishNumbers = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numbers);



        englishNumbers.add(new Numbers("One", "Yan"));
        englishNumbers.add(new Numbers("Two", "Sin"));
        englishNumbers.add(new Numbers("Three", "Krad"));
        englishNumbers.add(new Numbers("Four", "Koz"));
        englishNumbers.add(new Numbers("Five", "Smos"));
        englishNumbers.add(new Numbers("Six", "Sdis"));
        englishNumbers.add(new Numbers("Seven", "Sa"));
        englishNumbers.add(new Numbers("Eight", "Tam"));
        englishNumbers.add(new Numbers("Nine", "Tza"));
        englishNumbers.add(new Numbers("Ten", "Mraw"));

        //EnglishNumbers.remove(0);
        //EnglishNumbers.size();
        // EnglishNumbers.get(1);
        //Create a NEW TV object




        /**Creating an ArrayAdapter (DATA HOLDER)
         @param Context / the ACTIVITY concerned
         @param Android.R.layout : an XML layout file contains TextView predefined by Android
         @param Items to display */


        NumbersAdapter itemsAdapter = new NumbersAdapter (this, englishNumbers);

        // Linking the ListView object to a Variable
        ListView numbersListView = (ListView) findViewById(R.id.list);
        //Calling setAdapter Method on numbersListView with "itemsAdapter
        numbersListView.setAdapter(itemsAdapter);


    }


}


My Adapter :

public class NumbersAdapter extends ArrayAdapter<Numbers> {

    public NumbersAdapter(Context context, ArrayList<Numbers> englishNumbers) {
        super(context, 0, englishNumbers);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.listitem, parent, false);
        }

        Numbers numbers = getItem(position);


        System.out.println(position);

        TextView tamazight_item = (TextView) listItemView.findViewById(R.id.tamazight_item);
        TextView english_item = (TextView) listItemView.findViewById(R.id.english_item);


        tamazight_item.setText(numbers.getEnglishTranslation());
        System.out.println(numbers.getEnglishTranslation());

        english_item.setText(numbers.getTamazightTranslation());

        System.out.println(numbers.getTamazightTranslation());


        return listItemView;

    }
}

Solution

  • Your tamazightTranslation and englishTranslation attributes are static on the Numbers class meaning that, those are class attributes. Remove static from the variables and from the getters and you should be fine. See the difference between class attributes and instance attributes here https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html