blackst0ne
blackst0ne

Reputation: 3264

Issues Using Android LinearLayout

I have a LinearLayout, containing one EditText and one TextView. My ConsoleWindow is running in a loop, and I would like to update the TextView in each Iteration.

The problem is that I may initialize the EditText only once (otherwise it is inaccessible) and also the LinearLayout may only be initialized once (otherwise it would remove the EditText).

I cannot put the LinearLayout and the EditText in an if-statement:

if (firstRun) {
     // initialize LinearLayout and EditText
     firstRun = false;
}
// TEXTVIEW
TextView tv = new TextView(getApplicationContext())
tv.setText(dataStringTot);
layout.addView(tv); // "Qualifier must be an Expression."

because the IDE Returns at layout.addView(tv); : "Qualifier must be an expression."

My Code:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getInetContent();
}

getInetContent() { // would be a thread
// getting data...

Bundle b = new Bundle();
b.putString("StringNMEA", NMEA);
Message m = mhandler.obtainMessage();
m.setData(b);
mhandler.sendMessage(m);
}


Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        String dataString = "";
        Bundle bundle = msg.getData();

        if (bundle.containsKey("display")) {
        ConsoleWindow(dataString);
        }
    }
}

private void ConsoleWindow(String dataString) {

            // LINEAR LAYOUT
            LinearLayout layout = new LinearLayout(this);
            setContentView(layout);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.setBackgroundColor(Color.parseColor("#000000")); // black


            // EDITTEXT
            EditText et = new EditText(getApplicationContext());
            et.setHint("Enter Command");
            layout.addView(et);


            // TEXTVIEW
            TextView tv = new TextView(getApplicationContext());
            tv.setText(dataStringTot);
            layout.addView(tv);
            }
}

My Code with if-statement:

public boolean firstRun = true;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getInetContent();
}

getInetContent() { // would be a thread
// getting data...

Bundle b = new Bundle();
b.putString("StringNMEA", NMEA);
Message m = mhandler.obtainMessage();
m.setData(b);
mhandler.sendMessage(m);
}


Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        String dataString = "";
        Bundle bundle = msg.getData();

        if (bundle.containsKey("display")) {
        ConsoleWindow(dataString);
        }
    }
}

private void ConsoleWindow(String dataString) {
       if (firstRun) {
            // initialize LinearLayout and EditText:
            // LINEAR LAYOUT
            LinearLayout layout = new LinearLayout(this);
            setContentView(layout);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.setBackgroundColor(Color.parseColor("#000000")); // black


            // EDITTEXT
            EditText et = new EditText(getApplicationContext());
            et.setHint("Enter Command");
            layout.addView(et);
            firstRun = false;
        }


            // TEXTVIEW
            TextView tv = new TextView(getApplicationContext());
            tv.setText(dataStringTot);
            layout.addView(tv);
            }
}

How could I fix this Problem?

Upvotes: 1

Views: 973

Answers (1)

mlaHackingIsMagic
mlaHackingIsMagic

Reputation: 271

I imagine you want to update the colours/text of them each iteration.

You don't need to create a new View each time you want to modify it. Set up your layout, get the views and then pass those views into your loop.

LinearLayout layout = new LinearLayout(this);
EditText et = new EditText(this);
TextView tv = new TextView(this);
layout.addView(et);
layout.addView(tv);
setContentView(layout);

et.setHint("Enter Command");
tv.setText(dataStringTot);

startLoop(et, tv);

public void startLoop(final EditText et, final TextView tv) {
    ...
}

If your loop is a thread of some sort that isn't run on the main thread you might need to wrap it in a run on ui thread.

runOnUiThread(new Runnable() 
{
    public void run() 
    {
        Log.v("mainActivity", "test");
    }
});   

Upvotes: 1

Related Questions