2014-10-03 66 views
3

我有一個ListView用於在我的應用程序中顯示一些問題。當我ListView顯示,在第一次使用前我把它的滾動,一切看起來正常,直到我向下滾動,在我ListView向上滾動,在列表中的一些觀點從我ListView是如下改變:ListView滾動後出錯

1. ListView before Scroll 

ListView before Scroll

2.ListView After Fisrt Scroll 

ListView After Fisrt Scroll

3. ListView After next Scroll 

ListView After next Scroll

我不知道這是爲什麼發生在我ListView,我已經搜查了許多解決方案,但似乎沒有真正幫助我,這是我的代碼來調用我的適配器:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // Inisialisasi layout utama 
    kuisioner = inflater.inflate(R.layout.new_quisioner, container, false); 
    //ListView 
    list_tt = (ListView)kuisioner.findViewById(R.id.list_tt); 
    //list 
    listQuisioner = new ArrayList<ModelListQues>(); 
    lisLabelQues = new ArrayList<ModelLabelQues>(); 
    me=((ModelESPAJ)getActivity().getApplication()); 
    helper=new DBSpaj(getActivity()); 
    onLoadList(); 
    Adapter_Quisioner adapter_Quisioner = new Adapter_Quisioner(getActivity(), listQuisioner, lisLabelQues); 
    if(list_tt.getCount()!=listQuisioner.size()){ 
    list_tt.setAdapter(adapter_Quisioner); 
    } 
    return kuisioner; 
    } 

此代碼爲集我的適配器:

public class Adapter_Quisioner extends BaseAdapter{ 
    Context context; 
    int count; 
    ArrayList <ModelListQues>listQuisioner; 
    ArrayList <ModelLabelQues>lisLabelQues; 

    public Adapter_Quisioner(Context context, ArrayList <ModelListQues>listQuisioner, ArrayList <ModelLabelQues>lisLabelQues){ 
     this.context = context; 
     this.listQuisioner = listQuisioner; 
     this.lisLabelQues = lisLabelQues; 
     this.count=listQuisioner.size(); 
    } 

    @Override 
    public int getCount() { 

     return count; 
    } 
    @Override 
    public int getViewTypeCount() { 
     return count; 
    } 

    @Override 
    public Object getItem(int position) { 

     return listQuisioner.get(position); 
    } 

    @Override 
    public long getItemId(int arg0) { 

     return 0; 
    } 

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

//  convertView = null; 
     View view= convertView; 
     Holder holder= null; 

