I.Goel
I.Goel

Reputation: 3

EditText Lag on Activity Change

I am using Text from an EditText to get some items in a listview using t a custom adapter from a mysql database. On the click of a button next to the EditText , I am starting another activity using intent. The EditText works fine when th app first starts and opens to MainActivity but the cursor remains stagnant for 1-2 seconds when MainActivity is loaded from another acivity.The code is as follows:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="nf.co.riaah.chutte.MainActivity"
android:orientation="vertical"
android:background="#EEEEEE"
android:id="@+id/linear_main">
<android.support.v7.widget.Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:theme= "@style/ToolbarColoredBackArrow"
    android:id="@+id/app_bar"
    android:minHeight="?attr/actionBarSize"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:background="?attr/colorPrimary"
    android:contentInsetLeft="0dp"
    android:contentInsetStart="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetEnd="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="1dp"
    >
    <ImageView
        android:layout_width="150dp"
        android:layout_height="30dp"
        android:src="@drawable/title"
        android:id="@+id/title_image"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/oie_transparent"
        android:layout_alignParentRight="true"
        android:id="@+id/cart_icon"
        android:layout_toRightOf="@id/title_image"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        />
</RelativeLayout>
</android.support.v7.widget.Toolbar>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:orientation="vertical">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/search_area"
    android:padding="5dp">
<EditText
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:hint="Type Your Text Here"
    android:id="@+id/gettext"
    android:layout_weight="1"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:text="Search"
    android:layout_alignParentRight="true"
    android:background="@drawable/circle"
    android:textColor="#FFFFFF"
    android:onClick="search_product"
    />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:padding="5dp"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/circle"
            android:layout_margin="10dp"
            android:orientation="vertical"

            >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:text="\?"
            android:textColor="#FFFFFF"
            android:textAlignment="center"
            android:textSize="125dp">
        </TextView>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:textColor="#FFFFFF"
                android:textAlignment="center"
                android:textSize="25dp"
                android:text="About Us">
            </TextView>
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/circle"
            android:layout_margin="10dp"
            android:orientation="vertical">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:id="@+id/re"
                android:src="@drawable/home_clipart_edited"
                android:layout_weight="1"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:textColor="#FFFFFF"
                android:textAlignment="center"
                android:textSize="25dp"
                android:text="Nearby Shops"/>

        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/circle"
            android:layout_margin="10dp"
            android:orientation="vertical">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:src="@drawable/money_clipart_edited"
                android:layout_weight="1"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="5dp"
                android:textColor="#FFFFFF"
                android:textAlignment="center"
                android:textSize="25dp"
                android:text="Donate"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/circle"
            android:layout_margin="10dp"
            android:orientation="vertical"
            android:onClick="goto_list">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="5dp"
                android:textColor="#FFFFFF"
                android:textAlignment="center"
                android:textSize="25dp"
                android:text="Click Here for latest"/>

        </LinearLayout>
    </LinearLayout>
</LinearLayout>
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
private EditText editText;
public static String pname;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // an example activity_main.xml is provided below
    Toolbar tb = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(tb);
    editText = (EditText) findViewById(R.id.gettext);
}

public void goto_list(View view) {
    Intent intent = new Intent(this, Search_multiple.class);
    startActivity(intent);
}

public void search_product(View view) {
    Intent intent = new Intent(this, Search_multiple.class);
    pname = editText.getText().toString();
    startActivity(intent);
}
}

Upvotes: 0

Views: 91

Answers (1)

Movsar Bekaev
Movsar Bekaev

Reputation: 908

Use asynchronous threads when working with databases, your cursor stops because the second intent starts time consuming operation i.e. fething data from the mysql, on the main thread.

Upvotes: 1

Related Questions