0
i have placed a textview and a toggle button into a list view by using the following xml file and i have used an arrayadapter to set it on to my layout, my problem is when i scroll down in the list view by unchecking a few toggle buttons (which are all set to be checked by default), they go back to default state and few others change state. As i want to fetch the state of the toggle button in my later stages its very important to stick on to the state that ive assigned to the toggle button. Please help to resolve this problem. Here are my xml file and the java code i use to set it on to the adapter. thank you in prior... 


xml file that is named listdecor is included here 


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/lvlistdecor" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#000000" 
     android:orientation="horizontal" 
     android:weightSum="2" > 

     <TextView 
      android:id="@+id/tvlistdecor" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1.5" 
      android:ellipsize="marquee" 
      android:singleLine="true" 
      android:text="sample" 
      android:textColor="#FFFFFF" 
      android:textSize="24sp" 
      android:typeface="sans" /> 

     <CheckBox 
      android:id="@+id/cbusn_checked" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="6sp" 
      android:layout_weight="0.5" 
      android:checked="true" 
      android:focusable="false" 
      android:padding="10dp" /> 

    </LinearLayout> 

Sir 나는 목록보기에 사용자 지정 어댑터를 포함하고 또한 토글 단추 대신 CheckBox로 변경했습니다. 하지만 이제 전체 목록보기는 내 에뮬레이터에서 볼 수 없습니다. 난 내 편집 된 자바 클래스와 XML 파일을 포함 시켰습니다, 나에게 변경을 제안 해주십시오, 감사합니다 ... 수정 된 자바 코드는 여기목록보기가 안드로이드에서 스크롤 될 때 토글 버튼 상태가 변경됩니까?

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); // Setting the window to full screen Step 1 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); // Setting the window to full screen Step 2 

    setContentView(R.layout.attendance5); 


    facid = (TextView) findViewById(R.id.tvfacid); 
    facname = (TextView) findViewById(R.id.tvfacname); 
    subjectchosen = (TextView) findViewById(R.id.tvsubject); 
// llattendlist = (LinearLayout)findViewById(R.id.llattendlist); 


    String Uid = getIntent().getStringExtra("Uid"); 
    facid.setText(Uid); 

    String Name = getIntent().getStringExtra("Name"); 
    facname.setText(Name); 


    String Sub = getIntent().getStringExtra("Sub"); 
    subjectchosen.setText(Sub); 



    Attendusn = getIntent().getExtras(); 
    if(Attendusn!=null) 
    { 
     AttendStud_usn = (ArrayList<String>)Attendusn.getStringArrayList("AttendStud_usn"); 
    } 
    String AttendLen = String.valueOf(AttendStud_usn.size()); 
    //int flag = 0 ; 
    //subjectchosen.setText(String.valueOf(AttendStud_usn)); 

    myListView = (ListView) findViewById(R.id.lvnames); 
    // myListView.setAdapter(new ArrayAdapter<String>(this, R.layout.listdecoration,R.id.tvlistdecor, AttendStud_usn)); 
    //ArrayAdapter<String> MyAdapter= new ArrayAdapter<String>(this,R.layout.listdecoration, R.id.tvlistdecor,AttendStud_usn); 
    //myListView.setAdapter(MyAdapter); 
    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View item, int position, 
       long id) { 
      // TODO Auto-generated method stub 
      Stud_usn usn= MyAdapter.getItem(position); 
      usn.toggleChecked(); 
      Stud_usnViewHolder vh_usn= (Stud_usnViewHolder) item.getTag(); 
      vh_usn.getCheckBox().setChecked(usn.isChecked()); 
     } 
    }); 

    MyAdapter = new Stud_Adapter(this,AttendStud_usn); 
    myListView.setAdapter(MyAdapter); 

} 
private static class Stud_usn{ 
    private String usn_no=""; 
    private boolean checked=true; 
    public Stud_usn(String usn_no){ 
     this.usn_no = usn_no; 
    } 
    public String getUsn(){ 
     return usn_no; 
    } 
    public void setChecked(boolean checked){ 
     this.checked=checked; 
    } 
    public Object isChecked() { 
     // TODO Auto-generated method stub 
     return checked; 
    } 
    public String toString(){ 
     return usn_no; 
    } 
    public void toggleChecked() { 
     // TODO Auto-generated method stub 
     checked = !checked; 
    } 
} 
private static class Stud_usnViewHolder{ 
    private CheckBox checkbox; 
    private TextView textview; 

    public Stud_usnViewHolder(TextView textview, CheckBox checkbox){ 
     this.checkbox=checkbox; 
     this.textview=textview; 
    } 
    public CheckBox getCheckBox() { 
     // TODO Auto-generated method stub 
     return checkbox; 

    } 
    public TextView getTextView(){ 
     return textview; 
    } 
} 

private static class Stud_Adapter extends ArrayAdapter<Stud_usn>{ 

    public LayoutInflater inflater; 
    public Stud_Adapter(Context context, List<String> attendStud_usn) { 
     super(context,R.layout.listdecoration,R.id.tvlistdecor); 
     // TODO Auto-generated constructor stub 
     inflater = LayoutInflater.from(context); 
    } 

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

     Stud_usn usn = (Stud_usn) this.getItem(position); 

     CheckBox checkbox; 
     TextView textview; 

     if(convertView == null){ 
      convertView = inflater.inflate(R.layout.listdecoration,null); 
      textview = (TextView) convertView.findViewById(R.id.tvlistdecor); 
      checkbox = (CheckBox) convertView.findViewById(R.id.cbusn_checked); 


     } 

     return convertView; 

    } 

} 

}

+0

뷰 객체는 재활용됩니다. 그 정보를 지저분하게 저장해야합니다. – RaphMclee

답변

0

개체가 재활용 된 뷰 포함되어 있습니다. 그 정보를 지저분하게 저장해야합니다. 보기에서가 아니라 어댑터에서 사용하는 데이터가 있습니다. 보기를 생성 할 때 버튼 상태를 다시 설정하려면이 정보를 사용하십시오. 사용자 지정 어댑터를 구현해야합니다. BaseAdapter 클래스를 사용하십시오.

+0

선생님 제발 어떻게 말해 드릴까요? 감사합니다 – Vishvas

+0

제발 이것에 관한 링크를 제공, ive 3 일 이후 동일을 찾고있다 – Vishvas

+0

@Vishvas google.ch (이 웹 사이트는 정말 사용 중입니다) – RaphMclee