2015-01-09 51 views
1

我已經閱讀了許多關於此的關注,但是我找不到解決方案或無法在我的案例中實現它。帶文本刷新的ANDROID警報對話框

我有建築物列表,當我選擇列表中的元素時,對話框顯示,並且其他線程正在啓​​動,它連接到外部數據庫。

我的問題是: 我無法刷新我的功能對話框運行在其他線程上。文本將顯示正在獲取的數據庫元素。

我試過我自己的Dialog類,但還是什麼都沒有。

public class BuildingListActivity extends ListActivity { 

private WifiManager wifiManager; 
static String[] buildingsName = null; 
SqLiteDatabaseCRUD database; 
List<Budynek> budynkiExternal = new ArrayList<Budynek>();; 
List<Budynek> budynkiInternal = new ArrayList<Budynek>();; 
CustomArrayAdapter adapter; 
AlertDialog alertDialog; 
TextView dialogText; 
ListView listView; 
View v; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_building_list); 
    v = getLayoutInflater().inflate(R.layout.dialog, null); 
    dialogText = (TextView)v.findViewById(R.id.textView); 
    listView = (ListView)findViewById(android.R.id.list); 

    ... 


    getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 


      alertDialog = new AlertDialog.Builder(BuildingListActivity.this).create(); 
      alertDialog.setTitle("Alert Dialog"); 
      alertDialog.setMessage("Welcome to AndroidHive.info"); 


      alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // Write your code here to execute after dialog closed 
        Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show(); 
       } 
      }); 
      alertDialog.show(); 

      Thread getData = new Thread(getDatas, "external_database"); 
      getData.start(); 
     } 

    }); 
} 

private Runnable getDatas = new Runnable() 
{ 
    @Override 
    public void run() 
    { 
     SqLiteDatabaseCRUD database = new SqLiteDatabaseCRUD(getApplicationContext()); 
     List<Integer> idList = new ArrayList<Integer>(); 
     idList = adapter.getIdList(); 
     ExternalDatabase externalConnection = new ExternalDatabase(); 

     List<Pomieszczenie> pomieszczenieList; 
     List<DaneSurowe> daneList; 
     List<AP> routerList; 
     for(int i = 0; i < idList.size(); i++) 
     { 
      database.createFieldBudynek(externalConnection.getBudynek(idList.get(i))); 
      pomieszczenieList = externalConnection.getPomieszczenieByBudynek(idList.get(i)); 
      daneList = externalConnection.getDaneSurowe(idList.get(i)); 
      routerList = externalConnection.getAPbyPomieszczenie(idList.get(i)); 

      for(int j = 0; j < pomieszczenieList.size(); j++) 
      { 
       database.createFieldPomieszczenie(pomieszczenieList.get(j)); 
      } 
      for(int j = 0; j < daneList.size(); j++) 
      { 
       database.createFieldDaneSurowe(daneList.get(j)); 
      } 
      for(int j = 0; j < routerList.size(); j++) 
      { 
       database.createFieldAP(routerList.get(j)); 
      } 
     } 
    } 
}; 

我已經試過這樣:

public class DatabaseDialog extends Dialog { 
View v = null; 

public DatabaseDialog(Context context) { 
    super(context); 
} 

public Dialog show(Context context) { 
    Dialog d = new Dialog(context); 
    v = LayoutInflater.from(context).inflate(R.layout.dialog, null); 
    d.setContentView(v); 
    return d; 
} 

public void update() { 
    v.invalidate(); 
    // v.setId(R.id.textView); 
} 
} 

而且使用它像

  Dialog testDialog = new Dialog(getApplicationContext()); 
      DatabaseDialog dialog = new DatabaseDialog(getApplicationContext()); 
      testDialog = dialog.show(getApplicationContext()); 

      testDialog.show(); 

,但它說明不了什麼。

dialog.xml 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="Medium Text" 
    android:id="@+id/textView" 
    android:layout_gravity="center_horizontal" /> 
</LinearLayout> 



activity_building_list 
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="com.example.arkadio.naviwifi.BuildingListActivity"> 
<ListView 
    android:id="@android:id/list" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 
</RelativeLayout> 

感謝您的幫助!

回答

0

對於更新GUI,您需要在主線程上運行該代碼或更新將不會執行。在您要更新GUI的位置的線程內使用此模式。

activity.runOnUiThread(
    new Runnable(){ 
     @Override 
     public void run(){ 
      // HERE UPDATE GUI 
     } 
    } 
); 

也不錯的例子here

或者只是使用AsyncTask。示例here

+0

我不是說這是個好主意。爲了在UI線程中進行數據庫訪問可以阻塞主線程,甚至會拋出一個無響應的消息。 –

+0

只能在ui線程上運行,而不是在訪問數據庫時更新GUI。在線程中使用此示例(創建新的可運行只是爲了更新GUI)。 – Dejan

+0

或使用AsyncTask,它可以在後臺對你進行線程化處理。 – Dejan