I have a custom spinner with an ImageView and a TextView, however the return codes do not return either one of these values rather they return it as an object.
How do I get the drawable id or the textview string separately and not the object of the spinner?
Do I have to do it in my adapter, the spinner itself or other components trying to get the data?
Tq
Here is my Spinner XML code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/spinnerImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="5dp"
android:layout_weight="1"
android:src="#drawable/dark_red_square" /><!--Make sure image is present in Drawable folder-->
<TextView
android:id="#+id/spinnerText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="2"
android:textSize="14sp"
android:gravity="center"
android:text="Demo"
android:textColor="#000" />
</LinearLayout>
This spinner is in a fragment. Honestly I don't know what codes should I sample here for reference, but here's the custom adapter
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
String[] countryNames;
LayoutInflater inflter;
public CustomAdapter(Context context, int[] flags, String[] countryNames, LayoutInflater inflter) {
this.context = context;
this.flags = flags;
this.countryNames = countryNames;
this.inflter = (LayoutInflater.from(context));
}
#Override
public int getCount() {
return flags.length;
}
#Override
public Object getItem(int position) {
return countryNames[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflter.inflate(R.layout.spinner_item, null);
TextView spinnerText= convertView.findViewById(R.id.spinnerText);
ImageView spinnerImage = convertView.findViewById(R.id.spinnerImage);
spinnerText.setText(countryNames[position]);
spinnerImage.setImageResource(flags[position]);
return convertView;
}
}
Related
So I have created a custom adapter for my ListView with three Views - TextView, ImageView and a basic View:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/AllNotesFragment">
<TextView
android:id="#+id/addNoteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="25dp"
android:textSize="18sp"
android:gravity="center"
android:fontFamily="#font/ukij_qolyazma"
android:text="+new"
/>
<ImageButton
android:id="#+id/deleteNoteImageButton"
android:layout_width="50dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="0dp"
android:layout_marginEnd="30dp"
android:textSize="24sp"
android:gravity="center"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:fontFamily="#font/ukij_qolyazma"
/>
<View
android:id="#+id/underlineView"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="70dp"
android:background="#color/colorMainDark" />
<ListView
android:id="#+id/notesListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="75dp"
android:layout_marginBottom="0dp">
</ListView>
I have created and instantiated the ListView
notesListView = view.findViewById(R.id.notesListView);
And filled it with a bunch of my notes
notes = (ArrayList<Note>) db.getAllNotesForDay(NotesForDayActivity.getRememberDay(),
NotesForDayActivity.getRememberMonth(),
NotesForDayActivity.getRememberYear());
for (Note note : notes) {
noteTitles.add(note.getTitle());
}
NotesListAdapter adapter = new NotesListAdapter(((NotesForDayActivity) getActivity()).getContext(), notes);
notesListView.setAdapter(adapter);
With my custom adapter:
public class NotesListAdapter extends BaseAdapter {
private static final String TAG = "NotesListAdapter";
public static Context context;
private RelativeLayout notesListRelativeLayout;
private TextView noteTitleTextView;
private ImageView tickImage;
private View underlineView;
private List<Note> notes;
// !
private View listItemsView;
public NotesListAdapter(Context context, List<Note> notes) {
this.notes = notes;
this.context = context;
}
#Override
public int getCount() {
return NotesForDayActivity.getCountNotes();
}
#Override
public Object getItem(int position) {
return notes.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
listItemsView = convertView;
if (listItemsView == null) {
listItemsView = LayoutInflater.from(NotesForDayActivity.context).inflate(R.layout.notes_list_layout, null);
}
underlineView = listItemsView.findViewById(R.id.underlineView);
notesListRelativeLayout = (RelativeLayout) listItemsView.findViewById(R.id.notesListRelativeLayout);
noteTitleTextView = (TextView) listItemsView.findViewById(R.id.noteTitleTextView);
tickImage = (ImageView) listItemsView.findViewById(R.id.tickImageView);
noteTitleTextView.setText(notes.get(position).getTitle());
return listItemsView;
}
I can access an item inside my ListView with a OnItemClickListener, but I do not know how to access a View inside that particular Item of my ListView.
So If I set my OnClick like this:
notesListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
How can I reference one of the three Views in that item. For example I need to set the visibility of my imageview:
Any help is appreciated.
Try this, OnItemClickListener will return the view, so that you can get access through that view
notesListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView tickImage = view.findViewById(R.id. tickImageView);
if(tickImage!=null){
tickImage.setVisibility(View.GONE);
}
}
});
I'm trying to get ArrayList with different variables that i'm getting from a database to display in a customized listview
this is the main xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View the balance sheet"
android:textSize="29sp"
android:id="#+id/view"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView">
</ListView>
</RelativeLayout>
this is the customized layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/description_names"
android:text="description"
android:layout_marginTop="25sp"
android:layout_marginLeft="15sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/values_names"
android:text="value"
android:layout_marginTop="25sp"
android:layout_marginLeft="105sp"/>
</RelativeLayout>
this is the adapter, i wanted both the ArrayLists from the main activity but it's not working
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(#NonNull Context context, ArrayList<String> mDetails,ArrayList<String> mValues) {
super(context,R.layout.custom_layout ,mDetails);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(getContext());
View customView=inflater.inflate(R.layout.custom_layout,parent,false);
String details=getItem(position);
String values=getItem(position);
TextView description_names=(TextView)customView.findViewById(R.id.description_names);
TextView values_names=(TextView)customView.findViewById(R.id.values_names);
description_names.setText(details);
return customView;
}
}
main activity
ListView listvIew=(ListView)findViewById(R.id.listView);
final ArrayAdapter<String>array=new CustomAdapter(this,mDetails,mValues);
listvIew.setAdapter(array);
private ArrayList<String>mDetails=new ArrayList<>();
private ArrayList<String>mValues=new ArrayList<>();
V3Ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value=dataSnapshot.child("records").child("values").getValue(String.class);
Log.d(TAG, "onChildAdded: the value is"+value);
mDetails.add(value);
mValues.add(value);
array.notifyDataSetChanged();
}
i'm getting values from the database then putting them in ArrayLists then i'm trying to display them in a customized listview but it's not working when i try to use the second Arraylist
adapter calls from your main activity should be like,
CusatomeAdapter adapter;
adapter = new CusatomeAdapter(mActivity,mList1 ,mList2);
mListview.setAdapter(adapter);
here is your adapter calss
public class CusatomeAdapter extends BaseAdapter {
public ArrayList<String> mList1;
public ArrayList<String> mList2;
Activity activity;
public CusatomeAdapter(Activity activity,ArrayList<String> mList1 , ArrayList<String> mList2){
super();
this.activity = activity;
this.mList1 = mList1;
this.mList2 = mList2;
}
#Override
public int getCount() {
return mList1.size(); //give the size of list here
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_detail, null);
holder = new ViewHolder();
holder.tv_detail = (TextView) convertView.findViewById(R.id.tv_detail);
//same way define another text view and set their value ..
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_detail.setText(mList1.get(position).toString());
return convertView;
}
private class ViewHolder {
TextView tv_detail;
}
}
I Have created a Spinner using adapter with icons and text. Everything is displayed correctly but there is a requirement that Activity spinner should show text only. Icons should only appear in the drop-down list.
It this possible to implement?
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
String[] countryNames;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] flags, String[] countryNames) {
this.context = applicationContext;
this.flags = flags;
this.countryNames = countryNames;
inflter = (LayoutInflater.from(applicationContext));
}
[id3844467|#Override]
public int getCount() {
return flags.length;
}
[id3844467|#Override]
public Object getItem(int i) {
return null;
}
[id3844467|#Override]
public long getItemId(int i) {
return 0;
}
[id3844467|#Override]
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_items, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
TextView names = (TextView) view.findViewById(R.id.textView);
icon.setImageResource(flags[i]);
names.setText(countryNames[i]);
return view;
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="5dp"
android:src="#drawable/ic_launcher" /> // for check that icon existing
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="0dp"
android:text="Demo" // for check that text existing
android:textColor="#000" />
</LinearLayout>
Set these two property in spinner hopeFully it will work
android:dropDownHeight="0dp"
android:dropDownWidth="0dp"
i'am not sure but change your getItemId like this :
#Override
public long getItemId(int position) {
return position;
}
and don't return null on getItem
Hoping someone can help. I'm having an issue it seems inflating my view. My goal is to display a list of items in a custom layout using a custom array adapter. I re-worked some of my original code using another SO post, but where my simple adapter worked fine, the new list doesn't display. Please see below:
This is my Fragment class.
HomeFeedFragment.java
public class HomeFeedFragment extends ListFragment {
private ListView listView;
private ArrayList<MainEvent> items;
private MainEventListViewAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_homefeed,
container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//GetEvents is just a test method to return a list of pre-filled event objects
items = GlobalList.GetEvents();
adapter = new MainEventListViewAdapter(getActivity(), android.R.id.list, items);
listView.setAdapter(adapter);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// do something with the data
}
}
My custom row layout xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip" />
</LinearLayout>
<!-- Heading Text -->
<TextView
android:id="#+id/eventheader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:textColor="#040404"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<!-- Event Description -->
<TextView
android:id="#+id/eventdescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbnail"
android:layout_below="#id/eventheader"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:textColor="#343434"
android:textSize="10sp"
tools:ignore="SmallSp" />
<!-- Posted Time -->
<TextView
android:id="#+id/eventpostedtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/eventheader"
android:layout_marginRight="5dip"
android:gravity="right"
android:textColor="#10bcc9"
android:textSize="10sp"
android:textStyle="bold"
tools:ignore="SmallSp" />
</RelativeLayout>
The actual fragment layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
And my custom ArrayAdapter:
public class MainEventListViewAdapter extends ArrayAdapter<MainEvent> {
private Context context;
private List<MainEvent> eventList;
public MainEventListViewAdapter(Context context, int textViewResourceId, List<MainEvent> eventList){
super(context, textViewResourceId, eventList);
this.context = context;
//this.eventList = eventList;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtEventHeader;
TextView txtEventDescription;
TextView txtEventPostedTime;
}
public int getCount(){
if(eventList!=null){
return eventList.size();
}
return 0;
}
public MainEvent getItem(int position){
if(eventList!=null){
return eventList.get(position);
}
return null;
}
public long getItemId(int position){
if(eventList!=null){
return eventList.get(position).hashCode();
}
return 0;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
MainEvent rowItem = getItem(position);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null){
convertView = inflater.inflate(R.layout.fragment_homefeed_rowlayout, parent, false);
holder = new ViewHolder();
holder.txtEventHeader = (TextView) convertView.findViewById(R.id.eventheader);
holder.txtEventDescription = (TextView) convertView.findViewById(R.id.eventdescription);
holder.txtEventPostedTime = (TextView) convertView.findViewById(R.id.eventpostedtime);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtEventHeader.setText(rowItem.getHeadline());
holder.txtEventDescription.setText(rowItem.getDescription());
holder.txtEventPostedTime.setText(rowItem.getPostedTime());
/*TextView text = (TextView) v.findViewById(R.id.label);
text.setText(m.getHeadline());
ImageView imageView = (ImageView) v.findViewById(R.id.icon);
imageView.setTag(m.getImageURL());
//DownloadImageTask dt = new DownloadImageTask();
dt.execute(imageView);
return v;*/
return convertView;
}
public List<MainEvent> getEventList(){
return eventList;
}
public void setEventList(List<MainEvent> eventList){
this.eventList = eventList;
}
}
^ I initially had an Async Task in this class to retrieve images for each item, but I've removed all of that for now.
I don't get any actual errors in LogCat, no crashes or anything, the View just isn't inflating. Any help is appreciated.
Well this is embarrassing. I found out the issue. Apparently, I forgot to uncomment the line in the adapter constructor that initializes the event list:
//this.eventList = eventList;
I'd commented it out temporarily to fix another bug. Working fine now, but thanks for your suggestion.
Why everytime the onItemClick() event is invoked on a ListView with a custom adapter, the getView() method in the adapter is called again?
I have the following code in API level 7, and when I try to change the checked value in the row's CheckedTextView object (stored in a ViewHolder), the adapter's getView() for each row is invoked and I got a strange behaviour(The selected row in the listView check/uncheck the CheckedTextView in another row) :
The code for the Activity is:
public class EnvioImagenesActivity extends Activity implements OnItemClickListener {
ListView listView;
private EnvioImagenAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.envio_imagenes);
listView=(ListView) findViewById(R.id.listViewEnvios);
adapter=new EnvioImagenAdapter(this,Store.getImages());
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> arg0, View view, int pos,
long id) {
ViewHolder holder=(ViewHolder) view.getTag();
holder.checkedTextView.toggle();
}
}
The code for the Xml layout activity is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/envioImagenes"
android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center"/>
<ListView
android:id="#+id/listViewEnvios"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:choiceMode="multipleChoice">
</ListView>
</LinearLayout>
The listview adapter:
public class EnvioImagenAdapter extends BaseAdapter {
private List<ImageUri> items;
private Context context;
public EnvioImagenAdapter(Context context, List<ImageUri> items) {
this.context=context;
this.items = items;
}
public int getCount() {
return items.size();
}
public Object getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("---getView() method called");
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row_envio_imagen, null);
holder = new ViewHolder();
holder.imageView = (ImageView) v.findViewById(R.id.imageView1);
holder.textView = (TextView) v.findViewById(R.id.textView1);
holder.temporalProgressBar = (ProgressBar) v
.findViewById(R.id.progressBar1);
holder.checkedTextView = (CheckedTextView) v
.findViewById(R.id.checkedTextView12);
holder.selected = true;
holder.position = position;
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
ImageUri imageUri = items.get(position);
File f = new File(imageUri.getImageUri().getPath());
String fileSize = Util.formatFileSize(f.length());
holder.textView.setText(fileSize);
new ImageTask(imageUri).execute(holder);
return v;
}
class ImageTask extends AsyncTask<ViewHolder, Void, Bitmap> {
public ImageTask(ImageUri imageUri) {
this.imageUri = imageUri;
}
private ImageUri imageUri;
private ViewHolder viewHolder;
#Override
protected Bitmap doInBackground(ViewHolder... params) {
viewHolder = params[0];
Bitmap bmp = BitmapFactory.decodeFile(imageUri.getThumbUri()
.getPath());
return bmp;
}
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
viewHolder.temporalProgressBar.setVisibility(View.GONE);
viewHolder.imageView.setVisibility(View.VISIBLE);
viewHolder.imageView.setImageBitmap(result);
};
}
class ViewHolder {
public int position;
CheckedTextView checkedTextView;
ProgressBar temporalProgressBar;
ImageView imageView;
TextView textView;
boolean selected;
}
}
And the xml layout for each row is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="45dip"
android:layout_height="45dip"
android:layout_alignParentLeft="true" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="45dip"
android:layout_height="45dip"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:layout_toRightOf="#id/imageView1" />
<CheckedTextView
android:id="#+id/checkedTextView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="?android:attr/textCheckMark"
android:checked="true"
android:layout_alignParentRight="true">
</CheckedTextView>
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_below="#+id/textView1"
android:layout_toLeftOf="#id/checkedTextView12"
android:layout_toRightOf="#id/imageView1"
android:paddingLeft="5dip"
android:paddingTop="2dip" />
</RelativeLayout>
And when I run the app in the simulator and make click in a row, I obtain in the LogCat:
System.out(2947): ---getView() method called
for each row displayed in the list.
Thanks in advance!!!!
I had a similar problem in the code caused
ListView.setChoiceMode (ListView.CHOICE_MODE_SINGLE);
When you click on the item for each item getview summoned again, in your case, you can try to remove android:choiceMode="multipleChoice"
Maybe this will help you.
I have noticed that accessing the views from outside the getView() rarely produces the expected behaviour. I suppose it is because of the recycling mechanism. Two alternative options work:
1) implement onClick() in getView()
2) If you want to keep your logic out of the adapter, in onItemClick(), change the underlying data and call adapter.notifyDatasetChanged(). The checkbox will toggle.