I have android program but my list view doesn't work correctly. if list view has more item with scrolling when i am going to click last checkbox with scrolling its check another item. please find my code sample.please help me to resolve this.
This my code for getView for adapter class
@SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.unsettled_invoice_layout, null);
holder = new ViewHolder();
holder.desc = (TextView) vi.findViewById(R.id.usinvoicedesc);
holder.chkbox = (CheckBox) vi.findViewById(R.id.selected);
holder.chkbox.setOnClickListener(new OnItemClickListener(position));
vi.setTag(holder);
vi.setTag(R.id.selected, holder.chkbox);
} else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0){
holder.desc.setText("No Data");
}else{
tempValues=null;
tempValues = (Invoice)data.get(position);
if(mode == 1){
int myColor = Color.rgb(0, 128, 0);
if(tempValues.getStatus() == 2){
myColor = Color.rgb(0, 128, 0); // Green
holder.chkbox.setChecked(false);
}else if(tempValues.getStatus() == 3){
myColor = Color.rgb(128, 0, 0); // Red
holder.chkbox.setChecked(true);
}
holder.desc.setBackgroundColor(myColor);
}
holder.desc.setText(tempValues.toString());
vi.setOnClickListener(new OnItemClickListener( position));
}
return vi;
}
This is layout.xml which i used for android
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingTop="0dip"
android:textColor="#000000" >
<LinearLayout
android:id="@+id/usinvoicelayout"
android:layout_width="fill_parent"
android:layout_height="95dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="2dip"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="horizontal"
android:paddingTop="0dip" >
<TextView
android:id="@+id/usinvoicedesc"
android:layout_width="520dp"
android:layout_height="90dp"
android:layout_gravity="start"
android:gravity="center_vertical"
android:textColor="#FFFF00"
android:textSize="18sp" />
<CheckBox
android:id="@+id/selected"
android:layout_width="50dp"
android:layout_height="80dp"
android:layout_marginStart="15dip"
android:button="@drawable/checkbox_background"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
</LinearLayout>
For this you need to maintain a hashmap. When you clicked on checkbox put position in hashmap and in getview before set checkbox checked check position in hashmap. if exist then checked checkbox.
Please check below link: It is specific solution for your issue of another item checkbox checked on scroll:
http://lalit3686.blogspot.in/2012/06/today-i-am-going-to-show-how-to-deal.html
In above Link example in your listview Adapter class getView() method add below code:
viewHolder.checkbox.setTag(position); // This line is important.
and for checkbox click add below code:
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.