Android Change textView text from another method/class - android

After a click on button1, another layout and another class gets called.
Now I want to change the text of textView out of class 2 which results in an app crash with java.lang.NullPointerException
important parts of Class 1
public static TextView A;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
[button stuff in class 1]
setContentView(R.layout.raten);
final TextView A = (TextView) findViewById(R.id.A); //the textview I wanna chage
max = 10;
Easy easy = new Easy(); // the other class
easy.e();
[now the method in class 1 that should change the text]
public static void Tx(int i)
{
A.setText("adsfasdf");
}
[important parts of Class 2 ("Easy")]
public void e(){
System.out.println("called class easy");
int max = MainActivity.max;
System.out.println(max);
for (int i= 0; i<max; i++){
System.out.println("runde"+i);
MainActivity.Tx(i);
}
I know, some people already asked such questions but I didn't find a working solution. I already understood, that you can't access the UI things outside the UI thread and that the nullpointerexception appears, because he uses the "empty" public static TextView A; and not the final TextView A = (TextView) findViewById(R.id.A).
But how I can make it visible for the other methods?
Sorry if the post looks messed up but I didn't konw how to explain my situation in a better way

Rather than defining a new local variable A, just assign to the static.
change
final TextView A = (TextView) findViewById(R.id.A); //the textview I wanna chage
to
A = (TextView) findViewById(R.id.A); //the textview I wanna chage

remove static from both A and Tx
(of course your MainActivity must be created by the time you call easy.e())

Related

android- Appending textView objects to a LinearLayout by clicking on a button from a different page (multiple times)

I have two Activities. Activity 1 is designed to take in user input (EditText), and has a button that (if clicked) will go to activity 2. In activity 2, there is a LinearLayout and a button that will take you back to activity 1. I can currently add one textView (containing the user input from activity 1) to the LinearLayout in activity 2, but I would like to add several textView objects to the LinearLayout. When I try to add user input any time after the first, it simply replaces the textView object that held the information from the user input that was entered the first time.
From Activity 1 (AddExercise):
public class AddExercise extends AppCompatActivity {
private EditText name;
private EditText weight;
private EditText sets;
private EditText reps;
private String deets;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_exercise);
Button button = (Button) findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToAddWorkout();
}
});
}
private void goToAddWorkout() {
Intent intent = new Intent(this, AddWorkout.class);
name = (EditText) findViewById(R.id.name);
weight = (EditText) findViewById(R.id.weight);
sets = (EditText) findViewById(R.id.sets);
reps = (EditText) findViewById(R.id.reps);
deets = name.getText().toString() + "\n\t\tWeight: " + weight.getText().toString() + "\n\t\tSets: " + sets.getText().toString() + "\n\t\tReps: " + reps.getText().toString();
intent.putExtra("details", deets);
startActivity(intent);
}
}
From Activity 2 (AddWorkout):
public class AddWorkout extends AppCompactActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_workout);
LinearLayout vBox = (LinearLayout) findViewById(R.id.vBox);
Bundle extras = getIntent().getExtras();
if (extras != null) {
TextView tv = new TextView(this);
tv.setText(extras.getString("details").toString());
vBox.addView(tv);
}
Button button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToAddExercise();
}
});
}
}
You can try a public static List of String each time you can add your text to list and then On your second activity create text-view as per your list count. And add to your linear-layout.
For more click here....
So you want to be able to add multiple entries in your second activity, for every time you add one in the first. I would recommend a different and easier approach.
In your second activity, use a listview/recyclerview, instead of adding new textview. This has the added advantage that once you have added enough entries, scrolling won't be an issue.
Maintain a global list of entries, which you add the entries to. And populate the listview using this list.
you should try something like that as said by roshan-
((ArrayAdapter)listView.getAdapter()).insert(data_object, index);
((ArrayAdapter)listView.getAdapter()).notifyDataSetChanged();
use a shared preference for index... every time you come to second activity increment the index... on exit of app just clear the shared pref index variable.
Also you can add textview in your each list view item
There can be several ways to do it according to me, pick the one that suits your relevant application.
have a single activity and host two fragments. So you can share the data between the fragments using single activity. (Recommended way, I guess Fragments will ease the job for you.). Also you can store a local variable in Activity so that each time you start your application you can start it afresh, if its intended!
If not, you can use SharedPreferences. For each button click, add a string to the preference. When you add one more click, append the new data with a separator like ("|", "||").. So in Activity one write to the preference, in activity 2 read from the preference and display it as list view of dynamically create the linear layout and append it to the root layout.
Declare a static ArrayList in your Activity 1 and access it in activity 2. (Really bad way)

