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
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?