开发者

Designing an Android User Interface: Textview text doesn't change

I'm having issues with understanding 开发者_高级运维how I should organize my user interface in Android. My original plan was to create TextViews and ListViews programatically and change them when buttons are clicked, etc.

Here's my first simple attempt. viewFriends is a method within my Activity class. It's called when a menu button is pressed.

    private void viewFriends()
{
    mText = new TextView(this);
    mText.setText("Gathering information...");
    setContentView(mText);

...irrelevant code follows

Why doesn't this seemingly simple example work? How should I logically organize and manage my user interface objects (TextViews, ListViews, Buttons, etc).

Thanks.


The best work would be having those listviews and textviews in your XML files and give them a suitable ID like following:

    <ListView       
        android:id="@+id/myList"
            android:layout_width="wrap_content"
            android:layout_weight="1"
    />

Just like above have your text view too in XML file an add the android:id attribute. Once you define this way in your java file have references to them:

ListView myListObj = (ListView)findViewById(R.id.myList);

Now you have an object called myListObj in your java file and now you can do whatever you want to do with it.

:)

Let me if you find any issue in this so that I can update the answer to meet your specific need.


Don`t use setContentView in your method. Usually it should only be called once in the onCreate method of your activity. Best predefine your bottons/TextViews in xml, get a handle for them (findViewbyId...) and modify them that way.

If you create them programaticly, just add them to a view containter from your xml. Like :

setContentView(R.layout.main);

Lets say in main.xml there is a LinearLayout with the id: root.

// get accces to that layout:
LinearLayout rootLayout = (LinearLayout) findViewById (R.id.root);

// create a new TextView
TextView tv1 = new TextView (this);
tv.setText("Hello!");
// add it to your base layout
rootLayout.addView(tv1);

// done! :)


Make a double check on what you are getting in "this". change it to your java file name.this


You have to reload/refresh your activity once you change it.

Try this

@Override
    protected void onResume() {

        if(param.equalsIgnoreCase("gr"))
        {
        finish();
        Intent myIntent = new Intent(yourActivity.this, yourActivity.class);
        startActivity(myIntent);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