vendredi 13 mars 2015

Adding a check box (only one can be checked at a time) to my custom list view

I have made my custom adapter and am adapting an array of "persons". In my list item xml i have put a checkbox, but this is where i get confused. I want my list to only be able to have one check box checked at a time. And i also want to implement an OnCheckBox click listener of some sort to preform an action each time a different check box is selected.


this is my custom adapter



public class PersonAdapter extends BaseAdapter {

Context mContext;
Person[] mPersonArray;

public PersonAdapter(Context context , Person[] persons) {
mContext = context;
mPersonArray = persons;
}

@Override
public int getCount() {
return mPersonArray.length;
}

@Override
public Object getItem(int position) {
return mPersonArray[position];
}

@Override
public long getItemId(int position) {
return 0; // we wont use
}

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

if (convertView == null) {
// brand new
convertView = LayoutInflater.from(mContext).inflate(R.layout.person_list_item , null);
holder = new ViewHolder();
holder.nameText = (TextView) convertView.findViewById(R.id.nameTextView);
holder.birthDateText = (TextView) convertView.findViewById(R.id.birthDateTextView);
holder.checkBoxList = (CheckBox) convertView.findViewById(R.id.checkBox);

convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}

Person personForList = mPersonArray[position];

holder.nameText.setText(personForList.getName());
holder.birthDateText.setText(personForList.getBirthDate());





return convertView;
}

public static class ViewHolder {
TextView nameText;
TextView birthDateText;
CheckBox checkBoxList;
}


Here is my list item xml



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:background="#ff53">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Evan Dix"
android:id="@+id/nameTextView"
android:textColor="#ff323232"
android:textSize="30sp"
android:layout_marginLeft="50dp"
android:layout_marginStart="86dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="11-12-12"
android:id="@+id/birthDateTextView"
android:textColor="#ff323232"

android:layout_marginStart="23dp"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/imageView"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@android:drawable/presence_invisible"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/nameTextView"
android:layout_marginStart="12dp"/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="29dp"
android:checked="false"
android:clickable="true"
android:choiceMode = "singleChoice"/>


And inside my class where i am populating the listview.... it extends ListAdapter



Person[] personArray = new Person[mPersonArrayList.size()];
personArray = mPersonArrayList.toArray(personArray);

PersonAdapter adapter = new PersonAdapter(this , personArray);
setListAdapter(adapter);




Aucun commentaire:

Enregistrer un commentaire