android: after async process get data back from onPostExecute to view OR pass view to onPostExecute

i am trying to add extra input fields on the fly to my view.
i first read json string from url with an async funct and map this dynamically to an object with a hasmap with GSON.
next i want to iterate the hashmap to create and add the input fields:
public class NewProductActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_product);
TextView view = (TextView) this.findViewById(android.R.id.content);// is this the correct way to get view?
new loadTemplateAttributes(view).execute();// android studio complains about passing view here
.. snip...
class loadTemplateAttributes extends AsyncTask<String, String, HashMap> {
HashMap mymap;
.. snip...
protected void onPostExecute(HashMap mymap) {
pDialog.dismiss();
Iterator it = mymap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
String name = (String) pair.getKey();
TemplateAttribureProperties t = (TemplateAttribureProperties) pair.getValue();
String type = t.getType();
System.out.println("Name=" + name + " type=" + type);
LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout2);
// add text view
TextView tv = new TextView(this); // 'this' is unknow here
tv.setText(name);
ll.addView(tv);
EditText et = new EditText(view); // 'view' is what i want to pass but dont know how
et.setText();
ll.addView(et);
it.remove();
}
problem is that 'this' is unknown inside onPostExecute function.
i read something about passing the view to the async function but to me it is unclear how to get the view in the firstplace and how to pass it after...
also a lot of options dont seem to work because they are deprecated or are unsafe because the might introduce memory leaks according to the comments.
really lost here.
I don't know what you are doing but You can use Context context; and TextView view; before onCreate() making it global and then you can callTextView tv = new TextView(context); in your method.
public class NewProductActivity extends Activity {
Context context=this;
private TextView view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_product);
view = (TextView) findViewById(android.R.id.content);
}
new loadTemplateAttributes(view).execute();// android studio complains about passing view here
you would have to add constructor to your asynctask, like that:
class loadTemplateAttributes extends AsyncTask<String, String, HashMap> {
View view;
public loadTemplateAttributes(View v) {
view = v;
}
TextView tv = new TextView(this); // 'this' is unknow here
from internal class in java, you refer to parent class using NewProductActivity.this syntax in your case.
EditText et = new EditText(view); // 'view' is what i want to pass but dont know how
et.setText();
you could use aproach with constructor as I described above, or refer directly to activity view: NewProductActivity.this.view. But you would have to make view a class field in your activity.
Advanced: making your activity an internal class instead of static, and also passing views to it, might cause reference leaks and also crashes in case you use view (inside AsyncTask) that was invalidated due to screen rotation. This is especially possible if your AsyncTask is doing network operations. To prevent it always make AsyncTasks static, also if you pass views or activities to them, then wrap those references in WeakReference<>

TabHost crashes when changing a TextView inside it

In short, I set up a tabhost, and since I want the tab's content to have dynamic TextView's (among other things), I try initializing the text and it crashes. I am unsure of why, but commenting out the code that sets the text in the TextView's stopped the crashing.
One reference towards fixing this mentioned using intents to set activities for the tab's content, but apparently this didn't actually fix the crashing, it somehow changed it and then that lead died out without him ever saying how he fixed it, and my attempt at it also failed.
package com.example.main;
//removed imports
#SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
private boolean atMainMenu;
TextView warframeText;
TextView primaryText;
TextView secondaryText;
TextView meleeText;
TextView sentinelText;
TextView sentinelWeaponText;
Warframe warframe;
PrimaryWeapon primary;
SecondaryWeapon secondary;
MeleeWeapon melee;
Sentinel sentinel;
Weapon sentinelWeapon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
TabHost mTabHost = getTabHost();
//TabHost
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Build").setContent(R.id.build));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Stats").setContent(R.id.stats));
TextView title1 = (TextView)mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
title1.setTextColor(Color.parseColor("#30A0F0"));
TextView title2 = (TextView)mTabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title);
title2.setTextColor(Color.parseColor("#30A0F0"));
mTabHost.setCurrentTab(0);
warframeText = (TextView)findViewById(R.id.warframe);
primaryText = (TextView)findViewById(R.id.primary);
secondaryText = (TextView)findViewById(R.id.secondary);
meleeText = (TextView)findViewById(R.id.melee);
sentinelText = (TextView)findViewById(R.id.sentinel);
sentinelWeaponText = (TextView)findViewById(R.id.sentinelWeapon);
//Change To First Time Setup
warframe = new Excalibur();
primary = new BratonMk1();
secondary = new Lato();
melee = new Skana();
setBuild(); //<--removing this fixed the crashing, the method is included after onCreate, it sets the TextView's text
atMainMenu = true;
}
public void setBuild() {
warframeText.setText(warframe.getName());
primaryText.setText(primary.getName());
secondaryText.setText(secondary.getName());
meleeText.setText(melee.getName());
sentinelText.setText(sentinel.getName());
sentinelWeaponText.setText(sentinelWeapon.getName());
}
}
So what I would like to know, if anyone has the answer, why does editing the Textviews contained in a tab layout cause the app to crash and how can I fix this? :/
(also the code I provided is quite shortened, but I believe it contains all the relevant parts to the problem)
You did not initialize sentinel and sentinelWeapon but you are trying to get name from that
sentinelText.setText(sentinel.getName());
sentinelWeaponText.setText(sentinelWeapon.getName());
It May be a cause to your app get crashed

