2012-05-17 2 views
0

ListView의 행을 배치하는 방법을 정의하는 RelativeLayout 핸들을 얻으려고합니다. 나는 널 (null)이되어 가고 있으며 그것이 두 가지 중 하나이기 때문에 그것이라고 믿습니다.ListView에서 RelativeLayout으로 트리를 이동하려고 시도

가능성이 높습니다. 아직 과장되지 않았으므로 null을 반환합니다.이 글을 읽은 동안 인플레이션 개념에 대해 매우 불안정합니다. 저는 Java에서 3 주 동안 프로그래밍을 해왔습니다 (C#에서). 그래서 테스트하는 방법을 잘 모르겠습니다. 예를 들어, 내가하는 일은 어댑터를 목록보기로 설정하는 것입니다. 어떤 이벤트가 발생했는지 정확히 인플레이션이 커버 아래에서 발생하는지 확실하지 않습니다.

두 가지 가능성은 내가 제대로 나무를 걷지 않는다는 것입니다.

내 부모 :

<RelativeLayout 
    android:minHeight="320dp" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/rlay_LftDataParent" 
    android:orientation="vertical" > 

<ListView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/lstvw_LiftData" /> 

행 정의 I 해봤 활동에

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:id="@+id/rlay_LftDataRw" 
    android:layout_height="fill_parent" 
    android:background="@drawable/background"> 
... 
</RelativeLayout> 

(I가 최소 높이를 설정하려고 레이아웃) : (lstvw_LiftData는 ListView 개체입니다)

RelativeLayout RowLayout = (RelativeLayout)lstvw_LiftData.findViewById(R.id.rlay_LftDataRw); 

RelativeLayout RowLayout = (RelativeLayout)findViewById(R.id.rlay_LftDataRw); 

당신은 더 큰 그림을 알고 호기심이 있다면 이것이 내가 화면을 채우기 위해 행 레이아웃을 얻으려고이 게시물 Using RelativeLayout to control a ListView row height에서 질문에 대한 후속입니다.

답변

1

findViewById으로 ListView 행의 View에 액세스하지 마십시오. 대신 어댑터를 사용하여 getView 메서드에서 RelativeLayout 참조를 가져오고 너비와 높이를 설정합니다.

편집 : 당신은 그래서 여기에 사용하는 기본 ArrayAdapter에 대한 간단한 예입니다 않는 것을 어댑터 말도하지 않았다이 때 당신은 getView() 방법을 직접 호출하지 않는 어댑터가 자동으로 getView 메소드를 호출 새 행을 표시 할 시간입니다. 당신이 Adapter 클래스 다른 파일에있는 경우에 해당 어댑터의 생성자을

public class SomeActivity extends ListActivity { 

    private int mHeight, mWidth; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // ...do what ever stuff you need in this activity 
     // find out the height/width values: 
     DisplayMetrics metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 

     mHeight = metrics.heightPixels; 
     mWidth = metrics.widthPixels; 
     // set the adapter 
     setListAdapter(new CustomAdapter(this, 
       R.layout.adapters_listview_rowheight, R.id.textView1, items)); 
    } 

    //you use your own adapter class 
    private class CustomAdapter extends ArrayAdapter<String> { 

     public CustomAdapter(Context context, int resource, 
       int textViewResourceId, String[] objects) { 
      super(context, resource, textViewResourceId, objects); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
        //do whatever logic you need here 
        // finally you end up with the RelativeLayout you'll return as the view for the row 
        // before you return set the desired values for its width and height 
      rl.getLayoutParams().height = mHeight; 
      rl.getLayoutParams().width = mWidth; 
      return rl; 
     } 


    } 

} 

: 사용자 정의 Adapter 클래스는 당신이 그것을 사용하는거야 Activity의 내부 클래스 인 경우, 마지막 코멘트에서 질문에 대해서 폭 및 높이를 나타내는 두 개의 값을 취 int :

//the constructor 
public CustomAdapter(/*whatever parameters you already have*/, int height, int width) { 
    //do stuff 
    //now you also have a reference for the width and height that you can use in the getView method. 
} 

및하여 네 Activity합니다 (방법에서 onCreate 동일한 코드)

setListAdapter(new CustomAdapter(/*whatever parameters you already have*/, mHeight, mWidth)); 
012,

이것이 원하는 것인지 잘 모르겠습니다.

+0

나는 전달할 params를 제외한 모든 것을 수행하고 있습니다.사용자 정의 어댑터를 사용하여 예제가 잘 작동합니다. getView를 대체하기 위해 전달할 항목을 모르겠습니다. 내 활동에서 RelativeLayout X = (RelativeLayout) LiftDataAdapter.getView (position, convertView, parent)를 호출합니다. 어느 것이 내 무시 무시할 것인가 나는 단지 매개 변수를 모른다는 것인가? 별도의 경우 maheight를 사용하는 것이 좋지만, mahight는 활성 범위에서 사용자 지정 어댑터 범위로 해당 param을 가져 오는 방법을 알지 못하는 경우를 제외하고는 괜찮습니다. getView를 호출하기 전에 전달할 메소드를 작성합니까? – GPGVM

+1

좋습니다. 지금 받으십시오. 내 질문에 대한 작업 범위에서 어댑터 범위에 값을 가져 오는 방법에 대한 남아 있으므로 getView 해당 채우는대로 내 시스템 호출됩니다. – GPGVM

+0

@ user1278561 수정 된 답변보기 – Luksprog

0

Adapter을 통해 행에 액세스합니다. getView과 비슷하지만 사용중인 Adapter에 따라 다를 수 있습니다.

관련 문제