Android custom ArrayAdapter getView method called multiple times - resetting dynamic TextView value - android

The getView method in my custom ArrayAdapter is getting called multiple times, which I assume it is meant to. Problem is, I have a quantity TextView which is dynamically set but when you scroll and the box goes off screen the value disappears. I am not sure what I am doing wrong and Google is not proving to be much help. Hopefully someone here can help me.
The adapater is called:
adapter = new MenuAdapter(thisActivity, R.layout.menu, list);
setListAdapter(adapter);
My Custom ArrayAdapter:
public MenuAdapter(Context context, int textViewResourceId, ArrayList<Object> menu) {
super(context, textViewResourceId, menu);
this.menu = menu;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Object cat = menu.get(position);
if (cat.getClass().equals(Category.class)) {
v = vi.inflate(R.layout.category, null);
Category item = (Category)cat;
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
TextView tt = (TextView) v.findViewById(R.id.category);
tt.setText(item.getName());
} else if (cat.getClass().equals(OrderItem.class)) {
v = vi.inflate(R.layout.menu, null);
OrderItem orderItem = (OrderItem)cat;
Item item = orderItem.getItem();
TextView tt = (TextView) v.findViewById(R.id.title);
tt.setText(item.getName());
TextView bt = (TextView) v.findViewById(R.id.desc);
bt.setText(item.getDescription());
TextView qty = (TextView) v.findViewById(R.id.qty);
qty.setId(item.getId());
ImageButton minus = (ImageButton) v.findViewById(R.id.qtyMinus);
minus.setTag(item);
ImageButton plus = (ImageButton) v.findViewById(R.id.qtyPlus);
plus.setTag(item);
}
return v;
}
The menu layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip"
android:background="#FFFFFF">
<RelativeLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent"
android:cacheColorHint="#FFFFFF"
android:background="#FFFFFF">
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="16px"
android:textColor="#000000"
/>
<TextView
android:id="#+id/desc"
android:layout_below="#id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="11px"
android:textStyle="italic"
android:textColor="#000000"
/>
</RelativeLayout>
<ImageButton
android:id="#+id/qtyMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/minus"
android:paddingTop="15px"
android:onClick="minusQty" />
<TextView
android:id="#+id/qty"
android:layout_width="50px"
android:layout_height="50px"
android:textColor="#000000"
android:textSize="18px"
android:gravity="center_vertical|center_horizontal"
android:freezesText="true"
android:background="#android:drawable/editbox_background"/>
<ImageButton
android:id="#+id/qtyPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/plus"
android:paddingTop="15px"
android:onClick="addQty" />
</LinearLayout>

getView() will be called multiple times as you note. It shouldn't matter, because your array adapter is based on the state of it's internal data model (array, list of objects, whatever). getView() should be idempotent in that calling it multiple times shouldn't change the result.
You say "when you scroll and the box goes off screen the value disappears". Note sure what mean. When you scroll one of the views generated in getView() off the visible area, and when you scroll it back, the value is different? without any other information, I'd have to say that's not possible. The reason is again that unless you are modifying the internal state of the adapter, or changing the adapter, you will always generate the same view for a given position.
By the way, convertView can be null, so you want to do something like,
View v = convertView;
if (v == null) {
v = inflater.inflate(...);
}
// now use v in the rest of the method

In your listview set android:height = "match_parent". Now getview called your dataset count. we can reuse the convertview. Check the convertview if it is NULL inflate your view. Otherwise, write your remaining code.
if(convertView ! = null)
{
//rest of your Code
}
else
{
//inflate that view
}

Related

Android - Get a View (EditText) by ID from an ListView or ArrayAdapter