How to clear and add new texts to a dynamically created TextView?

I have added a TextView dynamically in a loop. On a button click, I want to clear the existing text in the text view and set some other text to it. How can I do this?
This is my current code:
ArrayList<String> Cheif_ComplaintNew = new ArrayList<String>();
int cc_Arraylist_length = Cheif_ComplaintNew.size();
android.widget.TextView cc_new = new android.widget.TextView(getApplicationContext());
for(int i=0; i<cc_Arraylist_length; i++)
{
cc_new.setId(i);
cc_new.setText(Cheif_ComplaintNew.get(i));
cc_new.setTextColor(getResources().getColor(R.color.black));
cc_new.setTypeface(null,android.graphics.Typeface.ITALIC);
cc_new.setTextSize(14);
cc_linearNew.addView(cc_new);
System.out.println("id"+i);
}
On a button click, the list is cleared and new data is stored in it. I want to display the new data in the same text view by clearing the old one.
You can either add the text to the textbox when you are creating it or assign it a class variable when you create it and later on you can add text to it.
TextView dynamicTextView;
...
private void CreateNewTextView()
{
dynamicTextView = Your New Text View;
}
...
private void ChangeTheText()
{
dynamicTextView.SetText("new value");
}
if you have more than one TextView you can create a class level generic list of TextViews and add them to the list and call them later.
you can also create a map of all TextView so you can call them with their key as well.
I think you can set a tag to cc_new before add it to cc_linearNew, like this: cc_new.setTag(i). when button got click, you can find those TextView by cc_linearNew.findViewByTag(i) in loop, and set new data to them.
Depends on how many TextViews you need to add to the layout dynamically.
As per the code mentioned, no TextViews are added to the layout as: Cheif_ComplaintNew.size() would return "0" so your loop will not be executed.
If you have to add only one TextView, then I agree with Daniel's answer of having a class level TextView variable.
If its multiple TextViews and you know which ID to use then in your Activity you can always get that TextView by calling findViewById("ID_OF_THE_TEXTVIEW_NEEDED")
You can do this in many ways, some of them :
Store your just created ids in array. Then just get your views calling parentView.findViewById(arreyOfIds(0));
Bad for performance do not do this :) - remove all of just added views from your parentView and create them one more time.
To handle back click in Activity use :
#Override
public void onBackPressed()
{
clearTextView();
}
All of this will simple look like this :
private List<Integer> ids = new ArrayList<Integer>();
#Override
public void onBackPressed()
{
clearTextView();
}
private void clearTextView()
{
for(Integer id : ids)
{
TextView view = (TextView)findViewById(id);
view.setText("")
}
}
private void createTextViews()
{
ArrayList<String>Cheif_ComplaintNew = new ArrayList<String> ();
int cc_Arraylist_length=Cheif_ComplaintNew.size();
android.widget.TextView cc_new = new android.widget.TextView(getApplicationContext() );
for(int i=0; i<cc_Arraylist_length; i++)
{
ids.add(i)
cc_new.setId(i);
cc_new.setText(Cheif_ComplaintNew.get(i));
cc_new.setTextColor(getResources().getColor(R.color.black));
cc_new.setTypeface(null,android.graphics.Typeface.ITALIC);
cc_new.setTextSize(14);
cc_linearNew.addView(cc_new);
System.out.println("id"+i);
}
}

Clean declaring and using variables in Android

