0

i have problems with my own Dialog. I need a Dialog which switches his text more times during my activity. Because of the activity don't need an UI i thought it is a good idea to use the activity as dialog. I set the Theme as @android:style/Theme.Dialog"

This works fine. The Layout is the following.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Evaluation"
        android:id="@+id/textView_couchdbactivity_dialog_text"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

My Problem is I want to make something an change the text of this textview. For Example:

textview_couchdbactivity_dialog_text.setText("creating CouchDB");
    textview_couchdbactivity_dialog_text.invalidate();
    try {
        dataManager = new CouchDataManager("evaluationcouchdb", this);
    } catch (IOException e)
    {
        e.printStackTrace();
    } catch (CouchbaseLiteException e)
    {
        e.printStackTrace();
    }
    textview_couchdbactivity_dialog_text.setText("resetting CouchDB");
    textview_couchdbactivity_dialog_text.invalidate();
    dataManager.reset();

But the text don't changes. Only at the end of my Activity the text gets changed. How can I resolve this problem?

At the moment my activity looks exactly so(Without any Dialog):

package de.test.dbperformancemessung;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.couchbase.lite.CouchbaseLiteException;
import java.io.IOException;
import java.util.ArrayList;
import de.test.dbperformancemessung.Persistierung.Daten;
import de.test.dbperformancemessung.Persistierung.CouchDB.CouchDataManager;

public class MainActivity extends Activity implements    View.OnClickListener
{
    private Button button_mainactivity_start_evaluation;
    private Button button_mainactivity_show_graph;

    //Test
    private CouchDataManager dataManager;
    private ArrayList<Daten> list_data;
    private long[] durations = new long[5];
    private long duration = 0;
    private Context ctx;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button_mainactivity_start_evaluation = (Button) findViewById(R.id.button_mainactivity_start_evaluation);
        button_mainactivity_start_evaluation.setOnClickListener(this);
        button_mainactivity_show_graph  = (Button) findViewById(R.id.button_mainactivity_show_graph);
        button_mainactivity_show_graph.setOnClickListener(this);
        ctx = this.getApplicationContext();
    }   
    @Override
    public void onClick(View v)
    {
        Log.i("LOGTAG", "onClick(View v");
        Intent intent;
        switch (v.getId())
        {
            case R.id.button_mainactivity_start_evaluation:    intent = new Intent(getApplicationContext(), CouchDBActivity.class);
                Log.i("LOGTAG", "Show Graph");
                startActivity(intent);
                break;
            case R.id.button_mainactivity_show_graph:
                Log.i("LOGTAG", "Show Graph");
                init();
                break;
        }
    }
    public void init()
    {
        try {
            dataManager = new CouchDataManager("evaluationcouchdb", ctx);
        } catch (IOException e)
        {
            e.printStackTrace();
        } catch (CouchbaseLiteException e)
        {
            e.printStackTrace();
        }
        dataManager.reset();
        list_data = new ArrayList<Daten>();
        RandomData randomData = new RandomData();
        for(int i = 0; i < 10000; i++)
        {
            list_data.add(randomData.getRandomData());
        }
        insertFahrt1();
    }
    public void insertFahrt1()
    {
        Log.i("LOGTAG", "insert 1.000 Daten (Fahrt1)");
        ArrayList<Daten> list_smallinsert = new ArrayList<Daten>();
        for (int i = 0; i < 1000; i++)
        {
            list_smallinsert.add(list_data.get(i));
        }
        durations[0] = dataManager.insertDataInTable("Fahrt1", list_smallinsert, 1);
        list_smallinsert = null;
        Log.i("LOGTAG", "Fahrt1: " + durations[0]);
        insertFahrt2();
    }
    public void insertFahrt2()
    {
        Log.i("LOGTAG", "insert 10.000 Daten (Fahrt2)");
        durations[1] = dataManager.insertDataInTable("Fahrt2", list_data, 1);
        Log.i("LOGTAG", "Fahrt2: " + durations[1]);
        insertFahrt3();
    }
    public void insertFahrt3()
    {
        Log.i("LOGTAG", "insert 1.000.000 (Fahrt3)");
        duration = 0;
        for (int i = 0; i < 100; i++)
        {
            duration = duration + dataManager.insertDataInTable("Fahrt3", list_data, 1);
        }
        durations[2] = duration;
        Log.i("LOGTAG", "Fahrt3: " + durations[2]);
        insertFahrt4();
    }
    public void insertFahrt4()
    {
        Log.i("LOGTAG", "insert 5.000.000 Daten (Fahrt4)");
        duration = 0;
        for(int i = 0; i < 500; i++)
        {
            duration = duration + dataManager.insertDataInTable("Fahrt4", list_data, 1);
        }
        durations[3] = duration;
        Log.i("LOGTAG", "Fahrt4: " + durations[3]);
        insertFahrt5();
    }
    public void insertFahrt5()
    {
        Log.i("LOGTAG", "insert 10.000.000 Daten (Fahrt5)");
        duration = 0;
        for(int i = 0; i < 1000; i++)
        {
            duration = duration + dataManager.insertDataInTable("Fahrt5", list_data, 1);
        }
        durations[4] = duration;
        Log.i("LOGTAG", "Fahrt5: " + durations[4]);
        Log.i("LOGTAG", "All tests passed");
        dataManager.close();
    }
}

What I need is a Dialog or something like this which shows the first log of each method. So the user knows at this moment this is happening. Best it would be without using Threads. I tried to insert some stuff inside a thread, but this needs more time then without using a thread.

Mike
  • 421
  • 4
  • 10
  • You don't need to call `invalidate` manually. Your code says nothing. I suppose you block `UI thread` and your `TextView` doesn't update. – eleven Jan 26 '16 at 12:32
  • Want I make is insert some Data in a db. In this case it is a couch DB. Maybe this blocks the Ui Thread. I tried to make the insert in a thread befre a few days. But then it is slower then in the UI Thread. Because I make an performance test I want to have the best performance. Is there anyway to update the text without outting the insert in a searate thread? – Mike Jan 26 '16 at 12:43
  • I did not understand a thing: When do you want to update your TextView? By some event or by a timer? – Danilo Dughetti Jan 26 '16 at 13:41
  • I want to update the Textview when the insert is finished. So, by a event. – Mike Jan 26 '16 at 13:46
  • I tried to use a simple ProgressDialog, but this is visible after the insert is ready. – Mike Jan 26 '16 at 13:47
  • You can dig for the observer design pattern. http://www.tutorialspoint.com/design_pattern/observer_pattern.htm – Mohamed Jan 27 '16 at 15:20

2 Answers2

0

Use handler to refresh your textview value by this link below

How to set Timer when button clicked?

Thanks

Community
  • 1
  • 1
Androider
  • 3,833
  • 2
  • 14
  • 24
  • there is no button inside the dialog activity, so this is not usable for me – Mike Jan 26 '16 at 13:05
  • You can also use it without button, button is only just an example – Androider Jan 27 '16 at 05:27
  • But then I have to put the working code inside a runnable. I discovered that the insert is more slow inside a thread, inside a asynctask... I need a solution without putting the working code inside some of this. – Mike Jan 27 '16 at 10:47
0

Because I don't find another way I tried putting my working code into a IntentService. This works fine, during the Service is active the user see a ProgressDialog which gets updated from the activity threw a Broadcastreceiver. The Service sends Broadcasts to my Activity and then the Activity updates the progressdialog.

Mike
  • 421
  • 4
  • 10