Gingerbread listview item animation issue - android

I've looked for this issue but nothing I've found matches my situation.
My problem occurs onAndroid 2.3.x (on 4.x it works perfectly)
I have an application with a custom list view. I initialize my listview as follows
ListAdapter mListAdapter = new ListAdapter(getApplicationContext(), this, ...);
lvSelector = (ListView) findViewById(R.id.listView1);
lvSelector.setAdapter(mListAdapter);
My ListAdapter is as follows:
public class ListAdapter extends BaseAdapter {
static class Holder {
LinearLayout layoutRoot, layoutColor;
TextView hText;
Animation anim = AnimationUtils.loadAnimation(mActivity, R.anim.anim_list_item);
public Holder() {
layoutRoot = new LinearLayout(mContext);
layoutColor = new LinearLayout(mContext);
hText = new TextView(mContext);
}
public Holder(Holder holder) {
this.layoutRoot = holder.layoutRoot;
this.layoutColor = holder.layoutColor;
this.hText = holder.hText;
}
}
int mSwap1, mSwap2;
Animation mAnimation;
public ListAdapter(Context _context, Activity _activity, FileHandler _fileHandler, String _strSchemaName, List<String> _list, List<String> _solution) {
mContext = _context;
mActivity = _activity;
mAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.anim_list_item);
mAnimation.reset();
mSwap1 = mSwap2 = -1;
/* ... */
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int fPosition = position;
View row = convertView;
Holder lHolder = null;
if (row==null) {
LayoutInflater inflater = mActivity.getLayoutInflater();
row = inflater.inflate(R.layout.layout_schema_element, parent, false);
lHolder = new Holder();
lHolder.layoutRoot = (LinearLayout) row.findViewById(R.id.elementLayoutRoot);
lHolder.layoutColor = (LinearLayout) row.findViewById(R.id.elementLayoutColor);
lHolder.hText = (TextView) row.findViewById(R.id.textViewWord);
row.setTag(lHolder);
}
else {
lHolder = (Holder)row.getTag();
}
row.setOnClickListener(null);
if (position==0 || position==mDataList.size()-1) {
lHolder.layoutColor.setBackgroundResource(R.drawable.bg_elem_fixed);
lHolder.layoutColor.setOnClickListener(null);
}
else {
lHolder.layoutColor.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveElement(fPosition);
}
});
}
lHolder.hText.setText(mDataList.get(position));
lHolder.layoutRoot.setBackgroundResource(0);
mHolder.set(position, lHolder);
return row;
}
}
protected void moveElement(int _element) {
if (mDataList.get(_element).equals(mSolution.get(_element)))
return;
if (mSwap1==-1)
{
System.out.println("setting swap1=" + _element);
mHolder.get(_element).layoutRoot.setBackgroundResource(R.drawable.bg_elem_selected_lite);
mSwap1 = _element;
}
else
{
if (mSwap2==-1)
{
System.out.println("setting swap2=" + _element);
mHolder.get(_element).layoutRoot.setBackgroundResource(R.drawable.bg_elem_selected_lite);
mSwap2 = _element;
}
}
if (mSwap1!=-1)
{
System.out.println("running animation on mSwap1=" + mSwap1);
mHolder.get(mSwap1).layoutRoot.clearAnimation();
mHolder.get(mSwap1).layoutRoot.startAnimation(mAnimation);
}
/***** THIS IS WHAT DOES NOT WORK *****/
if (mSwap2!=-1)
{
System.out.println("running animation on mSwap2=" + mSwap2);
mHolder.get(mSwap2).layoutRoot.clearAnimation();
mHolder.get(mSwap2).layoutRoot.startAnimation(mAnimation);
}
if (mSwap1!=-1 && mSwap2!=-1)
{
mHolder.get(mSwap1).layoutRoot.setBackgroundColor(0);
mHolder.get(mSwap2).layoutRoot.setBackgroundColor(0);
if (mSwap1==mSwap2)
{
mSwap1 = mSwap2 = -1;
return;
}
Collections.swap(mDataList, mSwap1, mSwap2);
Collections.swap(mHolder, mSwap1, mSwap2);
Collections.swap(dataObjs, mSwap1, mSwap2);
mSwap1 = mSwap2 = -1;
notifyDataSetChanged();
}
}
}
Everything works fine when I perform Collections.swap(list, mSwap1, mSwap2), elements are correctly swapped.
First animation (mSwap1) is run fine; my problem is that when second animation is run (mSwap2), it is executed on another element in screen even if mSwap2 is right (e.g.: mSwap1=1 -> second element in list is animated, mSwap2=2 -> n-1 element and n-2 element in list are animated where n is the number of visible elemnts).

