将静态字符串值传递给textview

时间:2017-04-15 02:51:13

标签: java android string

我将值存储在各种活动的静态字符串中。我试图通过这样做来展示这些:

TextView t = (TextView) findViewById(R.id.text_view);
t.setText(static_String);

当我在另一项活动中做同样的事情时,它起作用了。现在,文本视图没有显示任何内容。

以下是代码:

import static com.miniproject32.writeup.S12.scene0;
import static com.miniproject32.writeup.S16.scene1a;
import static com.miniproject32.writeup.S16.scene1b;
import static com.miniproject32.writeup.S16.scene1c;
import static com.miniproject32.writeup.S16.scene1d;
import static com.miniproject32.writeup.S16.scene1e;
import static com.miniproject32.writeup.S19.resolution;
import static com.miniproject32.writeup.S19.scene2a;
import static com.miniproject32.writeup.S19.scene2b;
import static com.miniproject32.writeup.S19.scene2c;
import static com.miniproject32.writeup.S19.scene2d;
import static com.miniproject32.writeup.S19.scene2e;

//scene editor: generate one for each scene from outline in a sequential order. 
//Store scenes in files. These can be accessed and viewed from home screen
public class S21 extends AppCompatActivity {
int sceneno;
TextView t1,t2;
EditText e;

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

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        sceneno = extras.getInt("Scene no ");
        //The key argument here must match that used in the other activity
    }
    else
        sceneno=1;

    Typeface tf = Typeface.createFromAsset(getAssets(),"font.ttf");

    t1 = (TextView) findViewById(R.id.editor_scene_label);
    t2 = (TextView) findViewById(R.id.editor_scene_val);

        switch (sceneno) {
            case 1:
                t1.setText(R.string.scene_0);
                t2.setText(scene0);
                break;
            case 2:
                t1.setText(R.string.scene_1a);
                t2.setText(scene1a);
                break;
            case 3:
                t1.setText(R.string.scene_1b);
                t2.setText(scene1b);
                break;
            case 4:
                t1.setText(R.string.scene_1c);
                t2.setText(scene1c);
                break;
            case 5:
                t1.setText(R.string.scene_1d);
                t2.setText(scene1d);
                break;
            case 6:
                t1.setText(R.string.scene_1e);
                t2.setText(scene1e);
                break;
            case 7:
                t1.setText(R.string.scene_2a);
                t2.setText(scene2a);
                break;
            case 8:
                t1.setText(R.string.scene_2b);
                t2.setText(scene2b);
                break;
            case 9:
                t1.setText(R.string.scene_2c);
                t2.setText(scene2c);
                break;
            case 10:
                t1.setText(R.string.scene_2d);
                t2.setText(scene2d);
                break;
            case 11:
                t1.setText(R.string.scene_2e);
                t2.setText(scene2e);
                break;
            case 12:
                t1.setText(R.string.resolution);
                t2.setText(resolution);
                break;

        }

    Button b=(Button) findViewById(R.id.but_editor);
    b.setText(R.string.done);
    b.setTypeface(tf);
}

public void writeStringAsFile(final String fileContents, String fileName) {
     try {
         FileWriter out = new FileWriter(new File(this.getFilesDir(), fileName));
         out.write(fileContents);
         out.close();
     }
     catch (IOException e) {
        e.printStackTrace();
     }
}

public void nextScreen(View view){
    //on click:
    //save scene
    //show popup/toast saying scene saved
    //move to new blank same screen with scene title different
    e=(EditText) findViewById(R.id.editor_ip);
        switch (sceneno) {
            case 1:
                writeStringAsFile(e.getText().toString(), "Scene1.txt");
                Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                writeStringAsFile(e.getText().toString(), "Scene2.txt");
                Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                break;
            case 3:
                writeStringAsFile(e.getText().toString(), "Scene3.txt");
                Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                break;
            case 4:
                writeStringAsFile(e.getText().toString(), "Scene4.txt");
                Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                break;
            case 5:
                writeStringAsFile(e.getText().toString(), "Scene5.txt");
                Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                break;
            case 6:
                writeStringAsFile(e.getText().toString(), "Scene6.txt");
                Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                break;
            case 7:
                writeStringAsFile(e.getText().toString(), "Scene7.txt");
                Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                break;
            case 8:
                writeStringAsFile(e.getText().toString(), "Scene8.txt");
                Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                break;
            case 9:
                writeStringAsFile(e.getText().toString(), "Scene9.txt");
                Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                break;
            case 10:
                writeStringAsFile(e.getText().toString(), "Scene10.txt");
                Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                break;
            case 11:
                writeStringAsFile(e.getText().toString(), "Scene11.txt");
                Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                break;
            case 12:
                writeStringAsFile(e.getText().toString(), "Scene12.txt");
                Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                break;

            default:
                break;
        }
    sceneno++;
    Intent intent = new Intent(S21.this,S21.class);
    intent.putExtra("Scene no ",sceneno);
    startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_home_screen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_home){
        Intent i = new Intent(this, Home.class);
        startActivity(i);
    }
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

这应该正常吗?谁能告诉我我错过了什么?

1 个答案:

答案 0 :(得分:0)

每次增加值sceneno ++时。因此它最多可以工作12个案例,但之后根据代码概念不显示任何内容。所以在这样的开关案例中添加默认值

   switch (sceneno) {
        case 1:

       .....
        case 12:
            t1.setText(R.string.resolution);
            t2.setText(resolution);
            break;
       default :
            t1.setText("your text");
            t2.setText("your text");
    }