     if (view==null){ 
      LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      view=inflater.inflate(R.layout.adapter_questformat, parent,false); 

      holder=new Holder(); 
      //linear layout 
      holder.layout_label_kesehatan = (LinearLayout)view.findViewById(R.id.layout_label_kesehatan); 
      holder.layout_label_kesehatan.setVisibility(View.GONE); 
      holder.layout_jawaban_kesehatan = (LinearLayout)view.findViewById(R.id.layout_jawaban_kesehatan); 
      holder.layout_jawaban_kesehatan.setVisibility(View.GONE); 
      holder.layout_label_pp = (LinearLayout)view.findViewById(R.id.layout_label_pp); 
      holder.layout_label_pp.setVisibility(View.GONE); 
      holder.lay_inputtext_qu = (LinearLayout)view.findViewById(R.id.lay_inputtext_qu); 
      holder.lay_inputtext_qu.setVisibility(View.GONE); 
      //imageview 
      holder.img_val_qu = (ImageView)view.findViewById(R.id.img_val_qu); 
      //textview 
      holder.judul_qu= (TextView)view.findViewById(R.id.judul_qu); 
      holder.judul_qu.setVisibility(View.GONE); 
      holder.text_val_qu= (TextView)view.findViewById(R.id.text_val_qu); 
      holder.text_val_qu.setVisibility(View.GONE); 
      holder.number_qu= (TextView)view.findViewById(R.id.number_qu); 
      holder.isi_qu= (TextView)view.findViewById(R.id.isi_qu); 

      holder.isi_qu_ya= (TextView)view.findViewById(R.id.isi_qu_ya); 
      holder.isi_qu_ya.setVisibility(View.GONE); 
      holder.initial_pp= (TextView)view.findViewById(R.id.initial_pp); 
      holder.initial_pp.setVisibility(View.GONE); 
      holder.initial_tt= (TextView)view.findViewById(R.id.initial_tt); 
      holder.initial_tt.setVisibility(View.GONE); 
      holder.initial_tt1= (TextView)view.findViewById(R.id.initial_tt1); 
      holder.initial_tt1.setVisibility(View.GONE); 
      holder.initial_tt2= (TextView)view.findViewById(R.id.initial_tt2); 
      holder.initial_tt2.setVisibility(View.GONE); 
      holder.initial_tt3= (TextView)view.findViewById(R.id.initial_tt3); 
      holder.initial_tt3.setVisibility(View.GONE); 
      holder.initial_input_qu_1= (TextView)view.findViewById(R.id.initial_input_qu_1); 
      holder.initial_input_qu_1.setVisibility(View.GONE); 
      holder.initial_input_qu_2= (TextView)view.findViewById(R.id.initial_input_qu_2); 
      holder.initial_input_qu_2.setVisibility(View.GONE); 
      //edittext 
      holder.essay_qu= (EditText)view.findViewById(R.id.essay_qu); 
      holder.essay_qu.setVisibility(View.GONE); 
      holder.essay_qu_ya= (EditText)view.findViewById(R.id.essay_qu_ya); 
      holder.essay_qu_ya.setVisibility(View.GONE); 
      holder.input_qu_1= (EditText)view.findViewById(R.id.input_qu_1); 
      holder.input_qu_1.setVisibility(View.GONE); 
      holder.input_qu_2= (EditText)view.findViewById(R.id.input_qu_2); 
      holder.input_qu_2.setVisibility(View.GONE); 
      //radiogroup 
      holder.option_grup_qu= (RadioGroup)view.findViewById(R.id.option_grup_qu); 
      holder.option_grup_qu.setVisibility(View.GONE); 
      holder.option_grup_qu_pp= (RadioGroup)view.findViewById(R.id.option_grup_qu_pp); 
      holder.option_grup_qu_pp.setVisibility(View.GONE); 
      holder.option_grup_qu_tt= (RadioGroup)view.findViewById(R.id.option_grup_qu_tt); 
      holder.option_grup_qu_tt.setVisibility(View.GONE); 
      holder.option_grup_qu_tt1= (RadioGroup)view.findViewById(R.id.option_grup_qu_tt1); 
      holder.option_grup_qu_tt1.setVisibility(View.GONE); 
      holder.option_grup_qu_tt2= (RadioGroup)view.findViewById(R.id.option_grup_qu_tt2); 
      holder.option_grup_qu_tt2.setVisibility(View.GONE); 
      holder.option_grup_qu_tt3= (RadioGroup)view.findViewById(R.id.option_grup_qu_tt3); 
      holder.option_grup_qu_tt3.setVisibility(View.GONE); 
      //RadionButton 
      holder.option_label_y= (RadioButton)view.findViewById(R.id.option_label_y); 
      holder.option_label_y.setVisibility(View.GONE); 
      holder.option_label_n= (RadioButton)view.findViewById(R.id.option_label_n); 
      holder.option_label_n.setVisibility(View.GONE); 
      holder.option_label_y_pp= (RadioButton)view.findViewById(R.id.option_label_y_pp); 
      holder.option_label_y_pp.setVisibility(View.GONE); 
      holder.option_label_n_pp= (RadioButton)view.findViewById(R.id.option_label_n_pp); 
      holder.option_label_n_pp.setVisibility(View.GONE); 
      holder.option_label_y_tt= (RadioButton)view.findViewById(R.id.option_label_y_tt); 
      holder.option_label_y_tt.setVisibility(View.GONE); 
      holder.option_label_n_tt= (RadioButton)view.findViewById(R.id.option_label_n_tt); 
      holder.option_label_n_tt.setVisibility(View.GONE); 
      holder.option_label_y_tt1= (RadioButton)view.findViewById(R.id.option_label_y_tt1); 
      holder.option_label_y_tt1.setVisibility(View.GONE); 
      holder.option_label_n_tt1= (RadioButton)view.findViewById(R.id.option_label_n_tt1); 
      holder.option_label_n_tt1.setVisibility(View.GONE); 
      holder.option_label_y_tt2= (RadioButton)view.findViewById(R.id.option_label_y_tt2); 
      holder.option_label_y_tt2.setVisibility(View.GONE); 
      holder.option_label_n_tt2= (RadioButton)view.findViewById(R.id.option_label_n_tt2); 
      holder.option_label_n_tt2.setVisibility(View.GONE); 
      holder.option_label_y_tt3= (RadioButton)view.findViewById(R.id.option_label_y_tt3); 
      holder.option_label_y_tt3.setVisibility(View.GONE); 
      holder.option_label_n_tt3= (RadioButton)view.findViewById(R.id.option_label_n_tt3); 
      holder.option_label_n_tt3.setVisibility(View.GONE); 
      view.setTag(holder); 
     } 
     else { 
      holder=(Holder)view.getTag(); 
     } 
     //set judul Quisioner 
     String judul = ""; 
     String Question = listQuisioner.get(position).getQUESTION(); 

