2017-02-14 69 views
0

我與CheckBox取得ListView但同時滾動列表視圖更CheckBox是隨機選擇,它不持有自己的立場。請幫幫我。在此先感謝我與複選框列表視圖,但在滾動列表視圖更復選框是隨機選擇,它不持有自己的立場

public override View GetView(int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    if (view == null) { 
     view = context.LayoutInflater.Inflate(Resource.Layout.row1, parent, false); 
    } 
    Data item = this[position]; 
    view.FindViewById<TextView>(Resource.Id.title).Text = item.nameview; 
    view.FindViewById<TextView>(Resource.Id.reporter).Text = item.EmailIdview; 
    checkbox = view.FindViewById<CheckBox>(Resource.Id.checkbox);   
    checkbox.Tag = item.nameview; 
    checkbox.SetOnCheckedChangeListener(new CheckedChangeListener(context, list, position)); 
    return view; 
} 

public class CheckedChangeListener : Java.Lang.Object, CompoundButton.IOnCheckedChangeListener { 

    private Activity context; 
    private List<Data> list; 
    private int mPosition; 

    public CheckedChangeListener(Activity context, List<Data> list, int mPosition) { 
     this.context = context; 
     this.list = list; 
     this.mPosition = mPosition; 
    } 

    public void OnCheckedChanged(CompoundButton buttonView, bool isChecked) { 
     Group gr = new Group();     
     string name = buttonView.Tag.ToString(); 
     if (isChecked) { 
      gr.checkboxSelected(name, isChecked);//list.ElementAt(mPosition)      
     } else {     
      gr.setPosition(mPosition);      
     } 
    }   
} 
+0

採取holder類 – user3040153

回答

0

你可以使用模型類來保存複選框的選中狀態。我所看到的喲有一個名爲數據模型類

您可以在添加器isChecked數據模型類這樣的,

public class Data { 

    String name; 
    boolean isChecked; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 


    public boolean isChecked() { 
     return isChecked; 
    } 

    public void setChecked(boolean checked) { 
     isChecked = checked; 
    } 
} 

現在你有某種適配器類:使用視圖架模式

您可以設置複選框標籤。 我可以給你喲參考例子,這跟隨相同的你會得到想要的結果。

import android.app.Activity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.TextView; 

import java.util.List; 

public class DataAdapter extends BaseAdapter { 


    public Activity context; 

    public List<Data> list; 

    public DataAdapter(Activity context, List<Data> list) { 
     this.context = context; 
     this.list = list; 
    } 

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

    @Override 
    public Object getItem(int i) { 
     return list.get(i); 
    } 

    @Override 
    public long getItemId(int i) { 
     return list.size(); 
    } 

    @Override 
    public View getView(int position, View view, ViewGroup viewGroup) { 
     Holder holder; 
     if (view == null) { 
      LayoutInflater inflater = context.getLayoutInflater(); 
      view = inflater.inflate(R.layout.list_row, viewGroup, false); 
      holder = new Holder(); 
      holder.textView = (TextView) view.findViewById(R.id.textView); 
      holder.checkBox = (CheckBox) view.findViewById(R.id.checkbox); 

      view.setTag(holder); 
     } else { 
      holder = (Holder) view.getTag(); 
     } 
     holder.checkBox.setOnCheckedChangeListener(new CheckedChangeListener()); 
     holder.checkBox.setTag(position);//important line 
     holder.textView.setText(list.get(position).getName()); 
     holder.checkBox.setChecked(list.get(position).isChecked()); 
     return view; 
    } 

    private class CheckedChangeListener implements CompoundButton.OnCheckedChangeListener { 

     public CheckedChangeListener() { 
     } 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
      int position=(int) compoundButton.getTag();//important line 
      list.get(position).setChecked(b);//important line 
     } 
    } 

    public static class Holder { 
     TextView textView; 
     CheckBox checkBox; 
    } 
} 

快樂編碼;)

+0

謝謝,它的工作的罰款。 – priyanka

0

覆蓋適配器的內部這兩種方法:

@Override 
    public int getViewTypeCount() { 

     return getCount(); 
    } 

    @Override 
    public int getItemViewType(int position) { 

     return position; 
    } 

希望這將解決您的問題。

乾杯!

0

他們可以是否定的。這個問題的原因。

您的適配器不知道他們在你的名單如何的觀點很多類型的。爲此,您可以在適配器中包含這兩個功能。

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

@Override 
public int getItemViewType(int position) { 
    return position; 
} 

其他原因可能是要更新列表中的東西,但在數據庫中(模型)沒有更新它,你取來from.On滾動列表視圖將失去約從離滾動的項目的信息screen.It將從您提供給它的數據庫再次加載它們。因此,該數據庫應該有更新的信息。檢查此鏈接的細節。

對適配器中的所有if都沒有使用else:如果您在適配器中使用的條件總是給它一個其他值,以便適配器知道該怎麼做。這有時是導致適配器項目混亂的原因。

不使用viewholder模式:這也是一個衆所周知的原因,這一問題。您應該使用視圖模式,以確保適配器不會一次又一次地繪製佈局。

0

這是因爲ListView的項目視圖的重用機制。您可以像其中一個答案那樣更改Data的源代碼,也可以將List字段添加到適配器,以保存每個項目的檢查狀態並在顯示相關項目時進行恢復。