Programing

사용자 정의 listview 어댑터 getView 메소드가 여러 번 호출되며 일관된 순서로 호출되지 않습니다.

crosscheck 2020. 6. 3. 21:18
반응형

사용자 정의 listview 어댑터 getView 메소드가 여러 번 호출되며 일관된 순서로 호출되지 않습니다.


사용자 정의 목록 어댑터가 있습니다.

class ResultsListAdapter extends ArrayAdapter<RecordItem> {

재정의 된 'getView'메서드에서 위치를 확인하고 위치가 convertView인지 여부를 확인하기 위해 인쇄합니다.

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        System.out.println("getView " + position + " " + convertView);

이것의 출력 (목록이 처음 표시 될 때 아직 사용자 입력이 없음)

04-11 16:24:05.860: INFO/System.out(681): getView 0 null  
04-11 16:24:29.020: INFO/System.out(681): getView 1 android.widget.RelativeLayout@43d415d8  
04-11 16:25:48.070: INFO/System.out(681): getView 2 android.widget.RelativeLayout@43d415d8  
04-11 16:25:49.110: INFO/System.out(681): getView 3 android.widget.RelativeLayout@43d415d8  
04-11 16:25:49.710: INFO/System.out(681): getView 0 android.widget.RelativeLayout@43d415d8  
04-11 16:25:50.251: INFO/System.out(681): getView 1 null  
04-11 16:26:01.300: INFO/System.out(681): getView 2 null  
04-11 16:26:02.020: INFO/System.out(681): getView 3 null  
04-11 16:28:28.091: INFO/System.out(681): getView 0 null  
04-11 16:37:46.180: INFO/System.out(681): getView 1 android.widget.RelativeLayout@43cff8f0  
04-11 16:37:47.091: INFO/System.out(681): getView 2 android.widget.RelativeLayout@43cff8f0  
04-11 16:37:47.730: INFO/System.out(681): getView 3 android.widget.RelativeLayout@43cff8f0  

AFAIK, 명시 적으로 찾을 수는 없지만 getView ()는 보이는 행에 대해서만 호출됩니다. 내 앱은 4 개의 보이는 행으로 시작하기 때문에 적어도 0-3에서 순환하는 위치 번호가 의미가 있습니다. 그러나 나머지는 엉망입니다.

  • 왜 각 행마다 getview가 세 번 호출됩니까?
  • 아직 스크롤하지 않았을 때 이러한 convertView는 어디에서 오는가?

나는 약간의 재교육을 받았으며 좋은 대답을 얻지 못한 채 사람들 이이 문제를 레이아웃 문제와 관련시키고 있음을 알았습니다. 따라서 목록이 포함 된 레이아웃은 다음과 같습니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" 
    android:orientation="vertical" >

    <TextView android:id="@+id/pageDetails"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" />

    <ListView android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:drawSelectorOnTop="false" />

</LinearLayout>

각 개별 행의 레이아웃 :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="108dp"    
android:padding="4dp">

<ImageView
    android:id="@+id/thumb"        
    android:layout_width="120dp"
    android:layout_height="fill_parent"        
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="8dp"        
    android:src="@drawable/loading" />

<TextView  
    android:id="@+id/price"
    android:layout_width="wrap_content"
    android:layout_height="18dp"         
    android:layout_toRightOf="@id/thumb"
    android:layout_alignParentBottom="true"       
    android:singleLine="true" />

<TextView  
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="18dp"         
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true" 
    android:paddingRight="4dp"       
    android:singleLine="true" />

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="17dp" 
    android:layout_toRightOf="@id/thumb"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:paddingRight="4dp"   
    android:layout_alignWithParentIfMissing="true"
    android:gravity="center" />

</RelativeLayout>

시간 내 주셔서 감사합니다


이것은 문제가 아니며, getView()몇 번이나 호출 될지 에 대한 보장은 없습니다 . 당신의 특별한 경우에 당신은 ListView그것을 제공함으로써 가능한 최악의 일 을하고 있습니다 height=wrap_content. 이렇게 ListView하면 레이아웃 타임에 어댑터에서 몇 개의 자식을 측정하여 크기가 얼마나 큰지 알 수 있습니다. 이것은 스크롤하기 전에 전달 된 것을 제공 ListView하는 convertViewsgetView()입니다.


목록보기 match_parentlayout_height속성을 사용해보십시오 . 그것은 방지 할 수 getView()너무 자주 호출 할 수 있습니다.


layout_width와 layout_height를 match_parent로 변경했을 때이 문제를 제거했습니다 (layout_height 만 변경해도 도움이되지 않음).


Helpful note watch out if you have nested items. You've got to change the "highest" one to match_parent. Hope it helps someone.


I am not able to answer your "Why" question but i definitely have a solution to the problem of the irritating "ListView items repeating" problem(if you have items in ur collection which are more than the screen height).

As many people above have mentioned, keep the android:layout_height property of the ListVew tag as fill_parent.

And about the getView() function, the solution is to use a static class called ViewHolder. Check out this example. It successfully does the task of adding all the items in ur Array or ArrayCollection.

Hope this helps friends!!

Best Regards, Siddhant


Ques: Why Adapter calls getView() manytimes? Ans: As Listview renders on scrolling is refreshes it's view with next upcoming views, for which adapter needs to get views by calling getView().

Ques: Why it's calls lesser if listview width and height set to fill_parent? Ans: Because as inflator has the fixed size for the screen area for list it calculates once for rendering the views onto the screen.

Hope it will resolve your query.


I was having the same problem with the dropdown in an AutoCompleteTextView. I was fighting with the problem for two days until I arrive here and you show me the solution.

If I write dropDownHeight="match_parent" the problem is fixed. Now the problem is related to UI (when you have one item the dropdown is too large) but the problem of multiple calls (much more important) is fixed.

Thank you!!


"Why is getview called for each row three times?" Because getView is called when you scroll on listview and to say better then that it is called when the position of an view of your list is changed!


I am having the same issue. If I have height set to fill_parent, then I get "usually" 2 calls per row. But, if I set height of my ListView to exact value, let's say to 300dp, then I get exactly one GetView call per row.

So, it seems to me that the only way is to first determine height of the screen, then programmatically set height of listvilew to that value. I don't like it. I hope there is a better way.


For all of you who still (After setting the height of the ListView to match_parent) are stuck (like I was):

You also have to set the height of the parent layout to match_parent.

See example below. The LinearLayout is the parent here:

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/something" />

        <ListView
            android:id="@+id/friendsList"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

This maybe coming late but if you are using layout_weight remember to always set layout_width="0dp"


i made this solution, maybe is not the best one, but does the work...

//initialize control string
private String control_input = " ";

then =

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View gridview = convertView;

    // change input_array for the array you use
    if (!control_input.equals(input_array[position])) {
        control_input = input_array[position];

        //do your magic

    } 

    return gridview;
}

hope it helps!

참고URL : https://stackoverflow.com/questions/2618272/custom-listview-adapter-getview-method-being-called-multiple-times-and-in-no-co

반응형