I'm developing an application that uses a check list to inform the state from a vehicle.
I'm using a Custom Adater for the ListView with a TextView and two Checkboxes.
The checkboxes are used to indicate that the characteristic meets or not an revision.
Actually the list has seven items but in the phone are displayed only six, because of the layout size, the problem is not here. the problem is the reuse of the View in the ListView, when check the first or second position, the sixth and seventh get checked too when i scroll to them, but the text of the TextView is show properly.
I have read these questions but I don't know to do now:
Duplicated entries in ListView
TextView in listview rows showing repeated values on scroll in Android?
List items position repeating in getview
Android Listview row repeating item <--- this question show my problem
The code:
ListAdapter:
public class ListAdapter extends ArrayAdapter<String> {
Context context;
int layoutResourceId;
String data[] = null;
LayoutInflater inflater;
public ListAdapter(Context context, int layoutResourceId,
String[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
MatrixHolder holder;
inflater = ((Activity) context).getLayoutInflater();
if (row == null) {
holder = new MatrixHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.headingitem);
holder.position = position;
final MatrixHolder hold = holder;
final int pos = hold.position;
holder.layout = (LinearLayout) row.findViewById(R.id.linearmain);
row.setTag(holder);
} else {
holder = (MatrixHolder) row.getTag(position);
}
holder.txtTitle.setText(data[position]);
holder.txtTitle.setTextSize(25);
return row;
}
static class MatrixHolder {
TextView txtTitle;
LinearLayout layout;
int position;
}
}
Adding the adapter:
ListAdapter listAdapter = new ListAdapter(getContext(),R.layout.horizontal_checkbox, itemList);
final ListView listView2 = (ListView)view.findViewById(R.id.listView);
listView2.setAdapter(listAdapter);
List Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/headingitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:padding="5dp"
android:textSize="20dp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearmain"
android:layout_gravity="center_horizontal|center_vertical"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cumple"
android:paddingLeft="2sp"
android:paddingRight="2sp"
android:layout_weight="1"
android:id="@+id/cumple"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Cumple"
android:paddingLeft="2sp"
android:paddingRight="2sp"
android:layout_weight="1"
android:id="@+id/noCumple"
/>
</LinearLayout>
The screenchots
And the final question. How to add a listener for those checkboxes and prevent to check the two checkboxes on a row
Aucun commentaire:
Enregistrer un commentaire