I have an application in android which lets you manage your school grades. It is just a project for learning so nothing serious.
Now if you open the details from a subject you can see the grades and edit them. If you then click on save I have to check if something changed or something new was added.
Now my problem is that I don't now how I can get a specific view from a ArrayAdapter or from the ListView because every one of those grade fields are generated by the ArrayAdapter and I have to check them all for changes.
So I have a ArrayAdapter which fills a ListView with the following elements (list_grades_prefab.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:paddingLeft="10pt"
android:weightSum="2"
android:orientation="horizontal">
<EditText
android:id="#+id/grade_field"
android:textSize="10pt"
android:textAlignment="center"
android:layout_weight="1"
android:layout_marginRight="10pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/weight_field"
android:textSize="10pt"
android:textAlignment="center"
android:layout_weight="0.7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="0.3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10pt"
android:textStyle="bold"
android:text="%"/>
</LinearLayout>
Heres the getView of the ArrayAdapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
if(convertView == null){
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_grades_prefab, null);
viewHolder.GradeField = (EditText) convertView.findViewById(R.id.grade_field);
viewHolder.WeightField = (EditText)convertView.findViewById(R.id.weight_field);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.GradeField.setText(String.valueOf(Grades.get(position)[0]));
viewHolder.GradeField.setTextColor(BasicFunctions.colorFromGrade(Grades.get(position)[0]));
viewHolder.WeightField.setText(String.valueOf(Grades.get(position)[1]).split("\\.")[0]);
return convertView;
}
It just fills the different EditText views with text.
Here I set the ArrayAdapter (GradesAdapter):
ListView gradeListView = (ListView)findViewById(R.id.grade_list_view);
GradesAdapter gradesAdapter = new GradesAdapter(this, R.layout.list_grades_prefab, grades);
gradeListView.setAdapter(gradesAdapter);
grade_list_view is the ListView and grades is an two dimensional array type double.
How do I get now every EditText with the id grade_field from the ArrayAdapter when I am not inside the getView method?
After the user edits the data, call notifyDataSetChanged on your adapter. Since you are using a ListView instead of RecyclerView, that's your only option to inform the views that something changed.

Optimizing ListView adapter to display dynamic content in individual ListView items

I'm trying to display a ListView where individual list items feature a dynamic number of elements. Each item list will have a TextView for a date. Following the date, there can be between 2-5 elements featured.
ListItemA
DateTextView
2-5 TextView
ListItemB
DateTextView
2-5 TextView
ListItemC
DateTextView
2-5 TextView
...
With this current method, it displays the blank TextViews at the bottom of the ListView's item. Would there be a better way to do this?
I thought that convertView would have had methods to add layout elements to it, but that's not the case.
Adapter's getView method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = this.inflater.inflate(R.layout.layout_list_item, parent, false);
holder.text_date_cashout = (TextView) convertView.findViewById(R.id.text_date_cashout);
holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout1));
holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout2));
holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout3));
holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout4));
holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout5));
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
//get and set date from current cashOutHistory
CashOutHistory coh = cashOutHistory.get(position);
holder.text_date_cashout.setText(coh.getDate());
//loop individual cashOuts to display their values
int i = 0;
for (CashOut co : coh.getCashOutHistory() ){
holder.dynamic_views.get(i).setText(co.toString());
i++;
}
return convertView;
}
private class ViewHolder{
TextView text_date_cashout;
ArrayList<TextView> dynamic_views = new ArrayList<TextView>(5);
}
List item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/text_date_cashout" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/text_cashout1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/text_cashout2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/text_cashout3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/text_cashout4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/text_cashout5" />
</LinearLayout>
Edit #1
Loop showing and hidding the TextViews in my list item:
//loop individual cashOuts to display their values
int i = 0;
for (CashOut co : coh.getCashOutHistory() ){
holder.dynamic_views.get(i).setText(co.toString());
i++;
}
while(i < 5){
holder.dynamic_views.get(i).setVisibility(View.GONE);
i++;
}
You can hide each view you don't need showing for that particular item by using:
Boolean someCondition = true;
if (someCondition)
holder.dynamic_views.get(i).setVisibility(View.GONE);
Hope this helps.

Multiple Clickable Areas in ListItem

