2012-01-11 2 views
0

내가 안드로이드에 어댑터를 만들어 다시 초기화 : 스피너 다시 초기화 그래서 목록 또는 EditView에서 액세스에서의 getView가 다시 호출 스크롤 할 때 때때로는 목록보기의 안드로이드 스피너 때로는

public class PointVerificationAdapter extends BaseAdapter { 
    List<PointVerification> mObjects; 
    Context mContext; 
    LayoutInflater mInflater; 
    Dao<ChoixPointVerification, Integer> mChoixPointVerificationDao; 
    HashMap<Integer, ReponsePointVerification> mReponses; 

public PointVerificationAdapter(Context context, 
     Dao<ChoixPointVerification, Integer> choixPointVerificationDao, 
     List<PointVerification> ListePointsVerification, 
     HashMap<Integer, ReponsePointVerification> listeReponsesPointsVerification) { 
    mInflater = LayoutInflater.from(context); 
    this.mContext = context; 
    this.mObjects = ListePointsVerification; 
    this.mChoixPointVerificationDao = choixPointVerificationDao; 
    this.mReponses = listeReponsesPointsVerification; 
} 

@Override 
public int getCount() { 
    return mObjects.size(); 
} 

@Override 
public PointVerification getItem(int position) { 
    return mObjects.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

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

    if (null == convertView) { 
     row = mInflater.inflate(R.layout.intervention_reponses_list, null); 
    } else { 
     row = convertView; 
    } 

    // affichage du nom du point de vérification 
    TextView tv = (TextView) row.findViewById(R.id.tvNom); 
    tv.setText(getItem(position).nom); 

    Integer idChoixPointVerification = null; 
    Integer idPointVerification = getItem(position).id; 
    if (mReponses != null && mReponses.containsKey(idPointVerification)) { 
     // affichage du commentaire 
     if (mReponses.get(idPointVerification).commentaire != null) 
     { 
      EditText edCommentaire = (EditText) row.findViewById(R.id.edCommentaire); 
      edCommentaire.setText(mReponses.get(idPointVerification).commentaire); 
     } 

     idChoixPointVerification = mReponses.get(idPointVerification).idChoixPointVerification; 
    } 



    // affichage de la liste déroulante 
    Spinner spi = (Spinner) row.findViewById(R.id.spiListeChoix); 
    ChoixPointVerificationDal choixPointVerificationDal = new ChoixPointVerificationDal(); 
    List<ChoixPointVerification> listeChoixPointVerification; 
    try { 
     listeChoixPointVerification = choixPointVerificationDal 
       .GetByIdPointVerification(mChoixPointVerificationDao, 
         getItem(position).id); 

     List<String> pointVerifications = new ArrayList<String>(); 
     for(ChoixPointVerification choixPointVerification: listeChoixPointVerification) 
     { 
      pointVerifications.add(choixPointVerification.nom); 
     } 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_dropdown_item, 
       pointVerifications); 

     spi.setAdapter(adapter); 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

    return row; 
} 

및 나는 사용자 선택을 잃는다. 이것에 대한 해결책이 있습니까?

편집

아니라 내 느낌의 getView 정말 매우 자주 전화해서 내가 그 기능에 간다 스피너 매번 REINIT하지 말아야한다는 것입니다. 그러나 이것이이 코드의 첫 번째 실행인지 어떻게 알 수 있습니까? 나는 ID에게 위치가이

 spi.setOnItemSelectedListener(new OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 
       varPosition = position; 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parentView) { 
       // your code here 
      } 

     }); 

