0

in my main activity there are tow edit texts one for student name and one for student college, also there is an Add student button, as the following code the onClick function for the Add student button will insert the values of the edit texts to a list view :

public class Students extends AppCompatActivity {

//Initializing ...
EditText NameEditText;
EditText CollegeEditText;
Button AddButton;
ListView StudentListView;
ArrayList<Student> list;
ArrayAdapter<Student> adapter;
Random rand = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_students);

//Getting references ...

AddButton=(Button) findViewById(R.id.AddBtn);
NameEditText=(EditText) findViewById(R.id.StudentName);
CollegeEditText=(EditText) findViewById(R.id.College);
StudentListView=(ListView) findViewById(R.id.StudentListView);

//define the array list of students  .
list=new ArrayList<Student>();

//set the communication between the ArrayList and the adapter.



adapter=new ArrayAdapter<Student>(this,android.R.layout.simple_list_item_1,list);
//set the communication between the Adapter and the ListView.



    StudentListView.setAdapter(adapter);

OnClickListener listener=new OnClickListener() {
    @Override
    public void onClick(View v) {
        int ID=rand.nextInt(50) + 1;
        Student student=new Student();
        student.setID(ID);
        student.setName(NameEditText.getText().toString());
        student.setCollege(CollegeEditText.getText().toString());

        list.add(student);
        NameEditText.setText("");
        CollegeEditText.setText("");
        adapter.notifyDataSetChanged();
    }
};
    AddButton.setOnClickListener(listener);

}}

As you can see I've created a student class and list of student and used Array adapter to add the values to the ListView.

What I really want is to custom my ListView so each row of the list view would look like this

ListViewRow.xml

So i'm customizing the Array Adapter class to Student Array Adapter as the following code :

class StudentAdapter extends ArrayAdapter <Student>{

public Context context;
public List<Student>students;




public StudentAdapter(@NonNull Context context, List<Student> list) {
    super(context,R.layout.student_row, list);
    this.context=context;
    this.students=list;

}

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

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.student_row, parent, false);
    TextView name = (TextView) rowView.findViewById(R.id.Name);
    TextView college = (TextView) rowView.findViewById(R.id.College);

//what to do next???

}

}

I want to set the value of each Text View in the Student Row how can I complete my student Adapter?

Samir Bhatt
  • 3,041
  • 2
  • 25
  • 39
Yousef Alaqra
  • 35
  • 1
  • 1
  • 11
  • hope this helps: https://stackoverflow.com/questions/26800762/issue-in-setting-textviews-text-in-custom-adapter-for-android – Sara Tirmizi Nov 16 '17 at 09:29

1 Answers1

0

Ok, let me see if i got this right.

You have an array of Students This array, you will pass to the Custom adapter as a List You have a custom XML Layout for the ListItems (R.layout.student_row) IN this Layout, you have to TextViews (Name and college)

Now what you want to do, is display the data from the students list in the Item of the List View. To do so, try the following:

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

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.student_row, parent, false);
    TextView name = (TextView) rowView.findViewById(R.id.Name);
    TextView college = (TextView) rowView.findViewById(R.id.College);

   Student student = getItem(position);

   name.setText(student.getname());
   college.setText(student.getCollege());

   return rowView;

}

Explanation:

Student student = getItem(position);

//You are passing a List of Students to the Custom Adapter
public StudentAdapter(@NonNull Context context, List<Student> list) {
    super(context,R.layout.student_row, list);
    this.context=context;
    this.students=list;
}

The dapter will iterate through all the Elements in the list. Therefore, ther will be as many List Items as there are Items in the List of Students.

If you CTRL + Click on the Array Adapter (that you extend in the custom adapter) you will get to the ArrayAdapter Java Class. Here you can search for the getPosition() function.

 /**
     * Returns the position of the specified item in the array.
     *
     * @param item The item to retrieve the position of.
     *
     * @return The position of the specified item.
     */
    public int getPosition(@Nullable T item) {
        return mObjects.indexOf(item);
    }

This means, that you will get the Student at the current position of the Student List.