#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Urgent! Changing Textview with button


    I have an assingment which i havent been able to finish even though i´ve put a lot of effort into this. my question is why cant i change the textviews content on button click? how could that be made possible? I apologize for posting all of it but basically ive created a lot of textviews and would like to be able to change the contents of them by button click!

    package com.example.projekt2;

    import java.util.ArrayList;
    import java.util.Random;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;

    public class NyttSpelActivity extends Activity {



    ArrayList<String> myArr = new ArrayList<String>();
    ArrayList<String> mållistan = new ArrayList<String>();
    TextView mål[];
    TextView pass[];



    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nytt_spel);



    }


    public void innehall() {

    HttpClient ny = new HttpClient();
    String namnet=ny.jou();

    myArr=ny.ding();

    TextView mål[] = new TextView[myArr.size()];
    TextView category[] = new TextView[myArr.size()];
    TextView pass[] = new TextView[myArr.size()];

    TextView namn = new TextView(this);
    namn.setText("name:");

    TextView måltext = new TextView(this);
    måltext.setText("goal:");

    TextView passtext = new TextView(this);
    passtext.setText("passes:");

    TextView inget = new TextView(this);


    Button knappar[] = new Button[myArr.size()];
    Button knappar2[] = new Button[myArr.size()];

    Button spara= new Button(this);
    Button nyspelare= new Button(this);

    spara.setText("save");
    spara.setTag("spara");
    spara.setOnClickListener(myButtonListener);


    nyspelare.setText("player");


    TableRow rad[]= new TableRow[myArr.size()];
    TableRow original[]=new TableRow[2];

    TableLayout tl = (TableLayout) findViewById(R.id.bord);


    original[0] = new TableRow(this);
    original[0].addView(nyspelare);
    original[0].addView(spara);

    original[1] = new TableRow(this);
    original[1].addView(namn);
    original[1].addView(måltext);
    original[1].addView(inget);
    original[1].addView(passtext);

    tl.addView(original[0]);
    tl.addView(original[1]);
    Random rand = new Random();
    int randomint;
    String randomtext;
    for(int i =0; i<myArr.size(); i++){
    randomint=rand.nextInt(5);
    randomtext=Integer.toString(randomint);
    category[i]=new TextView(this);
    category[i].setText(myArr.get(i));

    mål[i] = new TextView(this);
    mål[i].setText(randomtext);
    mållistan.add(mål[i].getText().toString());

    pass[i] = new TextView(this);

    pass[i].setText("1");

    knappar[i]=new Button(this);
    knappar[i].setText("+");
    if(i==0){
    knappar[0].setTag("prov");
    knappar[0].setOnClickListener(myButtonListener);
    }



    knappar2[i] = new Button(this);
    knappar2[i].setText("+");
    String istring= Integer.toString(i);
    knappar2[i].setTag(istring);



    rad[i]=new TableRow(this);

    rad[i].setBackgroundColor(042142);
    rad[i].addView(category[i]);
    rad[i].addView(mål[i]);
    rad[i].addView(knappar[i]);
    rad[i].addView(pass[i]);
    rad[i].addView(knappar2[i]);


    tl.addView(rad[i]);

    }




    }

    public void sparaskriv(View view)
    {




    String newNoteText="";
    for(int kk=0; kk < myArr.size(); kk++)
    {
    newNoteText=newNoteText + myArr.get(kk)+ " ";
    }

    Skrivare skrivare = new Skrivare();

    skrivare.saveNote(newNoteText, this);


    }

    public void sparamål(View view)
    {



    String intchar;
    String måltext="";
    for(int kk=0; kk < myArr.size(); kk++)
    {
    intchar=mållistan.get(kk).toString();
    måltext=måltext + intchar+ " ";
    }

    Skrivare skrivare = new Skrivare();
    skrivare.saveMål(måltext, this);

    }




    public void settext(){
    mål[2].setText("2");
    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_nytt_spel, menu);
    return true;
    }
    private OnClickListener myButtonListener = new OnClickListener() {
    public void onClick(View view) {

    String tag = (String) view.getTag();
    if (tag == "spara") {
    sparaskriv(view);
    sparamål(view);
    finish();
    }
    if (tag == "prov") {

    settext();
    }


    }
    };


    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    18
    Rep Power
    0
    So it's kind of hard to tell what's going on here with the way your code is but from what I see, you're never actually linking your text views to anything in your xml (or the button for that matter)

    so you want to have some line that tells your program what widget to deal with some something like

    myTextView = (TextView) findViewById(R.id.text_view_i_want);

    &

    myButton = (Button) findViewById(R.id.the_button_i_want);

IMN logo majestic logo threadwatch logo seochat tools logo