Every once in a while, entering in a new Activity requires updating values in let's say several TextViews. So let's say I got n Strings to write in n TextViews of the starting Activity.
Which one is the best approach to guarantee good performance and providing "clean code"?
Variant 1 (The way I actually apply):
I declare a single TextView variable "tempText" as global variable and assign the TextView to update to this variable (alternatively in a extra method).
Alternatively, a) is doing the whole procedere in the onCreate(), while b) is handle everything in a method called e.g. updateTextViews()
(...)
public class MyActivity extends Activity{
private TextView tempText;
public onCreate(Bundle icicle){
(...)
tempText = (TextView) findViewById(R.id.tv_1);
tempText.setText(string_1);
tempText = (TextView) findViewById(R.id.tv_2);
tempText.setText(string_2);
(...)
tempText = (TextView) findViewById(R.id.tv_n);
tempText.setText(string_n);
}
}
Variant 2 :
I declare a single TextView variable "tempText" as variable in the onCreate() or the respective method and assign the TextView to update to this variable.
The rest is in analogy to Variant 1.
(...)
public class MyActivity extends Activity{
public onCreate(Bundle icicle){
(...)
private TextView tempText;
tempText = (TextView) findViewById(R.id.tv_1);
tempText.setText(string_1);
tempText = (TextView) findViewById(R.id.tv_2);
tempText.setText(string_2);
(...)
tempText = (TextView) findViewById(R.id.tv_n);
tempText.setText(string_n);
}
}
Variant 3:
I declare a global TextView variable for every TextView to update. This, as far as I know, needs more space in the RAM, but I don't know about the impact to velocity. Also here, are there differences between handling it in the onCreate() (a)) or in a seperate method (b))?
(...)
public class MyActivity extends Activity{
private TextView tempText_1;
private TextView tempText_2;
(...)
private TextView tempText_n;
public onCreate(Bundle icicle){
(...)
tempText_1 = (TextView) findViewById(R.id.tv_1);
tempText_1.setText(string_1);
tempText_2 = (TextView) findViewById(R.id.tv_2);
tempText_2.setText(string_2);
(...)
tempText_n = (TextView) findViewById(R.id.tv_n);
tempText_n.setText(string_n);
}
}
Variant 4:
I declare a TextView variable for every TextView to update in the onCreate() or the respective method which handles this. The rest is in analogy to Variant 3?
(...)
public class MyActivity extends Activity{
public onCreate(Bundle icicle){
(...)
private TextView tempText_1;
private TextView tempText_2;
(...)
private TextView tempText_n;
tempText_1 = (TextView) findViewById(R.id.tv_1);
tempText_1.setText(string_1);
tempText_2 = (TextView) findViewById(R.id.tv_2);
tempText_2.setText(string_2);
(...)
tempText_n = (TextView) findViewById(R.id.tv_n);
tempText_n.setText(string_n);
}
}
Which one is the "best" method? Variants 1 and 2 provide reserving only one memory address in the RAM and use this, while according to Robert C. Martins "Clean Code" the variables are really ambiguous. Option 3 and 4 would be the exact opposite. But for the rest I'm not very conscious of other effects.
Personally, I use a Variant 3 if I need to access the TextViews again elsewhere in Activity (i.e. in onResume, which is where I usually put my static UI updates). If the setText is a one-shot thing, I do something like --
((TextView) findViewById( R.id.tv_1 ) ).setText( string_1 );
((TextView) findViewById( R.id.tv_2 ) ).setText( string_2 );
If there's any doubt that the view might not be there (i.e. when you're modifying layouts during dev), I'd use a wrapper such as --
private void safeSetText( int id, String caption ) {
TextView tv = (TextView) findViewById( id );
if( tv != null )
tv.setText( caption );
else
Log.d( "..... " );
}
And then in onCreate:
safeSetText( R.id.tv_1, string_1 );
safeSetText( R.id.tv_2, string_2 );
If you want to use the value through out the this activity, declare it as a member variable in the class as you normally do. There is nothing like you declare inside OnCreate, it is still a member variable. It is as good as declaring in the class body, it will improve the readability.
Also if you are declaring a member variable anywhere, it is going to occupy the same amount of ram. There is nothing you gain/loose there.
If you are sure that a variable is going to be used only locally, declare it locally, it will be allocated in stack instead of heap, and it will vanish as soon as you exit the block.
There is no such android optimized way of declaring variables as such. You can follow normal Object oriented/Java way of doing things.

Categories

Resources