Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to correctly use TextSwitcher in ListView?
    primarykey
    data
    text
    <p>My <code>TextSwitcher</code> for each record in <code>ListView</code> should display first value (<em>text1</em>) and then another value (<em>text2</em>), then first value again and so on. It should happen only if <em>text2</em> not empty. Otherwise <em>text1</em> should be always shown (without any changes and animation).</p> <p>I've created <code>Runnable()</code>, which changes boolean variable (<em>time2</em>) to then call <code>items.notifyDataSetChanged()</code>. It works as expected and in result <code>setViewValue()</code> for my <code>ListView</code> is called.</p> <p>Here is the code:</p> <pre><code>items.setViewBinder(new SimpleCursorAdapter.ViewBinder() { @Override public boolean setViewValue(View view, Cursor cursor, int columnIndex) { int viewId = view.getId(); switch(viewId) { case R.id.timetext: TextSwitcher itemTime = (TextSwitcher) view; if (itemTime.getChildCount() != 2) { itemTime.removeAllViews(); itemTime.setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { TextView t = new TextView(MyActivity.this); t.setTextSize(18); t.setTypeface(null, Typeface.BOLD); t.setTextColor(Color.WHITE); return t; } }); itemTime.setAnimateFirstView(true); itemTime.setInAnimation(AnimationUtils.loadAnimation(MyActivity.this, R.anim.push_up_in)); itemTime.setOutAnimation(AnimationUtils.loadAnimation(MyActivity.this, R.anim.push_up_out)); } if (!text2.equals("")) { if (!time2) { itemTime.setText(text1); } else { itemTime.setText(text2); } } else { itemTime.setCurrentText(text1); } return true; } return false; } } ); </code></pre> <p>It works almost as expected. With one minor item - when <em>text2</em> should be shown, it changes displayed value to some other value first (from another record!) and then animation is played. Change of <em>text2</em> to <em>text1</em> happens correctly.</p> <p>My understanding that the reason is the following - before displaying <em>text2</em>, all views of <code>itemTime</code> are removed and hence it is recreated and that is why some other value is shown for a second. But why does it show value from some other record?</p> <p>Actually <em>text2</em> and <em>text1</em> are values from the database, for ex. <code>text2 = cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_TIME_2))</code>, probably, something is wrong here and <code>setViewValue</code> called with wrong parameters?</p> <p>Upd. <em>text1</em> and <em>text2</em> are read from the database at <code>setViewValue</code>. Here is example of the full code: </p> <pre><code>itemTime.setText(cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_CLOSE_TIME_1)) + " - " + cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_OPEN_TIME_1))); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload