0

내 주요 활동에는 학생 이름과 학생용으로 텍스트를 편집하는 텍스트가 있으며, 학생 추가 버튼이 있습니다. 다음 코드는 학생 추가 기능을위한 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에 그렇게 목록보기의 각 행은 그래서 나는 다음과 같은 코드로 학생 배열 어댑터에 배열 어댑터 클래스를 정의하고있어이

ListViewRow.xml

과 같을 것이다 사용자 정의하는 것입니다 :

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); 

// 다음에 할 일 ???

} 

}

내가 내 학생 어댑터를 완료 할 수있는 방법 학생 행 각 텍스트보기의 값을 설정하려면?

+0

희망이 도움이 : https : // stackoverflow.co.kr/questions/26800762/issue-in-setting-textviews-text-in-custom-adaptor-for-android –

답변

0

좋아, 내가이 권리를 가지고 있는지 보자.

당신은 학생 이 배열의 배열을 가지고, 당신은 당신이 배치에서 많은 ListItems (R.layout.student_row) 에 대한 사용자 정의 XML 레이아웃이 목록 로 사용자 정의 어댑터에 전달합니다, 당신은 TextViews에있다 (이름 및 대학)

이제 목록보기의 항목에 학생 목록의 데이터가 표시됩니다. 이렇게하려면 다음을 수행하십시오

@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; 

} 

설명 :

학생 학생 =의 getItem (위치);

//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; 
} 

dapter는 목록의 모든 요소를 ​​반복합니다. 따라서 학생 목록에 항목이있는만큼 많은 목록 항목이됩니다.

(사용자 정의 어댑터에서 확장 한) 배열 어댑터를 Ctrl + 클릭하면 ArrayAdapter Java 클래스가 표시됩니다. 여기서 getPosition() 함수를 검색 할 수 있습니다.

/** 
    * 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); 
    } 

즉, 학생 목록의 현재 위치에서 학생을 받게됩니다.

+0

내가 그 점을 알았지 만 설명 할 수 있었다 학생 student = getItem (position); –

+0

나는 설명을 추가했는데, 충분하든, 아니면 더 = D –

+0

이 필요한지 알려주는 것이 매우 도움이되었다. 매우 감사드립니다. –