I have a list that is organized as follows:
CheckBox,
ImageButton,
LinearLayout{ TextView title, TextView Subtitle}
What I am trying to do is to have three separate clickable areas of the ListItem: the checkbox should be checkable, ImageButton, and the actual list item itself. The checkbox is hidden by default, but can be made visible when a certain button on the list's header is pressed (ie SELECT). I just want a way to select multiple items from the list, and I am not irreversibly set on using hidden checkboxes- if there is a way to do this that would be easier, I would be happy to hear the alternative.
Here is the relevant snippet of my adapter:
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
final T t = list.get(position);
if(convertView == null){
convertView = vi.inflate(R.layout.folder_list_item,parent,false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.text_view_folder_item);
holder.subtitle = (TextView) convertView.findViewById(R.id.text_view2);
holder.eye = (ImageButton) convertView.findViewById(R.id.folder_vis_button);
holder.check = (CheckBox) convertView.findViewById(R.id.phone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(t.getName());
holder.subtitle.setText(t.getDescription());// = (TextView) convertView.findViewById(R.id.text_view2);
return convertView;
}
And the XML is as follows. As you can see, I have made the individual list items clickable but unfocassable, as recommended in posts I have read about this very problem.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="true"
android:focusable="true">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:visibility="invisible"
android:focusable="false"/>
<ImageButton
android:id="#+id/folder_vis_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:layout_toRightOf="#+id/checkbox"
android:contentDescription="#string/contentDescription"
android:focusable="false"
android:src="#drawable/open_eyeball" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_toRightOf="#id/folder_vis_button"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:focusable="false"
android:clickable="true">
<TextView
android:id="#+id/text_view_folder_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/text_view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Clicking on the list item is handled, if I convert the listitem to actually use on of the built in listitems such as android.R.layout.simple_list_item_1.
However, when I use my own layout, I cannot get click events on the list item itself, just on the image button.
I have tried inflating the view in a different way (int resource, ViewGroup root) ... I have tried working just with the imagebutton, or just with the checkbox, using an imageview instead of an imagebutton, with little in terms of success. Thank you for taking the time to look at this.

how to add multiple imageview on listview row with horizontal scroll view

I want to display multiple imageview and text view on every row of listview and number of row is not fix (number of rows integer can be fetched from server)
How can i add horizontal scroll view in every row.
i am going to use for loop for number of imageview in getView method is it right way to do that.
My question is how can i inflate horizontal scroll with numbers image views in row in getview method
public View getView(final int position, View convertView,
ViewGroup parent)
{
final ViewHolder viewHolder;
viewHolder = new ViewHolder();
// View vi = convertView;
//if (vi == null)
//{
int Numberofimageview=2;
for(int i=0;i<Numberofimageview;i++)
{
// assume i want to display 2 imageview in every row
LayoutInflater inflater = (LayoutInflater) con
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.my_row_layout, parent,
false);
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
Log.d("myapp", "position" + position);
convertView.setTag(viewHolder);
// }
// else
// {
// viewHolder=(ViewHolder)convertView.getTag();
// }
viewHolder.title.setText(data.get(position));
}
return convertView;
}
This is xml for row
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/wagon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/wagon" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/wagon"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:src="#drawable/video" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_marginLeft="33dp"
android:layout_toRightOf="#+id/imageView1"
android:text="title of file" />
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/title"
android:layout_alignRight="#+id/title"
android:layout_marginBottom="18dp"
android:text="type of file" />
Try to include your relativeLayout into a HorizontalScrollView
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout>[...]</RelativeLayout>
</HorizontalScrollView>
Create a custom listview. Create a xml file. within the main linear layout of the xml file put imageview and textview. Make sure the orientation of the linear layout is horizontal.. Make use of that xml file in getview methood

Android: ListView bug

