2013-12-10 3 views
0

"textview", "edittext", "button"및 "listview"가 포함 된 동적 화면을 생성하려고합니다. edittext에 쓰여진 내용을 내 listview에 추가하려고합니다. 오류가 발생하지 않고 코드를 디버깅 할 때 ArrayList 내부에 추가 된 항목을 볼 수 있습니다. 그러나 listview는 목록에 추가 된 첫 번째 항목 만 표시합니다.동적으로 추가 된 ListView는 첫 번째 항목 만 표시합니다.

  LayoutParams layoutMatchParent = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
      LayoutParams layoutWrapContent = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      LayoutParams layoutMatchParentWidth = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 

      ScrollView scrollView = new ScrollView(this); 

      LinearLayout linearLayout = new LinearLayout(this); 
      linearLayout.setOrientation(LinearLayout.VERTICAL); 

      scrollView.addView(linearLayout); 
      setContentView(scrollView, layoutMatchParent); 

      LinearLayout vgMul=new LinearLayout(this); 
      vgMul.setOrientation(LinearLayout.VERTICAL); 

      TextView tvMul=new TextView(this); 
      tvMul.setText("Some Label"); 
      tvMul.setId(1); 
      tvMul.setLayoutParams(layoutWrapContent); 
      vgMul.addView(tvMul); 

      EditText edtMul=new EditText(this); 
      edtMul.setId(2); 
      edtMul.setLayoutParams(layoutMatchParentWidth); 
      vgMul.addView(edtMul); 

      Button btnAddMul=new Button(this); 
      btnAddMul.setText("Add"); 
      btnAddMul.setLayoutParams(layoutMatchParentWidth);        
      vgMul.addView(btnAddMul); 

      ListView lvMul =new ListView(this); 
      lvMul.setId(3); 
      lvListItems =new ArrayList<String>(); 
      lvArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lvListItems); 
      lvMul.setAdapter(lvArrayAdapter); 
      lvMul.setScrollContainer(false); 
      vgMul.addView(lvMul); 

      btnAddMul.setOnClickListener(new View.OnClickListener() { 
       @Override public void onClick(View v) { 
        EditText edtMul=(EditText)findViewById(2); 
        lvListItems.add(edtMul.getText().toString()); 
        lvArrayAdapter.notifyDataSetChanged();} 
       }); 

      linearLayout.addView(vgMul); 

스크롤보기로 인한 것인가? 선형 레이아웃의 자식으로 컨트롤을 추가하고 있지만 붙어 있어요 ... 모든 도움을 주셔서 감사합니다 ... 감사 ...

+1

거의 확실합니다. ScrollView와 ListView는 공존하지 않습니다. http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing – NigelK

답변

1

목록보기는 이미 scrollview를 구현합니다.

좋아요, 그래서 당신은 그 활동의 일부가 listview를 가지고 있다고 가정하고, listview를 스크롤하고 전체 활동을 스크롤하고자합니다.

다른 상대 레이아웃 (또는 선형 레이아웃)에서 목록보기를 작성하면됩니다. 나를

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

     <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
       <RelativeLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" > 
     //Your text views and other stuff goes here 


       </RelativeLayout> 
     </ScrollView> 


    <RelativeLayout 
     android:id="@+id/SecondRelativeLayoutDetailedPage" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentBottom="true" 
     android:background="@color/holoActionBarColor"> 

      <ListView 
       android:id="@+id/myRewardsActiveList" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/myActiveRewardsHeadingLinLayout"> 
      </ListView>   

    </RelativeLayout> 
</RelativeLayout> 

이 지금은 더 의미가 있습니까 XML로 당신을 위해 예를 쓰자?

+0

감사합니다.하지만 scrollview를 제거한 후 전체 화면을 스크롤하는 방법은 무엇입니까? 보시다시피 저는 동적 인 화면을 생성하려고합니다. 화면이 커지면 화면을 스크롤 할 수 있어야합니까? –

+0

@ EmreÇınar, 방금 내 답을 업데이트했습니다. –

관련 문제