I've solved my problem replacing animation calls
mHolder.get(idx).layoutRoot.clearAnimation();
mHolder.get(idx).layoutRoot.startAnimation(mAnimation);
with the following method
private void animateItem(int _index, Animation _animation) {
if (
_index<mLvSelector.getFirstVisiblePosition() // selected item is above first visible element
|| _index>mLvSelector.getLastVisiblePosition() // selected item is below last visible element
)
// element is invisible -> no animation
return;
int newIndex = _index;
if (
android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH // before android 4.0
&& mSwap2>=0 // second selection
&& mSwap1!=mSwap2 // second selection differs from first selection
)
newIndex = mLvSelector.getFirstVisiblePosition() + (mLvSelector.getLastVisiblePosition() - _index);
mHolder.get(newIndex).layoutRoot.clearAnimation();
mHolder.get(newIndex).layoutRoot.startAnimation(_animation);
}
Adding Animation argument to the method allows to differentiate animation between elements (mSwap1 and mSwap2).

Related

Execute Function/Procedure from Listview item post-delete adapter in xamarin android

Sorry, I'm using google translate.
Hello, I am developing an app in xamarin android with visual studio 2017 for taking orders where I send information from a fragment to a List<EN_WishDetalle> to store the selection of items and this is dumped to an adapter to show it in another fragment.
In layout design it has 3 buttons (add quantity/decrease quantity/delete item),
the error lies when removing an item from the listview with sList.RemoveAt(position); NotifyDataSetChanged();
This is deleted and updated in the listview but when I try to execute any of the mentioned buttons again they make a double call (it is as if they were pressed twice and they execute their function twice)
all as a result of the NotifyDataSetChanged();
I have already checked it, however if I close the fragment with backspace (back) and re-enter everything works correctly until I use the delete item button again and we return to the same problem; so I thought of avoiding using it (NotifyDataSetChanged();) and looking for a way to reload the fragment's listview from the adapter without any success. I would like your support or suggestion to take the right path.
adapter:
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
try
{
if (row == null)
{
row = LayoutInflater.From(sContext).Inflate(Resource.Layout.item_Wish, null, false);
}
TextView txtCodigo = row.FindViewById<TextView>(Resource.Id.Codigo);
txtCodigo.Text = sList[position].codigo;
TextView txtArticulo = row.FindViewById<TextView>(Resource.Id.Articulo);
txtArticulo.Text = sList[position].articulo;
EditText txtCantidad = row.FindViewById<EditText>(Resource.Id.Cantidad);
txtCantidad.Text = sList[position].cantidad.ToString();
TextView txtPrecio = row.FindViewById<TextView>(Resource.Id.Precio);
txtPrecio.Text = sList[position].importetotal;
ImageView Art = row.FindViewById<ImageView>(Resource.Id.Image);
Art.SetImageResource(Android.Resource.Color.Transparent);
if (sList[position].imagenproducto == "")
{
Art.SetImageResource(Resource.Drawable.NoDisponible);
}
else
{
Android.Net.Uri myUri = (Android.Net.Uri.Parse(sList[position].imagenproducto));
//Art.SetImageURI(myUri);
Art.SetImageURI(myUri);
}
Button buttonMax = row.FindViewById<Button>(Resource.Id.btnMax);
Button buttonMin = row.FindViewById<Button>(Resource.Id.btnMin);
ImageButton buttonDel = row.FindViewById<ImageButton>(Resource.Id.btnDel);
buttonMax.Click += delegate
{
sList[position].cantidad = sList[position].cantidad + 1;
txtCantidad.SetText(Convert.ToString(sList[position].cantidad), TextView.BufferType.Normal);
};
buttonMin.Click += delegate
{
sList[position].cantidad = sList[position].cantidad - 1;
txtCantidad.SetText(Convert.ToString(sList[position].cantidad), TextView.BufferType.Normal);
};
buttonDel.Click += delegate
{
Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(sContext);
AlertDialog alert = dialog.Create();
alert.SetTitle("GumisaAPP");
alert.SetMessage("Eliminar item : (" + position.ToString() + ") - " + sList[position].codigo + sList[position].articulo);
alert.SetIcon(Resource.Drawable.Alerta);
alert.SetButton("OK", (c, ev) =>
{
sList.RemoveAt(position);
NotifyDataSetChanged();
});
alert.SetButton2("CANCEL", (c, ev) =>
{
});
alert.Show();
//mAlertMessageBoxOk.onOkClick(5);
};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
finally { }
return row;
}
fragment:
private ListView WishlistView;
WishAdapter adapter;
List<EN_WishDetalle> List_Wish = new List<EN_WishDetalle>();
public override View OnCreateView(LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
//HasOptionsMenu = true;
var view = inflater.Inflate(Resource.Layout.Main_Wish, null);
WishlistView = view.FindViewById<ListView>(Resource.Id.List);
EditText txtCantidad = view.FindViewById<EditText>(Resource.Id.Cantidad);
List_Wish = Variables.WishDetalle;
adapter = new WishAdapter(Activity, List_Wish);
//adapter2 = new WishAdapter( ).;
WishlistView.Adapter = adapter;
//InputSearch = view.FindViewById<EditText>(Resource.Id.inputSearch);
//InputSearch.TextChanged += InputSearch_TextChanged;
//List<EN_Clientes> objstud = new List<EN_Clientes>();
WishlistView = view.FindViewById<ListView>(Resource.Id.List);
Button buttonMax = view.FindViewById<Button>(Resource.Id.btnMax);
Button buttonMin = view.FindViewById<Button>(Resource.Id.btnMin);
ImageButton buttonDel = view.FindViewById<ImageButton>(Resource.Id.btnDel);
WishlistView.ItemClick += buttonMax_ItemClick;
//WishlistView.ItemClick += buttonMin_ItemClick;
//WishlistView.ItemClick += buttonDel_ItemClick;
return view;
}
void buttonMax_ItemClick(object sender,AdapterView.ItemClickEventArgs e)
{
}
void buttonMin_ItemClick(object sender, AdapterView.ItemClickEventArgs x)
{
}
void buttonDel_ItemClick(object sender, AdapterView.ItemClickEventArgs z)
{
}
Also indicate that I have a class with the variable List<EN_WishDetalle> in public to be able to store the information in memory and avoid using the database.
public static List<EN_WishDetalle> WishDetalle = new List<EN_WishDetalle>();
enter image description here
enter image description here
Because of ListView's recycling mechanism, you can use ViewHolder and button Tag to achieve this.
Based on your code, I created a simple demo, it works on my side for Button buttonDel.
You can refer to the following code:
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView; // re-use an existing view, if one is available
MyViewHolder holder;
if (view != null)
{
holder = view.Tag as MyViewHolder;
holder.buttonDel.Tag = position;
//holder.buttonMax.Tag = position;
//holder.buttonMin.Tag = position;
}
else
{ // otherwise create a new one
holder = new MyViewHolder();
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
view = inflater.Inflate(Resource.Layout.CustomView, null);
holder.HeadingTextView = view.FindViewById<TextView>(Resource.Id.Text1);
holder.SubHeadingTextView = view.FindViewById<TextView>(Resource.Id.Text2);
holder.IconImage = view.FindViewById<ImageView>(Resource.Id.Image);
holder.buttonMax = view.FindViewById<Button>(Resource.Id.btnMax);
holder.buttonMin = view.FindViewById<Button>(Resource.Id.btnMin);
holder.buttonDel = view.FindViewById<ImageButton>(Resource.Id.btnDel);
holder.buttonMax.Click += delegate
{
};
holder.buttonMin.Click += delegate
{
};
holder.buttonDel.Click += delegate
{ // we get the tag here for buttonDel
int position = (int)holder.buttonDel.Tag;
Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(context);
AlertDialog alert = dialog.Create();
alert.SetTitle("GumisaAPP");
alert.SetMessage("Eliminar item : (" + position.ToString() + ") - " + items[position].Heading + items[position].SubHeading);
alert.SetIcon(Resource.Drawable.love);
alert.SetButton("OK", (c, ev) =>
{
items.RemoveAt(position);
NotifyDataSetChanged();
});
alert.SetButton2("CANCEL", (c, ev) =>
{
});
alert.Show();
};
holder.buttonDel.Tag = position;
view.Tag = holder;
}
holder.HeadingTextView.Text = item.Heading;
holder.SubHeadingTextView.Text = item.SubHeading;
holder.IconImage.SetImageResource(item.ImageResourceId);
return view;
}
public class MyViewHolder : Java.Lang.Object
{
public TextView HeadingTextView { get; set; }
public TextView SubHeadingTextView { get; set; }
public ImageView IconImage { get; set; }
public Button buttonMax { get; set; }
public Button buttonMin { get; set; }
public ImageButton buttonDel { get; set; }
}

