내 주요 활동에는 학생 이름과 학생용으로 텍스트를 편집하는 텍스트가 있으며, 학생 추가 버튼이 있습니다. 다음 코드는 학생 추가 기능을위한 onClick 함수입니다. 버튼은 목록보기로 편집 텍스트의 값을 삽입합니다 :ListView에 편집 문구 값을 추가하는 맞춤 배열 어댑터
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);
}}
...
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);
// 얻기 참조 당신은 내가 학생 클래스와 학생의 목록을 만들었습니다 볼 수 있듯이 배열 어댑터를 사용하여 값을 ListView에 추가합니다.
내가 정말 원하는 것은 내 ListView에 그렇게 목록보기의 각 행은 그래서 나는 다음과 같은 코드로 학생 배열 어댑터에 배열 어댑터 클래스를 정의하고있어이
과 같을 것이다 사용자 정의하는 것입니다 :
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);
// 다음에 할 일 ???
}
}
내가 내 학생 어댑터를 완료 할 수있는 방법 학생 행 각 텍스트보기의 값을 설정하려면?
희망이 도움이 : https : // stackoverflow.co.kr/questions/26800762/issue-in-setting-textviews-text-in-custom-adaptor-for-android –