2011-10-08 3 views
2

ListView의 모든 요소를 ​​검사하여 해당 레이블 중 하나만 레이블을 설정해야합니다. 데이터베이스 또는 어댑터를 편집 할 수 없습니다. ListView를 스크롤하여 TextView에 문자열을 설정하고 검사를 수행하기 만하면됩니다.getChildCount()는 ListView에서 0을 반환합니다.

@Override 
protected void onResume(){ 
    super.onResume(); 
    ... 
    cursor = getCursor(); 
    startManagingCursor(cursor); 
    adapter = new SimpleCursorAdapter(this,R.layout.profile_item_layout,cursor,from,to); 
    lst_profiles.setAdapter(adapter); 
    SharedPreferences customSharedPreference = PreferenceManager.getDefaultSharedPreferences(ProfilerActivity.this.getApplicationContext()); 
    long current = customSharedPreference.getLong(ProfileManager.CURRENT, -1); 
    toast(lst_profiles.getChildCount()); //display 0 
    if(current!=-1){ 
     for(int i=0;i<lst_profiles.getChildCount();i++){ 
      if(lst_profiles.getItemIdAtPosition(i)==current) 
       ((TextView)lst_profiles.getChildAt(i).findViewById(R.id.activeLabel)).setText(getString(R.string.active)); 
      else 
       ((TextView)lst_profiles.getChildAt(i).findViewById(R.id.activeLabel)).setText(""); 
     }  
    } 
} 

어떻게하면됩니까? 기다릴 필요가 있니?

P. 대개 ListView는 비어 있지 않습니다.

+0

당신이() 여기 getCursor를 호출하는 방법? 이것에 대한 Activity 또는 Context 메서드가 보이지 않습니다 ... – Maximus

+0

예 onResume() 내부에서 getCursor()를 호출합니다. 응용 프로그램이 잘 작동, 유일한 문제는 0 getChildCount()입니다 – JoP

답변

10

불쾌한 해킹 인 것 같습니다. 하지만 괜찮아 ...

것은 목록이 사용자에게 표시되지 않는 한 귀하의 목록에는 하위 항목이 없습니다. getChildCount은 보이는 목록 항목의 양을 반환하므로 (대략 10 개 정도) 해당 위치가 어댑터의 실제 항목 위치와 관련이 없음을 이해해야합니다. 당신이 정말 낮은 수준에서 전망과 통신해야하는 경우

당신은 당신의 목록에 스크롤 리스너를 연결을 시도 할 수 있습니다 :

@Override 
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
    for (int i = 0; i < visibleItemCount; i++) { 
     View child = getChildAt(i); 
     // i + firstVisibleItem == the actual item position in the adapter 
    } 
} 
+1

Hehe, [여기] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235# 5235) 당신은 어떻게, 왜 그리고 언제 대답을 받아야하는지에 대해 필요한 모든 정보를 찾는다. – Knickedi