Android - Regular repetition problem in custom listview in getView

I'm pulling data into Custom Listview from the mysql database, but as follows, I have a custom button. The button is either black or red, according to the records in the database. For example, there are 30 yields in my database and I pull the data with only 2 red and 28 black. Let's say the data in red is my data number 3 and data number 5. When I start to scroll down; my data number 3 and 5 turn red, then 8 data then the yield number 13 and 15, 23 and 25 turns red .. so there is a repetition .. I can't prevent it .. Is there a solution to this? Can you give support with sample code? My Custom Adapter code is as follows:
public class CostumAdaptor extends ArrayAdapter<Ses> {
private ArrayList<Ses> list;
private ViewHolderr vHolder;
boolean flag = false;
public CostumAdaptor(Context context, int resource, ArrayList<Ses> list) {
super(context, resource, list);
list = new ArrayList<Ses>();
list.addAll(list);
}
public int getCount() {
return list.size();
}
public Ses getItem(int position) {
return list.get(position);
}
public long getItemId(int position) {
return list.get(position).hashCode();
}
public View getView(int position, View conView, ViewGroup parent) {
View vi = conView;
if (vi == null) {
vHolder = new ViewHolderr();
vi = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.tiyatro_item, null);
vHolder.tiyatro_adi = vi.findViewById(R.id.tiyatro_adi);
vHolder.begeni_sayisi2 = (TextView) vi.findViewById(R.id.begeni_sayisi2);
vHolder.daha_sonra_dinleyecegim_2 = (TextView) vi.findViewById(R.id.daha_sonra_dinleyecegim_2);
vHolder.begenmedim_2 = (TextView) vi.findViewById(R.id.begenmedim_2);
vHolder.tiyatro = vi.findViewById(R.id.tiyatro);
vHolder.tur = vi.findViewById(R.id.tur);
vHolder.mensei = vi.findViewById(R.id.mensei);
vHolder.yapimci = vi.findViewById(R.id.yapimci);
vHolder.songCoverImage = vi.findViewById(R.id.coverImage);
vHolder.begendim = vi.findViewById(R.id.begendim);
vHolder.begenmedim = vi.findViewById(R.id.begenmedim);
vHolder.daha_sonra_dinleyecegim = vi.findViewById(R.id.daha_sonra_dinleyecegim);
vi.setTag(vHolder);
}
else {
vHolder = (ViewHolderr) vi.getTag();
}
Ses dmodel = (Ses) list.get(position);
String t_tiyatro_adi = dmodel.getTiyatro_adi();
vHolder.tiyatro_adi.setTag(dmodel);
vHolder.tiyatro.setTag(dmodel);
vHolder.tur.setTag(dmodel);
vHolder.mensei.setTag(dmodel);
vHolder.yapimci.setTag(dmodel);
vHolder.begendim.setTag(dmodel);
vHolder.tiyatro_adi.setText(dmodel.getTiyatro_adi());
vHolder.tiyatro.setText(dmodel.getTiyatro());
vHolder.tur.setText(dmodel.getTur());
vHolder.mensei.setText(dmodel.getMensei());
vHolder.yapimci.setText(dmodel.getYapimci());
String CACHE = t_tiyatro_adi;
new Handler().postDelayed(new Runnable() {
public void run() {
for (int i = 0; i < List1.size(); i++) {
for (int ii = 0; ii < List2.size(); ii++) {
if (List1.get(i).equals(List2.get(ii))) {
if (List1.get(i).equals(CACHE)) {
flag = true;
vHolder.begendim.setImageResource(R.drawable.begendim_dolu);
}
}
}
}
if (!flag) {
vHolder.begendim.setImageResource(R.drawable.begendim_bos);
}
}
}, 250);
return vi;
}
private class ViewHolderr {
TextView tiyatro_adi, tiyatro, tur, mensei, yapimci, begeni_sayisi2, daha_sonra_dinleyecegim_2, begenmedim_2;
ImageView songCoverImage;
ImageView begendim;
}
}
Note: List1 and List2 are ArrayLists pulled from the mysql database and compared with a for loop. The color of the button is red or black as a result of the comparison.

