2012-12-16 2 views
1

내 listview에 대한 사용자 정의 레이아웃 항목이 있습니다.
이 방법과 관련된 문제는 일반 사용자가 값을 수정할 수 없다는 점입니다.이 값은 내가 포함하고 싶은 값입니다. 레이아웃 항목과 후속 회 전자가 같은 listview에서 여러 번 반복되므로 프로그래밍 방식으로 한 번 채울 수있는 방법이 있어야한다고 생각합니다. 나는 그것을 이해할 수 없다.사용자 정의 레이아웃이있는 스피너의 동적 채우기

XML :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/customListItem" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/exerciselbl" /> 

     <Spinner 
      android:id="@+id/Exercise" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:entries="@array/workout_items" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/repslbl" /> 

     <Spinner 
      android:id="@+id/Reps" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:entries="@array/reps_count" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/weightlbl" /> 

     <EditText 
      android:id="@+id/Weight" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:inputType="number" /> 
    </LinearLayout> 

</LinearLayout> 

그래서 그래, 나는 이것이 XML 리소스 파일에서 회 전자에 대한 모든 단일 항목을 직접 입력하는 의미로 android:entries="@array/workout_items"을 사용하지 않도록 할 AND I 동적 동안 항목을 추가 할 수 없습니다 프로그램이 실행 중입니다.

답변

0

다음은 어떻게 수행 될지에 대한 간단한 예입니다. 제네릭이 변경 될 수 있으며 회 전자를로드하는 다른 방법이 있습니다. 더 중요한 라인의

public void populateSpinner() { 
    Spinner spinner = (Spinner) findViewById(R.id.spinner1); 
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(adapter); 
    List<CharSequence> list = new ArrayList<CharSequence>(); 
    for (int i = 0; i < 10; i++) { 
     list.add("Item " + i); 
    } 
    adapter.addAll(list); 
    adapter.notifyDataSetChanged(); 
} 

하나는 그것이이 스피너와 관련된 데이터를 새로 고칠 필요가 있다는 견해를 알 수 있기 때문입니다, adapter.notifyDataSetChanged()이다.

자세한 내용은 ArrayAdapterSpinner 클래스를 참조하십시오.

+0

그는 행 레이아웃에있는 '회 전자 (Spinner)'에 대해 말하고 있으며 현재 코드는 이에 적합하지 않습니다. 또한,'add() '와'addAll()'(API 레벨 11에서 사용 가능하다.)이 이미'notifyDataSetChanged()'를 호출하므로 마지막 호출은 쓸모가 없다. – Luksprog

+0

add()'''와'''addAll()''''notifyDataSetChanged()'''가 API에 문서화되지 않은 것처럼 보였습니다. 나는 그 방법으로 회 전자를 찾는 것을 제외하고는 나의 예가 여전히 유효하다고 믿는다. 대신, 나는 비슷한 것을 할 것이라고 생각한다 : '''LinearLayout row = ((ListView) findViewById (R.id.listview1)). getItemAtPosition (position);''' ''(회 전자) 행. 즉, 지금 당장 테스트 할 시간이 없으므로 추측 일뿐입니다. – TomJ

+0

고마워요, 이건 제가 가고있는 줄과 비슷합니다. 나는 그걸 작동시킬 수 없었습니다. 이 메서드의 중요성은 View가 실제로 초기화 된 것입니다. 보기가 아직 보이지 않기 때문에'onCreate()','onStart()'또는'onResume()'에서 스피너를 찾으려고 노력하지 않습니다 ... 내가 발견 한 문제는 여전히해야 할 것입니다. ListView에서 각 Spinner에 대해 이렇게하면 XML 또는 비슷한 것으로 참조 할 수있는 onCreate()에서 만든 Resource와 같이 좀 더 일반적인 것을 기대하고 있습니다. 실제로 가능하다고 생각하지 않습니다. 봤어.) – Trent

관련 문제