I have a custom list view which has a Textview and an image. When I click on the textview a hidden layout will be expanded for that particular row. But what happening was, for eg., when I click on 2nd row, 10th row is also getting expanded. Here is my code,
CustomListAdapter.java
public View getView(final int position, View convertView, ViewGroup parent) {
holder = null;
DataFields rowItems = (DataFields) getItem(position);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.home_field_row, null);
holder = new ViewHolder();
holder.dataFields = items.get(position);
holder.mName = (TextView) convertView
.findViewById(R.id.hmFieldName);
holder.mDeleteImage = (ImageView) convertView
.findViewById(R.id.hmFieldDeleteImage);
holder.deleteMainRL = (RelativeLayout) convertView
.findViewById(R.id.hmdeleteMainRL);
holder.mDeleteImage.setTag(position);
holder.mName.setTag(position);
holder.deleteMainRL.setTag(position);
final View clickView = convertView;
holder.mName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout displayAddInfo = (RelativeLayout)clickView.findViewById(R.id.displayRecordRL);
Animation expandAnim = expand(displayAddInfo,
true);
displayAddInfo
.startAnimation(expandAnim);
}
});
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.mName.setText(rowItems.getName());
return convertView;
}
How can I fix this? Any kind of help or suggestion is much appreciated. Thanks.
Update
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hmFieldMainRL"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/grid_shape" >
<TextView
android:id="#+id/hmFieldName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/displayRecordRL"
android:layout_alignParentLeft="true"
android:gravity="left"
android:padding="15dp"
android:shadowColor="#000000"
android:shadowDx="0"
android:shadowDy="0"
android:clickable="false"
android:shadowRadius="2"
android:text="#string/no_data"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#F2F2F2" />
<RelativeLayout
android:id="#+id/displayRecordRL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/hmFieldName"
android:layout_centerVertical="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:background="#drawable/display_record_bg"
android:visibility="gone" >
<EditText
android:id="#+id/displayRecordName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/displayRecordUpdate"
android:padding="10dp"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/displayRecordPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/displayRecordName"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="#+id/displayRecordShow"
android:inputType="textPassword"
android:paddingLeft="10dp"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/displayRecordAddInfoImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_below="#id/displayRecordPwd"
android:contentDescription="#string/right_arrow"
android:visibility="gone"
android:src="#drawable/info" />
<EditText
android:id="#+id/displayRecordAddInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/displayRecordAddInfoImg"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:hint="Additional Information"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="gone" />
<ImageView
android:id="#+id/displayRecordUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/display_record_img"
android:contentDescription="#string/right_arrow"
android:padding="10dp"
android:src="#drawable/update_rec" />
<ImageView
android:id="#+id/displayRecordShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/displayRecordUpdate"
android:layout_marginTop="10dp"
android:contentDescription="#string/right_arrow"
android:padding="10dp"
android:src="#drawable/eye" />
<ImageView
android:id="#+id/displayRecordShowRed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/displayRecordUpdate"
android:layout_marginTop="10dp"
android:contentDescription="#string/right_arrow"
android:padding="10dp"
android:src="#drawable/redeye"
android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>
But what happening was, for eg., when I click on 2nd row, 10th row is also getting expanded.
You are fighting the way ListViews recycle the row layouts. But this is easy enough to deal with, first let's add a couple new class variables:
SparseBooleanArray expanded = new SparseBooleanArray();
LayoutInflater inflater; // initialize this in your constructor
Now we'll use expanded to check whether displayRecordRL should be visible:
public View getView(final int position, View convertView, ViewGroup parent) {
DataFields rowItems = (DataFields) getItem(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.home_field_row, null);
holder = new ViewHolder();
holder.dataFields = items.get(position);
holder.mName = (TextView) convertView
.findViewById(R.id.hmFieldName);
holder.mDeleteImage = (ImageView) convertView
.findViewById(R.id.hmFieldDeleteImage);
holder.deleteMainRL = (RelativeLayout) convertView
.findViewById(R.id.hmdeleteMainRL);
// Add this to your holder
holder.mAddInfo = (RelativeLayout) convertView
.findViewById(R.id.displayRecordRL);
holder.mDeleteImage.setTag(position);
holder.mName.setTag(position);
holder.deleteMainRL.setTag(position);
holder.mName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Get the ViewHolder
holder = (ViewHolder) ((View) v.getParent()).getTag();
Animation expandAnim = expand(holder.mAddInfo, true);
holder.mAddInfo.startAnimation(expandAnim);
// Remember that this View is expanded
int pos = (Integer) v.getTag();
expanded.put(pos, true);
}
});
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.mName.setText(rowItems.getName());
if(expanded.get(position, false))
holder2.mAddInfo.setVisibility(View.VISIBLE);
else
holder2.mAddInfo.setVisibility(View.GONE);
return convertView;
}
Notice how this tracks whether each row should be visible and uses a simple if statement to make sure the recycled layout correctly shows or hide the "Add Info" section.
You need to add a attribute to your adapter, a simple int. When you click on an item, update the current selected position. When you build the view, check if position equals the selected position.
public View getView(final int position, View convertView, ViewGroup parent) {
if(this.selectedPosition == position){
//add a holder
else{
//don't add a holder
return convertview;
}
What I'm saying is that you are encountering a View recycling problem. If you modify item n°2 and then the view of item n°10 is build from the View n°2, you end up with an item unwanted (that will look as it was clicked).
When an item of your list is clicked:
1) update the selected item (attribute of your adapter). Example, if you click on item 12 of your listview, selectedItem = 12;
2) call method notifyDataSetChanged(). This will refresh the view of the list.
3) When you build the view (getView() of adapter), check for each position if it corresponds to the selected item. If it does, build your special view (I don't know exactly what you want to do). If it doesn't correspond to the selected item, build your view "normally"
Try to put the R.id.displayRecordRL as part of the viewholder and on the OnClick method call it.
holder.displayAddInfo = (RelativeLayout)clickView.findViewById(R.id.displayRecordRL);
and on the OnclickMethod :
Animation expandAnim = expand(holder.displayAddInfo,
true);
holder.displayAddInfo
.startAnimation(expandAnim);
}
The problem you experience could be that you animate a View (converView) that is being re-used for another item in your list-view when scrolling.
Look at this YouTube video and you may get an idea on how to solve your problem.
Chet Haase (Google engineer) "DevBytes" series "ListView Animations" :
https://www.youtube.com/watch?v=8MIfSxgsHIs#!

Categories

Resources