ArrayAdapter strange behaviour with id when items's height exceed Listview height

I'm facing a strange behaviour using an ArrayAdapter.
When the number of listview item exceed the height of the listView (say after item 8), the next item get the id 0 instead the id 9.
In my opinion this type of issue was explained here with the convertView, but i use it in the same way (i think).
The following code is my ArrayAdapter.
public class StepsAdapter extends ArrayAdapter<String> {
Context context;
List<String> steps;
public StepsAdapter(Context context, int resourceId, List<String> steps) {
super(context, resourceId, steps);
this.context = context;
}
private class ViewHolder {
EditText stepValue;
ImageView removeStep;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final String step = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_step, null);
holder = new ViewHolder();
holder.stepValue = (EditText) convertView.findViewById(R.id.stepEdit);
holder.removeStep = (ImageView) convertView.findViewById(R.id.removeStep);
holder.stepValue.setText(step);
holder.removeStep.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,"* Remove id step " + position, Toast.LENGTH_LONG).show();
steps.remove(position);
notifyDataSetChanged();
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
Then my main activity where i get existing data and put it in my listView, the add button and the save button.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_game);
mContext = getApplicationContext();
steps = new ArrayList<String>();
stepsAdapter = new StepsAdapter(mContext,R.layout.row_step,steps);
Gson gson = new GsonBuilder().create();
game = gson.fromJson(gameJson, Games.class);
/*
* Settings values
*/
gameNameValue.setText(game.getName());
gameBackgroundPreview.setBackgroundColor(game.getColor());
colorSelected = game.getColor();
for(int i = 0; i < game.getSteps().size() ; i++){
//steps.add(game.getSteps().get(i).toString());
//notifyDataSetChanged();
stepsAdapter.add(game.getSteps().get(i).toString());
}
final ListView listSteps = (ListView) findViewById(R.id.listViewSteps);
listSteps.setAdapter(stepsAdapter);
gameNameValue.setText(gameName);
addSteps.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stepsId = steps.size();
Toast.makeText(getApplicationContext(), "addSteps : " + stepsId, Toast.LENGTH_LONG).show();
stepsAdapter.insert("newstep", stepsId);
}
});
buttonSaveGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String valueEditGameName = gameNameValue.getText().toString();
int valueColorBackaground = colorSelected;
String picture = "testPic";
for(int i=0; i < listSteps.getChildCount(); i++) {
LinearLayout rowLayout = (LinearLayout) listSteps.getChildAt(i);
//Log.e(TAG, ">> :) layout >>" + listSteps.getChildAt(i).getClass().getName());
EditText editRow = (EditText) rowLayout.getChildAt(0);
stepsValues.add(editRow.getText().toString());
//Log.e(TAG, ">> :) inside layout >>" + editRow.getText().toString());
}
if(valueEditGameName.trim().length() > 0 && picture.trim().length() >0 ){
Games game = new Games(valueEditGameName,valueColorBackaground,picture,stepsValues);
String goToSave = game.createJson();
Log.e(TAG, ">>Saved>>" + goToSave);
final CkdFile file = new CkdFile();
String saved = file.writeToSDFile(game.getName(), goToSave);
Toast.makeText(mContext, saved, Toast.LENGTH_LONG).show();
Intent backToMain = new Intent(mContext,MainActivity.class);
startActivity(backToMain);
} else {
Toast.makeText(mContext, "Fill all texts", Toast.LENGTH_LONG).show();
}
}
});
}
I try to add items in 2 different ways :
add item through : List steps
add item through : StepsAdapter stepsAdapter
Both give me same behaviour.
If someone has a clue to help understanding what i'm doing wrong with my implementation of ListView/ArrayAdapter.
Thanks in advance !
EDIT 1 :
After pushing some logs everywere, it understand the strange behaviour :
My adapter have only 6 slots (the limit came from the size of the listview in layout), and when my arraylist have more than 6 items, the getView select items only between 0 and 5.
I'm searching now a way to get the position in ArrayList and not the position in arrayadapter.
I faced same issue recently. Add following overrides to Adapter:
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
I found a simple xml "trick" to avoid this behaviour : i set a biger height to listView.
<ListView
android:layout_width="match_parent"
android:layout_height="1000dp"
android:layout_gravity="center_horizontal"
android:id="#+id/listViewSteps"
android:layout_margin="10dp">
</ListView>
It's not really resolve but a take it ...

