2016-06-11 50 views
0

我正在使用TextTextText從一個自定義的適配器從一個MySQL數據庫中獲取列表視圖中的一些項目。在點擊EditText旁邊的按鈕時,我正在使用intent開始另一項活動。該EditText上正常工作時,日應用第一啓動並打開MainActivity但當MainActivity從另一個acivity.The代碼加載光標保持停滯1-2秒如下:EditText滯後活動變化

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); 
} 
} 

回答

1

使用異步線程處理數據庫時,光標停止,因爲第二個intent開始耗時操作,即在主線程上從mysql中獲取數據。

+0

我正在使用asynctask來訪問Search_multiple.class中的數據庫。我是否應該將其用於意圖呢? –

+1

@ I.Goel只是刪除第二個活動的oncreate(opause,onstart,onrestart)中的所有內容,並檢查光標是否停止,如果它不能通過線路檢查恢復您的代碼,那麼光標停止時的確切點在哪裏 –

+0

我只是想到,在觸摸EditText時,鍵盤沒有滯後,但光標停留大約0.5到1秒。這是正常還是我犯了一些其他錯誤。 –