0

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;

    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

4 Answers4

1

try this way

Context context; 
ArrayList<Numbers> englishNumbers;

public NumbersAdapter(Context context, ArrayList<Numbers> englishNumbers) 
{
  this.context=context;
  this.englishNumbers=englishNumbers;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    convertView= LayoutInflater.from(context).inflate(R.layout.listitem,parent,false);

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


    tamazight_item.setText(englishNumbers.get(position).getEnglishTranslation());
    english_item.setText(englishNumbers.get(position).getTamazightTranslation());

    return convertView;
}

And not forgot to make NumbersAdapter extends BaseAdapter

ajay dhadhal
  • 306
  • 4
  • 16
0

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

Tiago Ornelas
  • 1,109
  • 8
  • 21
  • Thank you Tiago i had struggled with this for along time..i already tried it without static but didn't work then..but now it Worked like Magic :) Thank You Soooo Much <3 – Brahim Koukin Feb 07 '19 at 19:11
0

I have changed your model class and Adapter class please follow this It will work

 public class Numbers {
    //First we Set the Stat of our Class :

    // English Translation
    private String englishTranslation;

    //Tamazight Translation
    private  String tamazightTranslation;

    public String getEnglishTranslation() {
        return englishTranslation;
    }

    public void setEnglishTranslation(String englishTranslation) {
        this.englishTranslation = englishTranslation;
    }

    public String getTamazightTranslation() {
        return tamazightTranslation;
    }

    public void setTamazightTranslation(String tamazightTranslation) {
        this.tamazightTranslation = tamazightTranslation;
    }
}


Adapter Class

    public class NumbersAdapter extends ArrayAdapter<Numbers> {
     private Context mContext;
     private ArrayList<Numbers>list;
     public NumbersAdapter(Context context, ArrayList<Numbers> englishNumbers) {
      super(context, 0, englishNumbers);
      this.mContext=context;
      this.list = englishNumbers;
    }

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

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

        Numbers numbers = list.get(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;

    }
}
Sandeep dhiman
  • 1,863
  • 2
  • 17
  • 22
0

If it is showing one last item in your listview, there might be several reasons for that.

  1. You need to override getCount() in ArrayAdapter to have size of the list. It may fix the problem.

  2. Make sure, the list view is not declared inside scrollview in your xml layout. If it is, handle it properly using nestedscrollview or else.

If it is showing the same data in all rows of your list view, then it might be

  1. In your case, you need to remove static keywords in front of properties, static means that properties or methods belong to Number class, not to the instance of your Number class object. Basically, it must be the reason.
Farruh Habibullaev
  • 2,342
  • 1
  • 26
  • 33