ListView not refreshed until user interaction

I have a ListView and a custom adapter. When the user changes the sort type from a spinner, I want to redraw the list with the new items. The problem is that the ListView is re-drawn only after I interact with it on the device (start scrolling for example).
if (update) {
mOfferList = (ArrayList<PositionSearchItem>) psr
.getPositionSearchItem();
mAdapter.clear();
mAdapter.addAll(mOfferList);
mAdapter.notifydataSetChanged()
mList.invalidate();
mList.invalidateViews();
}
I have also tried to fully reset a new instance of my adapter to the list, it is giving the same result except that the list becomes blank at first and appears on user interaction.
I'm using HoloEveryWhere maybe this is an important info.
EDIT : Here is some more code
#EFragment(R.layout.list_fragment_results)
public class ResultsListFragment extends Fragment {
#ViewById(R.id.search_results_list)
ListView mList;
#ViewById(R.id.search_result_list_spinner)
Spinner mSpinner;
MainActivity mActivity;
private String mTypedText;
private List<String> mSortType;
private ResultsListFragment mFrag;
private Bundle mOffersPageBundle;
private SearchResultsListAdapter mAdapter;
private ArrayList<PositionSearchItem> mOfferList;
#AfterViews
public void afterViews() {
mFrag = this;
mActivity = (MainActivity) getActivity();
mSortType = new ArrayList<String>();
mSortType.add(SearchResultSortType.SCORE_DESCENDING);
mOfferList = new ArrayList<PositionSearchItem>();
mOffersPageBundle = getArguments();
PositionSearchResponse psr = mOffersPageBundle.getParcelable("offers");
mTypedText = mOffersPageBundle.getString("typedText");
mOfferList = (ArrayList<PositionSearchItem>) psr.getPositionSearchItem();
mAdapter = new SearchResultsListAdapter(this,
android.R.layout.simple_list_item_1, mOfferList, mTypedText,
mSortType, mActivity.getLat(), mActivity.getLon());
mList.setAdapter(mAdapter);
this.setListeners();
}
private void setListeners() {
mSpinner.setOnItemSelectedListener(new org.holoeverywhere.widget.AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(
org.holoeverywhere.widget.AdapterView<?> parent, View view,
int pos, long id) {
Boolean update = false;
PositionSearchResponse psr = null;
switch (pos) {
case 0:
if (mSortType.get(0) != SearchResultSortType.SCORE_DESCENDING) {
mSortType.clear();
mSortType.add(SearchResultSortType.SCORE_DESCENDING);
psr = processSearch(mTypedText, mSortType, 0, mActivity);
update = true;
}
break;
case 1:
if (mSortType.get(0) != SearchResultSortType.DATE) {
mSortType.clear();
mSortType.add(SearchResultSortType.DATE);
psr = processSearch(mTypedText, mSortType, 0, mActivity);
update = true;
}
break;
default:
break;
}
if (update) {
mOffersPageBundle.putParcelable("offers", psr);
mOfferList = (ArrayList<PositionSearchItem>) psr
.getPositionSearchItem();
mAdapter.mIdMap.clear();
for (int i = 0; i < mOfferList.size(); ++i) {
mAdapter.mIdMap.put(mOfferList.get(i), i);
}
mOfferList = (ArrayList<PositionSearchItem>) psr
.getPositionSearchItem();
mAdapter.clear();
mAdapter.addAll(mOfferList);
mAdapter.notifydataSetChanged()
mList.invalidate();
mList.invalidateViews();
}
}
#Override
public void onNothingSelected(
org.holoeverywhere.widget.AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}
EDIT : the getView method from custom adapter
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
this.v = convertView;
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
PositionSearchItem psr = getItem(pos);
v = vi.inflate(R.layout.result_list_item, parent, false);
v.setTag(pos);
// Inject text into view
((TextView) v.findViewById(R.id.result_title_textview))
.setText(getItem(pos).getTitle());
// Disable hardware acceleration for the view (it brakes dotted lines on
// some devices)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
v.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
// reload list if scrolled to bottom
int page = 0;
if (pos > mLastViewed && pos == getCount() - 1) {
mLastViewed = pos;
ResultsListFragment frag = new ResultsListFragment_();
PositionSearchResponse newSearch = frag.processSearch(mTypedText, mSortType, mPage,
mActivity);
mPage = mPage + 12;
if(newSearch != null && newSearch.getPositionSearchItem() != null && newSearch.getPositionSearchItem().size() > 0){
for (int i = 0; i < newSearch.getPositionSearchItem().size(); ++i) {
mIdMap.put(newSearch.getPositionSearchItem().get(i), i);
}
this.addAll(newSearch.getPositionSearchItem());
}
}
return v;
}
Use
if (update) {
mAdapter.NotifyDataSetChanged();
}
this may solve your problem.
Try to set the adatpter again.Place the below code in all the places you wrote the code to update listview
mAdapter = new YourAdapter();
mList.setAdapet(mAdapter)
I hope you have created custom list view using adapter and model.
Now each time when you need to update your list view dynamically then you need to clear your previously created list view. Then after you need to fetch all new values and then update same over custom list view.
Hope it helps!

Android ListView OnItemClick event fires sometimes, but not always

I have a very wierd problem with the Android ListView component.
Situation: I'm basically utilizing the setOnItemClickListener method and subsequently overriding the "onItemClick" method to implement a custom action when clicking on an item in the listview.
Problem: The onItemClick event fires as it should on an item tap, however having tapped a decent amount of items (20-30-40), suddenly one, and only one, of the items become un-tapable, meaning nothing happens when you tap it. Meanwhile, all the other items are still tapable.
I have overridden the ListView component to capture the "dispatchTouchEvent" (see code below), and that reveals that when an item suddenly becomes un-tapable, the dispatchTouchEvent is indeed still fired, with reasonable X and Y coordinates, but somehow the "onItemClick" event failes to fire.
public class ExtendedListView extends ListView
{
public ExtendedListView(Context context) {
super(context);
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
Log.v("dispatchTouchEvent", "click|X:" + ev.getX() + "|Y:" + ev.getY());
return super.dispatchTouchEvent(ev);
}
}
Method that initiates the ListView:
#SuppressWarnings("unchecked")
private void renderHomeScreenItemsList() {
HomeScreenItemAdapter m_adapter;
ArrayList<HomeScreenItem> lHomeScreenItems = null;
LayoutParams lpHomeScreenItems = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lpHomeScreenItems.leftMargin = Util.dipsToPx(0, this);
lpHomeScreenItems.topMargin = Util.dipsToPx(155, this);
lpHomeScreenItems.gravity = 3; //3 = LEFT. Some gravity is necessary.
lvHomeScreenItems = new ExtendedListView(this);
lvHomeScreenItems.setLayoutParams(lpHomeScreenItems);
ColorDrawable sage = new ColorDrawable(this.getResources().getColor(R.color.HomeScreenDividerColor));
lvHomeScreenItems.setDivider(sage);
lvHomeScreenItems.setDividerHeight(1);
lvHomeScreenItems.setCacheColorHint(Color.WHITE);
lvHomeScreenItems.setBackgroundColor(Color.WHITE);
lHomeScreenItems = new ArrayList<HomeScreenItem>();
Iterator<EnabledSection> ite = Global.EnabledSectionsVector.iterator();
while (ite.hasNext()) {
EnabledSection es = (EnabledSection) ite.next();
HomeScreenItem h1 = new HomeScreenItem();
h1.setItemText(es.FunctionTitle);
h1.setFunctionName(es.FunctionName);
int imageId = getResources().getIdentifier("homescreen_" + es.FunctionName.toLowerCase(), "drawable", "XXXXXXXXX");
h1.setItemImageResourceId(imageId);
lHomeScreenItems.add(h1);
}
HomeScreenItem hCopyright = new HomeScreenItem();
hCopyright.setItemText("copyright");
hCopyright.setItemImageResourceId(-1);
lHomeScreenItems.add(hCopyright);
m_adapter = new HomeScreenItemAdapter(this, R.layout.row, lHomeScreenItems);
lvHomeScreenItems.setAdapter(m_adapter);
lvHomeScreenItems.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v("onItemClick", "Click Detected");
HomeScreenItem hmi = (HomeScreenItem)parent.getItemAtPosition(position);
Boolean blnAcceptClick = true;
if (WrapperWebView == null)
blnAcceptClick = true;
else
if (WrapperWebView.getProgress() != 100)
blnAcceptClick = false;
if (hmi.getItemImageResourceId() != -1 && blnAcceptClick)
{
Global.IsLoadingWrapper = true;
vSelectedItem = view;
ImageView ivIcon = (ImageView)view.findViewById(R.id.rightarrow);
LinearLayout ll = (LinearLayout)view.findViewById(R.id.RowContainer);
TextView ll2 = (TextView)view.findViewById(R.id.toptext);
ll.setBackgroundResource(R.color.BlueActive);
ll2.setBackgroundResource(R.color.BlueActive);
ll2.setTextColor(Color.WHITE);
int[] intCoordinates = new int[2];
view.getLocationInWindow(intCoordinates);
lpLoadingSpinner.topMargin = intCoordinates[1] - Util.dipsToPx(10, HomeScreen.this);
ivLoadingSpinner.setVisibility(View.VISIBLE);
ivLoadingSpinner.bringToFront();
LoadingSpinnerAnimation.start();
ivIcon.setVisibility(View.GONE);
ivClickedItemIcon = ivIcon;
InitializeWebView(hmi.getFunctionName());
}
}
});
}
Any help would be greatly appeciated. The bug is confirmed to happen on multiple devices and on multiple android versions.
UPDATE: Problem fixed
I've managed to find the problem and fix it. Basically, the list of items contained all "normal" items (in my context), except for the bottom one, which was a "copyright" item with a link (in blue) that had to be clickable. To get this click effect working, I used:
tt.setMovementMethod(LinkMovementMethod.getInstance());
if the item being rendered was the copyright item.
However, I didn't counter that effect if the item being rendered was a "normal" item, so by only adding the line below when a normal item is rendered, the problem went away:
tt.setMovementMethod(null);
Full example code (with non-relevant code removed)
public class HomeScreenItemAdapter extends ArrayAdapter<HomeScreenItem> {
private ArrayList<HomeScreenItem> items;
public HomeScreenItemAdapter(Context context, int textViewResourceId, ArrayList<HomeScreenItem> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
HomeScreenItem o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
if (iv != null && tt != null)
{
if (position != (items.size() - 1))
{
tt.setMovementMethod(null);
}
else
{
//last element, so setup element to show copyright notice instead
tt.setMovementMethod(LinkMovementMethod.getInstance()); //this makes the link clickable
}
}
}
return v;
}
}

Categories

Resources