带有文本刷新的 ANDROID 警报对话框

标签 android refresh android-alertdialog

我已经阅读了很多关于此的文章,但我找不到解决方案或无法在我的案例中实现。

我有建筑物列表,当我选择列表中的元素时,对话框会显示,其他线程正在启​​动,它连接到外部数据库。

我的问题是: 我无法在其他线程的 run 函数中刷新我的对话框。文本将显示正在获取的数据库元素。

我已经尝试了我自己的 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>

感谢您的帮助!

最佳答案

要更新 GUI,您需要在主线程上运行该代码,否则不会执行更新。在更新 GUI 的地方的线程中使用此模式。

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

也是很好的例子 here .

或者只使用 AsyncTask。示例 here .

关于带有文本刷新的 ANDROID 警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27862919/

相关文章:

android - 如何在android中截取应用程序屏幕的屏幕截图?

android - 如何在按钮上赋予无限波纹效果

php - PHP/MYsql 中的重复问题

jquery - 刷新页面或执行大量 JQuery?

android - 创建 AlertDialog 时出错

android - 确定离线 skmap mapcut 的边界

ember.js - EmberJS,轮询,更新路由模型,重新渲染组件

java - android.content.res.Resources$NotFoundException : String resource ID #0x42

android - 如何以编程方式关闭 Android Material 警报对话框?

java - AsyncTask onPostExecute 对话框不起作用