I am developing an app which has a listView having 1 textView which displays the content and one imageView. What i want is, when i click the listView i want to set the imageView with the tick mark image which i have. Its working fine. Once i click the listView, the tick mark image is loaded on that listview item on which i clicked.
The problem arises when i scroll. When i scroll, i could see some other listview item down below has a tick mark loaded. not sure how the image was set on that position.
I have read somewhere that when the listView is scrolled, the view is refreshed. Is that causing the problem?
Can anyone help how can iresolve this? I want the image to be shown(loaded) on the listView item on which i clicked and now other listView item.
Below is the xml stating listView items
method_item.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"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:text="This is a check box button which reacts upon the check that user clicks. I am testing it with the big string and checking how it looks" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignParentRight="true"
android:layout_marginRight="34dp" />
</RelativeLayout>
Below is the part of class snippet:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.method_list_main);
final ListView list = (ListView)findViewById(R.id.listView1);
adapter=new MethodLazyAdapter(this,ARRAY.preparationSteps,ARRAY.timeToPrepare,ARRAY.arrowValue,noOfSteps,list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
RelativeLayout ll = (RelativeLayout) view;
ImageView image = (ImageView)ll.findViewById(R.id.imageView2);
if(tick[position]){
tick[position] = false;
image.setImageResource(0);
}
else{
tick[position] = true;
image.setImageResource(R.drawable.select_right);
}
System.out.println("Position is: "+position);
}
});
}
Initially all tick[i] value is set to false.
Below is the getView function of adapter class
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.method_item, null);
TextView text = (TextView)vi.findViewById(R.id.textView1);
text.setText(preparationSteps\[position\]);
return vi;
}
Try this to get what you want
in public area add this variable
public SparseBooleanArray checked = new SparseBooleanArray();
then in onItemClick
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
boolean stat = checked.get(position, false);
checked.put(position, !stat);
adapter.setChecked();
System.out.println("Position is: "+position);
}
});
and in Adapter Class
If adapter not sub class in activiy add variable in public area
private SparseBooleanArray checked = new SparseBooleanArray();
and add this method to class
public void setChecked(SparseBooleanArray ch){
checked = ch;
notifyDataSetChanged();
}
Or if it sub class use our cheked variable we defined it up
then in getView method
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.method_item, null);
TextView text = (TextView)vi.findViewById(R.id.textView1);
text.setText(preparationSteps\[position\]);
ImageView image = (ImageView)vi.findViewById(R.id.imageView2);
if(checked.get(position, false)){
image.setImageResource(0);
}
else{
image.setImageResource(R.drawable.select_right);
}
return vi;
}
Please let me know if this help you.
Check out this post:
android - populating ListView with data from JSONArray
the reason for that is bacouse your are using your converted view wrong.
follow the example code on the post i paste.
if you will inflate your row view every time this problem will disappear. but this implementation is not recommended.
Related
I'm trying to bind a custom layout (a LinearLayout containing two TextViews) to a Spinner. I subclassed ArrayAdapter (mostly) correctly. Selecting an item in the Spinner calls getView() correctly, setting the LinearLayout's TextViews' values correctly. The problem is the initial display of the items in the Spinner (when clicking on the Spinner) just shows Objects; not the TextViews they should be displaying. Only AFTER clicking on one of the Objects does the Spinner correctly set the TextViews using my custom adapter's getView() method. Here's the custom adapter class:
public class BusRouteAdapter extends ArrayAdapter<BusRoute> {
...
public BusRouteAdapter(Context context, int resource, int textViewId, ArrayList<BusRoute> routes) {
super(context, resource, textViewId, routes);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
BusRoute route = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.bus_route, parent, false);
}
TextView tvBusRoute = (TextView) convertView.findViewById(R.id.tvBusRoute);
TextView tvBusRouteNumber = (TextView) convertView.findViewById(R.id.tvBusRouteNumber);
tvBusRoute.setText(route.routeName);
tvBusRoute.setTag(route.route);
tvBusRouteNumber.setText(route.route);
if (!route.routeColor.equals("")) {
tvBusRouteNumber.setBackgroundColor(Color.parseColor(route.routeColor));
}
return convertView;
}
}
Here's the layout that is to be used for each Spinner list item (bus_route.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/tvBusRouteNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:padding="8dp" />
<TextView
android:id="#+id/tvBusRoute"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:padding="8dp" />
</LinearLayout>
And in my Activity, I'm setting the adapter to a properly-populated list of BusRoute objects:
busRouteAdapter = new BusRouteAdapter(getBaseContext(), R.layout.bus_route, R.id.tvBusRoute, arrayOfBusRoutes);
routesSpinner.setAdapter(busRouteAdapter);
It does seem strange that I need to pass the Layout id (R.layout.bus_route) AND one of the TextViews contained in that Layout (R.id.tvBusRoute).
Here's what is rendered when clicking on the Spinner:
But if I click one of the Objects, getView() is called, and the selected Layout and TextViews are rendered properly (apparently I selected "GMU - Pentagon"):
What am I missing to get the Spinner's popup list to display ALL my bus route items rendered correctly?
I think you need to override getDropDownView to deal with spinners
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
BusRoute route = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.bus_route, parent, false);
}
TextView tvBusRoute = (TextView) convertView.findViewById(R.id.tvBusRoute);
TextView tvBusRouteNumber = (TextView) convertView.findViewById(R.id.tvBusRouteNumber);
tvBusRoute.setText(route.routeName);
tvBusRoute.setTag(route.route);
tvBusRouteNumber.setText(route.route);
if (!route.routeColor.equals("")) {
tvBusRouteNumber.setBackgroundColor(Color.parseColor(route.routeColor));
}
return convertView;
}
I made a custom listView and custom array adapter with 1 image and 2 textviews. What I want to work is when you click on an item in the listview that it opens a new activity with an intent.
this is the mainactivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.?_main);
ListView listView = (ListView) findViewById(R.id.list);
ArrayList<Subjects> obsclassiclist = new ArrayList<Subjects>();
obsclassiclist.add(new Subjects("?", "?", R.mipmap.ic_launcher));
obsclassiclist.add(new Subjects("?", "?", R.mipmap.ic_launcher));
WordAdapter adapter = new WordAdapter(this, obsclassiclist);
listView.setAdapter(adapter);
LinearLayout listViewIntent = (LinearLayout) findViewById(R.id.intent);
listViewIntent.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
if (position==0) {
Intent tutorial = new Intent(view.getContext(),
Cl_01.class);
// Start the new activity
startActivityForResult(tutorial,0);
}
if (position==1) {
Intent requirements = new Intent(view.getContext(),
CL_02.class);
// Start the new activity
startActivityForResult(requirements,1);
}
}
});
}
}
this the custom adapter
public WordAdapter(Activity context, ArrayList<Subjects> subjects) {
// Here, we initialize the ArrayAdapter's internal storage for the context
and the list.
// the second argument is used when the ArrayAdapter is populating a single
TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the
adapter is not
// going to use this second argument, so it can be any value. Here, we used
0.
super(context, 0, subjects);
}
/**
* Provides a view for an AdapterView (ListView, GridView, etc.)
*
* #param position The position in the list of data that should be displayed
in the
* list item view.
* #param convertView The recycled view to populate.
* #param parent The parent ViewGroup that is used for inflation.
* #return The View for the position in the AdapterView.
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {#link subject} object located at this position in the list
Subjects currentSubjects = getItem(position);
// Find the TextView in the list_item.xml layout with the ID subject
TextView subjectTextView = (TextView)
listItemView.findViewById(R.id.subject);
// Get the version name from the current wordadapter object and
// set this text on the subject TextView
subjectTextView.setText(currentSubjects.getObsclassicSubject());
// Find the TextView in the list_item.xml layout with the ID description
TextView descriptionTextView = (TextView)
listItemView.findViewById(R.id.description);
// Get the version number from the current wordadapter object and
// set this text on the description TextView
descriptionTextView.setText(currentSubjects.getObsclassicDescription());
// Find the Imageview in the list_item.xml layout with the ID version_number
ImageView pictureImageView = (ImageView)
listItemView.findViewById(R.id.image);
// Get the version number from the current wordadapter and
// set this image on the imageview
pictureImageView.setImageResource(currentSubjects.getObsclassicimageid());
// Return the whole list item layout (containing 2 TextViews and an
ImageView)
// so that it can be shown in the ListView
return listItemView;
}
}
here is the xml custom listview
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/intent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/image"
android:scaleType="centerInside"
android:src="#mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/subject"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingBottom="10dp"
android:textSize="18sp"
android:textStyle="italic"
android:id="#+id/description"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
and the xml from the main activity
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list">
</ListView>
if somebody has a solution pls let me know
You are doing wrong, your are making clicklistener on linear layout, instead do it on ListView
Change this
listViewIntent.setOnItemClickListener(new
AdapterView.OnItemClickListener()
To
listView .setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
// Now if you want to startActivity
Intent tutorial = new Intent(MainActivity.this,
Cl_01.class);
// Start the new activity
startActivityForResult(tutorial,position);
}
});
I have a list view and inside it's items I have a view pager as you can see in the below code.
While the viewPager is inside the item view List doesn't accept onItemClickListener and when I click Items nothing happens.
One solution that I found was adding onClickListener to ViewPager pages , But I think this is not a good solution because If you list contains 100 Items and each item have a view pager that contains 5 page so I should define 500 Listener Instead of one Listener.
I tried all solutions that I searched for example changing the focusable or clickable attribute of root view.
Please share your suggestions with me.
This is the list item layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/propertylist_item_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/purple"
android:paddingBottom="1dp"
android:descendantFocusability="blocksDescendants" >
<!-- background slider -->
<android.support.v4.view.ViewPager
android:id="#+id/property_images_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- favorite icon -->
<ImageButton
android:id="#+id/favorite_button"
android:contentDescription="#string/favorite_button_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:layout_margin="10dp"
android:src="#drawable/ic_favorite" />
...
<!-- Some other tags -->
This is the list adapter code :
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
LayoutInflater inflater;
inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.property_listing_item, parent, false);
}
RPropertyStorage property = getItem(position);
if (property != null)
{
PropertyListingImagesAdapter adapter = new PropertyListingImagesAdapter(property.getImages());
ViewPager imagesPager = (ViewPager) view.findViewById(R.id.property_images_view_pager);
imagesPager.requestDisallowInterceptTouchEvent(false);
imagesPager.setAdapter(adapter);
}
return view;
}
OnItemClickListener of ListView :
list.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
RToast.getInstance().showMessage("Clicked");
RLog.info("Clicked");
}
});
ViewPager has very "strong" code that handle touch events. For me it is only one way to catch viewpager clicks through it's own clicklisteners.
Don't care about
If you list contains 100 Items and each item have a view pager that contains 5 page so I should define 500 Listener Instead of one Listener.
because listview creates only as many view as can display on the screen at the same time + 2; You can check it by such code in your adapter
int count=0;
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
LayoutInflater inflater;
inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.property_listing_item, parent, false);
Log.v("","creating "+count);
count++;
}
//your code
}
Listener is only links. It is no matter how many links you have on some method. Just check if it is not null.
Apologies if this question has been asked previously.
I have a ListFragment that contains a Button in each list item. Clicking on the Button should change the background color of that particular list item. I understand that the ListView inside the ListFragment refreshes itself implying that if a user scrolls it is likely that a list item that was not previously clicked will have its background color changed.
Many of the solutions I have come across involve storing the position of the list item that was clicked in an overridden getView(int position, View convertView, ViewGroup parent) method implemented in a custom adapter (that extends SimpleAdapter in my case).
However my problem is compounded by the presence of a Button inside every list item implying that the Button will not be clickable unless I attach an OnClickListener inside a custom adapter (this part is done). Now, trying to store the position of the Button's list item fails because the saved position is always a refreshed position and not the absolute position of the list item. I also tried setting tags for each button but even those get recycled as the page scrolls, which is (unfortunately) expected behaviour considering the design of ListView.
I understand that this problem is easily solvable if the a list item does not have children that need to be clicked. But what if there are children within every list item that need to respond to clicks separate from the parent view that contains each list item ? I have also tried using selectors to manipulate a list item's background but that hasn't helped either.
Here is my Custom Adapter's getView method:
#Override
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
final int pos = position;
if(view == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.fragment_layout, null);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Log.d("CLICKED POSITION",Integer.valueOf(pos).toString());
View parent = (View) v.getParent();
parent.setBackgroundColor(Color.GREEN);
}
});
}
return view;
}
And the XML layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:descendantFocusability="blocksDescendants"
android:layout_margin="5dp"
>
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label"
android:layout_gravity="start"
/>
<TextView
android:id="#+id/text_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="end"
/>
<Button
android:id="#+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button"
android:layout_margin="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:clickable="true"
/>
</LinearLayout>
I have spent many hours on this problem and I am unable to arrive at a solution. Any direction, guidance or a solution would be most appreciated.
In your Custom Adapter's getView() method, you are inflating the view and attaching the onclicklistener only if the convertView is null. convertView IS the recycled view. If convertView is not null, then you need to change the values in that view. Your getView() method should be like this instead
#Override
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
final int pos = position;
//No recycled view, so create a new one
if(view == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.fragment_layout, null);
}
//By this line, we either have a view that is created in the if block above or passed via convertView
Button button = (Button) view.findViewById(R.id.button);
//attach listener to the view, recycled or not
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Log.d("CLICKED POSITION",Integer.valueOf(pos).toString());
//parent not required as we are have the view directly
//View parent = (View) v.getParent();
v.setBackgroundColor(Color.GREEN);
}
});
return view;
}
Indeed you should also be changing any values associated with that item according to that position, if they change. For example, if each button displays the position in the list, you should also add the following line
button.setText("Button " + position);
I made a custom Listview (Without overriding getView() method) with each item in a Listview having a following Layout
contactlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" android:weightSum="1">
<CheckBox android:id="#+id/checkBox1" android:text="CheckBox" android:layout_width="134dp" android:layout_height="108dp" android:focusable="false"></CheckBox>
<LinearLayout android:id="#+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical" android:layout_width="87dp" android:layout_weight="0.84" android:weightSum="1" >
<TextView android:text="TextView" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/name" android:layout_weight="0.03"></TextView>
<TextView android:text="TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/phone"></TextView>
<TextView android:text="TextView" android:layout_height="wrap_content" android:layout_weight="0.03" android:layout_width="match_parent" android:id="#+id/contactid" android:visibility="invisible"></TextView>
</LinearLayout>
</LinearLayout>
I am populating the Listview using a SimpleCursorAdapter in a following way...
Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String from[] = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
int to[] = new int[]{R.id.name,R.id.phone,R.id.contactid};
SimpleCursorAdapter s = new SimpleCursorAdapter(this,R.layout.contactlayout,c,from,to);
lv.setAdapter(s);
On Click of a button I am reading the states of all the Checkboxes. The problem is, if I check one CheckBox several others down the line get automatically Checked. I know this is reusing of Views. How do I avoid it ?. I am not even overriding getView() method in this case, so I wonder if there is still any way to achieve what I want?
Answer
Finally I implemented what #sastraxi suggested...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
final CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
final TextView name = (TextView) view.findViewById(R.id.name);
final TextView contactId = (TextView) view.findViewById(R.id.contactid);
final int pos = position;
checkBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(checkBox.isChecked())
{
checkList.add(String.valueOf(pos));
nameList.add(name.getText().toString());
contactList.add(contactId.getText().toString());
Log.i("Chk added",String.valueOf(pos));
}
else
{
checkList.remove(String.valueOf(pos));
nameList.remove(name.getText().toString());
contactList.remove(contactId.getText().toString());
Log.i("Un Chk removed",String.valueOf(pos));
}
}
});
if(checkList.contains(String.valueOf(pos)))
{
checkBox.setChecked(true);
}
else
{
checkBox.setChecked(false);
}
return view;
}
another way to force not reusing the views, add the following in your cursor adapter:
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return 500;
}
but yes, you should reuse views whenever possible. In my particular case, I am adding images to my views and without forcing to not reuse the view, they would get re-rendered in the reused views.
Ah, I see what the problem is.
Make a new class that extends SimpleCursorAdapter, say CheckboxSimpleCursorAdapter, and override getView as such:
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.CheckBox1);
checkBox.setChecked(getIsThisListPositionChecked(position));
return view;
}
As you're not using a layout whose top-level View implements Checkable, you have to do everything yourself. That includes clearing state (in this case), as the default implementation re-used a View that was checked--as you correctly intuited.
Edit: use this new code, and implement a protected boolean getIsThisListPositionChecked(int position) method that returns whether or not the item is currently checked (or something like that). I hope I'm being clear enough--you need to figure out if the item should be checked according to your model, and then set that when you create the View.
It's always better to re-use views. What you are trying to achieve can be done with a few tweaks in your code. You need to record the checkedness of your checkboxes by using aanother variable list (a boolean for each list item).
Override these two methods in your Adapter class
#Override
public int getViewTypeCount()
{
return getCount();
}
#Override
public int getItemViewType(int position)
{
return position;
}