같이가 someting로 선택하지만 난 그것이 좋은 방법이라고 생각하지 않습니다 가게 ... 내가 원하는 경우 :(

편집 2

했다 OnItemClickListener와 스피너를 만들 수있는 기술이 순간 내가 무엇 그 :.?

public class PointVerificationAdapter extends BaseAdapter { 
List<PointVerification> mObjects; 
Context mContext; 
LayoutInflater mInflater; 
Dao<ChoixPointVerification, Integer> mChoixPointVerificationDao; 
HashMap<Integer, ReponsePointVerification> mReponses; 
Integer mPositionSelectionne; 

public PointVerificationAdapter(
     Context context, 
     Dao<ChoixPointVerification, Integer> choixPointVerificationDao, 
     List<PointVerification> ListePointsVerification, 
     HashMap<Integer, ReponsePointVerification> listeReponsesPointsVerification) { 
    mInflater = LayoutInflater.from(context); 
    this.mContext = context; 
    this.mObjects = ListePointsVerification; 
    this.mChoixPointVerificationDao = choixPointVerificationDao; 
    this.mReponses = listeReponsesPointsVerification; 
} 

@Override 
public int getCount() { 
    return mObjects.size(); 
} 

@Override 
public PointVerification getItem(int position) { 
    return mObjects.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

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

    if (null == convertView) { 
     row = mInflater.inflate(R.layout.intervention_reponses_list, null); 
    } else { 
     row = convertView; 
    } 

    // affichage du nom du point de vérification 
    TextView tv = (TextView) row.findViewById(R.id.tvNom); 
    tv.setText(getItem(position).nom); 

    Integer idPointVerification = getItem(position).id; 
    if (mReponses != null && mReponses.containsKey(idPointVerification)) { 
     // affichage du commentaire 
     if (mReponses.get(idPointVerification).commentaire != null) { 
      EditText edCommentaire = (EditText) row 
        .findViewById(R.id.edCommentaire); 
      edCommentaire 
        .setText(mReponses.get(idPointVerification).commentaire); 
     } 

    } 

    // affichage de la liste déroulante 
    Spinner spi = (Spinner) row.findViewById(R.id.spiListeChoix); 
    spi.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      View row = mInflater.inflate(R.layout.intervention_reponses_list, null); 

      ChoixPointVerificationDal choixPointVerificationDal = new ChoixPointVerificationDal(); 
      List<ChoixPointVerification> listeChoixPointVerification; 
      try { 
       listeChoixPointVerification = choixPointVerificationDal 
         .GetByIdPointVerification(mChoixPointVerificationDao, 
           getItem(position).id); 

       ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, 
         android.R.layout.simple_spinner_dropdown_item, 
         pointVerifications); 

       spi.setAdapter(adapter); 

      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 



    return row; 

} 

에드 그것은 3

목록보기

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

    <ListView 
     android:id="@+id/lstPointsVerification" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" > 
    </ListView> 

</LinearLayout> 

그리고 목록보기

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

    <TextView 
    android:id="@+id/tvNom" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
android:layout_weight="1" 
    style="@style/ListePrincipal" 
    /> 

    <Spinner 
     android:id="@+id/spiListeChoix" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 


    <EditText 
    android:id="@+id/edCommentaire" 
     android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    style="@style/ListePrincipal" 
    /> 
</LinearLayout> 

내 목표의 각 라인에 대한 목록보기가 포함 된 작업의 레이아웃은 verificate하는 점의 목록을 작성하기 위해 단지이다 사용자가 그는 약 20 줄의 검증을해야합니다. 각 행에는 레이블, 차이 선택 항목이있는 회 전자 및 주석 필드가 들어 있습니다. 그러면 각 줄마다 각 대답을 검색해야합니다.

+0

getView() 내부에 스피너를 넣으면 올바른 작업을 수행하고 있습니까? OnItemClick과 같은 것을 원하고 회 전자를 다시 만들 수 있습니까? –

+0

글쎄 아마 그걸하는 법을 배울 것입니다 ... tk you –

+0

다음은 getView()의 호출 수에 대한 다른 게시물의 답변입니다. AdapterView getView는 AdapterView에 뷰가 필요할 때만 호출됩니다 . getView가 호출되는 빈도 또는 횟수에 대한 가정을해서는 안됩니다. getView가 수행해야하는 유일한 작업은 필요한보기를 가능한 빨리 리턴하는 것입니다. 여기에서 가져온 http://stackoverflow.com/questions/3287563/doubts-with-base-adapter-class-and-its-functionality. ---- 왜 코드의 첫 번째 실행을 확인하고 싶습니까? 아마도 당신은 어댑터를 작성한 후에 itemclicks를 사용하여 결함에 대해 더 많은 통찰력을 제공해야합니다. –

답변

1

다음은 Listview에 대한 OnItemClick의 예입니다. 별도의 방법이 아닌 다른 곳에서 회 전자 생성 을 이동해야합니다. 오히려 별도의 방법에 - 그것은 여기

ListView mainListview = new ListView(this); 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_single_choice, new String[]{"Search","Options"}); 
      mainListview.setAdapter(adapter); /// your adapter here 
      mainListview.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
         long arg3) { 

     ///doYourSpinnerStuff(arg1) here 

        } 

          }); 

아래 다른 곳에 이동해야합니다 코드의이 모든 조각을 할 것 같은 그런 대해서 itemClick에 호출. 그런 다음 회 전자를 작성한 다음 클릭시 채 웁니다. 스케치를 표시 할 수 있습니까? 스크린 샷이나 레이아웃을 사용하여 활동에 뷰를 배치 할 수 있습니까?

// affichage de la liste déroulante 
    Spinner spi = (Spinner) row.findViewById(R.id.spiListeChoix); 
    ChoixPointVerificationDal choixPointVerificationDal = new ChoixPointVerificationDal(); 
    List<ChoixPointVerification> listeChoixPointVerification; 
    try { 
     listeChoixPointVerification = choixPointVerificationDal 
       .GetByIdPointVerification(mChoixPointVerificationDao, 
         getItem(position).id); 

     List<String> pointVerifications = new ArrayList<String>(); 
     for(ChoixPointVerification choixPointVerification: listeChoixPointVerification) 
     { 
      pointVerifications.add(choixPointVerification.nom); 
     } 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_dropdown_item, 
       pointVerifications); 

     spi.setAdapter(adapter); 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
+0

그래서 사용자가 listview에서 회 전자를 클릭하면 listview의 onitemclick이 호출됩니까? 그것은 다소 복잡하게 들린다. –

+0

Noo. 귀하의 PointVerificationAdapter는 목록보기 또는 다른 회 전자에 맞게 생성됩니까? 따라서 기본적으로이 목록보기 또는 회 전자 중 하나를 만들면 OnItemClick을 설정하고 해당 클릭을 기반으로 추가 회 전자를 만듭니다. 나는 네가 맞았 으면 좋겠어. –

+0

PointVerificationAdapter는 Spinner 용이다. ListView 다른 어댑터가 있습니다. 나는 조금 잃어 버렸습니다. 당신은 회 전자를 onitemclick으로 어떻게 만들 수 있습니까? 질문을 편집하여 내 작업 상태를 다시 채 웁니다. 도와 줘서 고마워. –

관련 문제