Androider Androider - 5 months ago 18
Android Question

Why does an Spinner disappear and reappear again on touch?

Using spinner in Android application. I set an adapter and populate it with data from database.

The problem is that each time I touch outside the spinner (say on an

EditText
) - the spinner's item layout disappears for a second and only then appears again.

Seems like the spinner refreshes each time.

I am filling the spinner only once in
onCreate()
method of the Activity:

List<Deal> listDeals = mDealDao.getLastActiveDeals();
if (listDeals != null) {
mAdapter = new SpinnerDealsAdapter(this, listDeals);
mSpinnerDeal.setAdapter(mAdapter);
mSpinnerDeal.setOnItemSelectedListener(this);
}


Here is the adapter:

public class SpinnerDealsAdapter extends BaseAdapter {

public static final String TAG = "SpinnerDealsAdapter";

private List<Deal> mItems;
private LayoutInflater mInflater;

public SpinnerDealsAdapter(Context context, List<Deal> listCompanies) {
this.setItems(listCompanies);
this.mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return (getItems() != null && !getItems().isEmpty()) ? getItems().size() : 0 ;
}

@Override
public Deal getItem(int position) {
return (getItems() != null && !getItems().isEmpty()) ? getItems().get(position) : null ;
}

@Override
public long getItemId(int position) {
return (getItems() != null && !getItems().isEmpty()) ? getItems().get(position).getId() : position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if(v == null) {
v = mInflater.inflate(R.layout.spinner_item_deal, parent, false);
holder = new ViewHolder();
holder.txtDealNumber = (TextView) v.findViewById(R.id.tv_deal_number);
holder.txtDealName = (TextView) v.findViewById(R.id.txt_client_name);
holder.txtProduct = (TextView) v.findViewById(R.id.txt_product);
v.setTag(holder);
}
else {
holder = (ViewHolder) v.getTag();
}

// fill row data
Deal currentItem = getItem(position);
if(currentItem != null) {
holder.txtDealNumber.setText(String.valueOf(currentItem.getId()));
holder.txtDealName.setText(currentItem.getClientName());
holder.txtProduct.setText(currentItem.getProduct());
}

return v;
}

public List<Deal> getItems() {
return mItems;
}

public void setItems(List<Deal> mItems) {
this.mItems = mItems;
}

class ViewHolder {
TextView txtDealNumber;
TextView txtDealName;
TextView txtProduct;
}
}


UPDATE:
Here is how I get data from database:

public List<Deal> getLastActiveDeals() {
List<Deal> listDeals = new ArrayList<Deal>();

Cursor cursor = mDatabase.query(DBHelper.TABLE_DEALS, mAllColumns,
DBHelper.COLUMN_DEAL_IS_COMPLETED_FLAG + " = ?",
new String[] { "0" }, null, null, DBHelper.COLUMN_DEAL_ID + " DESC");
if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Deal deal = cursorToDeal(cursor);
listDeals.add(deal);
cursor.moveToNext();
}

// closing the cursor
cursor.close();
}
return listDeals;


Activity layout:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aandroid="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin">


<android.support.design.widget.TextInputLayout
android:id="@+id/txt_contactor_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColorHint="@color/text_hint_color">


<AutoCompleteTextView
android:id="@+id/txt_contactor"
style="@style/EditTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:hint="@string/contractor"
android:inputType="text" />

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:id="@+id/txt_expense_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txt_contactor_layout"
android:textColorHint="@color/text_hint_color">


<AutoCompleteTextView
android:id="@+id/txt_expense"
style="@style/EditTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:hint="@string/expense"
android:inputType="numberDecimal" />


</android.support.design.widget.TextInputLayout>


<TextView
android:id="@+id/tv_active_deals_spinner_description"
style="@style/DescriptionTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txt_expense_layout"
android:layout_marginTop="15dp"
android:layout_marginBottom="@dimen/descriptions_vertical_margin"
android:text="@string/active_deals_spinner_description" />

<LinearLayout
android:id="@+id/ll_spinner_deals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_active_deals_spinner_description"
android:background="@drawable/bg_white_accent_stroke_round">


<Spinner
android:id="@+id/spinner_deals"
style="@style/EditTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:animateLayoutChanges="true"
android:prompt="@string/select_a_deal"
android:spinnerMode="dropdown" />
</LinearLayout>

<Button
android:id="@+id/btn_add_quick_expense"
style="@style/ButtonStyle"
android:layout_width="match_parent"
android:layout_height="@dimen/button_height"
android:layout_below="@+id/ll_spinner_deals"
android:layout_marginTop="12dp"
android:gravity="center"
android:onClick="onClick"
android:paddingBottom="2dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="2dp"
android:text="@string/add_new_expense" />

<TextView
android:id="@+id/tv_quick_add_expense_no_active_deals"
style="@style/DescriptionTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txt_expense_layout"
android:gravity="center"
android:text="@string/no_active_deals"
android:textColor="@color/accent"
android:visibility="gone" />

<Button
android:id="@+id/btn_add_quick_expense_add_deal"
style="@style/ButtonStyle"
android:layout_width="match_parent"
android:layout_height="@dimen/button_height"
android:layout_below="@+id/tv_quick_add_expense_no_active_deals"
android:layout_marginTop="15dp"
android:gravity="center"
android:onClick="onClick"
android:paddingBottom="2dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="2dp"
android:text="@string/add_new_deal"
android:visibility="gone" />
</RelativeLayout>

</ScrollView>


What is wrong?

Answer

Finally found the solution.
The problem was in the activity's layout.
I just removed android:animateLayoutChanges="true" from the spinner and lagging disappeared.