     holder.number_qu.setText(listQuisioner.get(position).getQUESTION_NUMBER()); 
     //Pemegang Polis 
     if(listQuisioner.get(position).getQUESTION_TYPE_ID()==1){ 
      holder.layout_label_pp.setVisibility(View.VISIBLE); 
      holder.lay_inputtext_qu.setVisibility(View.VISIBLE); 
      judul = "  PEMEGANG POLIS"; 
     } 
     //Tertanggung 
     if(listQuisioner.get(position).getQUESTION_TYPE_ID()==2){ 
      holder.layout_label_pp.setVisibility(View.VISIBLE); 
      holder.lay_inputtext_qu.setVisibility(View.VISIBLE); 
      judul = "  TERTANGGUNG"; 
     } 
     //Kesehatan 
     if(listQuisioner.get(position).getQUESTION_TYPE_ID()==3){ 
      holder.layout_label_kesehatan.setVisibility(View.VISIBLE); 
      holder.layout_jawaban_kesehatan.setVisibility(View.VISIBLE); 
      judul = "  DATA KESEHATAN"; 
     } 
     //check untuk judul 
     if (position == 0){ 
      holder.judul_qu.setVisibility(view.VISIBLE); 
      holder.judul_qu.setText(judul); 
     }else{ 
      if (lisLabelQues.get(position).getQUESTION_TYPE_ID() == listQuisioner.get(position-1).getQUESTION_TYPE_ID()){ 
       holder.judul_qu.setVisibility(view.GONE); 
      }else { 
       holder.judul_qu.setVisibility(view.VISIBLE); 
       holder.judul_qu.setText(judul); 
      } 
     } 

     //looping for labels 

      for(int i = 0; i<lisLabelQues.size(); i++){ 
       if(lisLabelQues.get(i).getQUESTION_TYPE_ID() == listQuisioner.get(position).getQUESTION_TYPE_ID()){ 
        if(lisLabelQues.get(i).getQUESTION_ID() == listQuisioner.get(position).getQUESTION_ID()){ 
         switch (lisLabelQues.get(i).getOPTION_TYPE()) { 
         case 0: 
          if(lisLabelQues.get(i).getOPTION_ORDER()==1){ 
          holder.essay_qu.setVisibility(View.VISIBLE); 
          if(listQuisioner.get(position).getQUESTION().contains("\nJika Tidak, jelaskan ...\nJika Ya")){ 
           Question = Question.substring(0,Question.lastIndexOf("\nJika Ya")); 
          } 
          holder.isi_qu.setText(Question); 
          } 
          if(lisLabelQues.get(i).getOPTION_ORDER()==2){ 
          holder.isi_qu_ya.setVisibility(View.VISIBLE); 
          Question = listQuisioner.get(position).getQUESTION(); 
          if(listQuisioner.get(position).getQUESTION().contains("\nJika Tidak, jelaskan ...\nJika Ya")){ 
          Question = Question.substring(Question.lastIndexOf("Jika Ya")); 
          holder.isi_qu_ya.setVisibility(View.VISIBLE); 
          } 
          holder.isi_qu_ya.setText(Question); 
          } 
         break; 

         case 1: 
          if(listQuisioner.get(position).getQUESTION_TYPE_ID()==3){ 

           if(lisLabelQues.get(i).getOPTION_GROUP()==1){ 
            holder.option_grup_qu_pp.setVisibility(view.VISIBLE); 
            holder.initial_pp.setVisibility(View.VISIBLE); 
            if(lisLabelQues.get(i).getOPTION_ORDER()==1){ 
              holder.option_label_y_pp.setVisibility(View.VISIBLE); 
              holder.option_label_y_pp.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
             }else{ 
              holder.option_label_n_pp.setVisibility(View.VISIBLE); 
              holder.option_label_n_pp.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
             } 
            } 

           if(lisLabelQues.get(i).getOPTION_GROUP()==2){ 
            holder.option_grup_qu_tt.setVisibility(view.VISIBLE); 
            holder.initial_tt.setVisibility(View.VISIBLE); 
            if(lisLabelQues.get(i).getOPTION_ORDER()==1){ 
              holder.option_label_y_tt.setVisibility(View.VISIBLE); 
              holder.option_label_y_tt.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
             }else{ 
              holder.option_label_n_tt.setVisibility(View.VISIBLE); 
              holder.option_label_n_tt.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
             } 
            } 

           if(lisLabelQues.get(i).getOPTION_GROUP()==3){ 
            holder.option_grup_qu_tt1.setVisibility(view.VISIBLE); 
            holder.initial_tt1.setVisibility(View.VISIBLE); 
            if(lisLabelQues.get(i).getOPTION_ORDER()==1){ 
              holder.option_label_y_tt1.setVisibility(View.VISIBLE); 
              holder.option_label_y_tt1.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
             }else{ 
              holder.option_label_n_tt1.setVisibility(View.VISIBLE); 
              holder.option_label_n_tt1.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
             } 
            } 

           if(lisLabelQues.get(i).getOPTION_GROUP()==4){ 
            holder.option_grup_qu_tt2.setVisibility(view.VISIBLE); 
            holder.initial_tt2.setVisibility(View.VISIBLE); 
            if(lisLabelQues.get(i).getOPTION_ORDER()==1){ 
              holder.option_label_y_tt2.setVisibility(View.VISIBLE); 
              holder.option_label_y_tt2.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
             }else{ 
              holder.option_label_n_tt2.setVisibility(View.VISIBLE); 
              holder.option_label_n_tt2.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
             } 
            } 

           if(lisLabelQues.get(i).getOPTION_GROUP()==5){ 
            holder.option_grup_qu_tt3.setVisibility(view.VISIBLE); 
            holder.initial_tt3.setVisibility(View.VISIBLE); 
            if(lisLabelQues.get(i).getOPTION_ORDER()==1){ 
              holder.option_label_y_tt3.setVisibility(View.VISIBLE); 
              holder.option_label_y_tt3.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
             }else{ 
              holder.option_label_n_tt3.setVisibility(View.VISIBLE); 
              holder.option_label_n_tt3.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
             } 
            } 

           }else{ 
            holder.option_grup_qu.setVisibility(View.VISIBLE); 
            if(lisLabelQues.get(i).getOPTION_ORDER()==1){ 
             holder.option_label_y.setVisibility(View.VISIBLE); 
             holder.option_label_y.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
            }else{ 
             holder.option_label_n.setVisibility(View.VISIBLE); 
             holder.option_label_n.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
            } 
          } 
         break; 

         case 2: 
         break; 

         case 3: 
         break; 

         case 4: 
          if(lisLabelQues.get(i).getOPTION_ORDER()==1){ 
           holder.input_qu_1.setVisibility(View.VISIBLE); 
           holder.initial_input_qu_1.setVisibility(View.VISIBLE); 
           holder.initial_input_qu_1.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
          }else{ 
           holder.input_qu_2.setVisibility(View.VISIBLE); 
           holder.initial_input_qu_2.setVisibility(View.VISIBLE); 
           holder.initial_input_qu_2.setText(lisLabelQues.get(i).getOPTION_LABEL()); 
          } 
         break; 

         case 5: 
         break; 
         } 

        } 
       } 
      } 

     return view; 
    } 

    private class Holder { 
     public LinearLayout layout_label_kesehatan, layout_jawaban_kesehatan, layout_label_pp, lay_inputtext_qu; 
     public ImageView img_val_qu; 
     public TextView text_val_qu, number_qu, isi_qu, initial_pp, initial_tt, initial_tt1, initial_tt2, initial_tt3, initial_input_qu_1, 
     initial_input_qu_2,isi_qu_ya,judul_qu; 
     public EditText essay_qu, input_qu_1, input_qu_2,essay_qu_ya; 
     public RadioGroup option_grup_qu, option_grup_qu_pp, option_grup_qu_tt, option_grup_qu_tt1, option_grup_qu_tt2, option_grup_qu_tt3; 
     public RadioButton option_label_y, option_label_n, option_label_y_pp, option_label_n_pp, option_label_y_tt, option_label_n_tt, 
     option_label_y_tt1, option_label_n_tt1, option_label_y_tt2, option_label_n_tt2, option_label_y_tt3, option_label_n_tt3; 

    } 

} 

那麼我的代碼有什麼問題嗎?我希望有人能幫助我。謝謝。

+0

其中之一,你沒有正確使用ViewHolder模式。你應該在ViewHolder中執行findViewById(),而不是在適配器的getView()方法中執行。除此之外,您只是因視圖回收問題而苦惱......請參閱下面的答案。 – 2014-10-03 02:49:58

回答

0
if(view == null){ 

## don't make any view GONE here 

}else{ 

} 
## Play with visibility or GONE for every view here.This will solve your problem 

holder.option_label_y_tt3.setVisibility(View.GONE); 
holder.option_label_y_tt2.setVisibility(View.GONE); 
holder.option_label_y_tt1.setVisibility(View.GONE); and more... 

Then rest of your code 
+0

感謝您的幫助^^ – Menma 2014-10-03 03:34:24