Change Text with Inflatring

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Change Text with Inflatring

Postby Andy123 » Fri Jan 29, 2010 11:03 am

Hy to all.
I will change text with Inflater.
how I can make it?

I did this.:

main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:orientation="vertical"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent"
  11.  
  12.    >
  13.  
  14. <TextView  
  15.  
  16.    android:layout_width="fill_parent"
  17.  
  18.    android:layout_height="wrap_content"
  19.  
  20.    android:text="Hello"
  21.  
  22.    android:id="@+id/txt_view" android:textSize="40px"/>
  23.  
  24.  
  25.  
  26. <Button
  27.  
  28.                 android:id="@+id/Button01"
  29.  
  30.                 android:layout_height="50px"
  31.  
  32.                 android:layout_width="100px"
  33.  
  34.                 android:text="Pus"></Button>
  35.  
  36. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


InflaterTest.java :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package org.example;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.view.View;
  11.  
  12. import android.view.View.OnClickListener;
  13.  
  14. import android.widget.Button;
  15.  
  16.  
  17.  
  18. public class InflaterTest extends Activity {
  19.  
  20.  
  21.  
  22.     @Override
  23.  
  24.     public void onCreate(Bundle savedInstanceState) {
  25.  
  26.         super.onCreate(savedInstanceState);
  27.  
  28.         setContentView(R.layout.main);
  29.  
  30.  
  31.  
  32.         Button bt_pus = (Button) findViewById(R.id.Button01);
  33.  
  34.         bt_pus.setOnClickListener(new OnClickListener() {
  35.  
  36.                        
  37.  
  38.                         public void onClick(View v) {
  39.  
  40.                                 ModifText modiftext = new ModifText(getParent());
  41.  
  42.                                 modiftext.change("Hello World");
  43.  
  44.                                
  45.  
  46.                         }
  47.  
  48.                 });  
  49.  
  50.     }    
  51.  
  52. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


ModifText.java :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package org.example;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.view.View;
  9.  
  10. import android.widget.TextView;
  11.  
  12.  
  13.  
  14. public class ModifText {
  15.  
  16.        
  17.  
  18.         private TextView textview;
  19.  
  20.  
  21.  
  22.         public ModifText(Activity activity) {
  23.  
  24.                
  25.  
  26.                 View view = activity.getLayoutInflater().inflate(R.layout.main, null);
  27.  
  28.                 textview = ((TextView)view.findViewById(R.id.txt_view));
  29.  
  30.                
  31.  
  32.         }
  33.  
  34.  
  35.  
  36.         public void change(String changedText) {
  37.  
  38.                
  39.  
  40.                 textview.setText(changedText);
  41.  
  42.                
  43.  
  44.         }
  45.  
  46.        
  47.  
  48. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4




what is wrong?
thenks

Andy123
Andy123
Freshman
Freshman
 
Posts: 2
Joined: Fri Jan 29, 2010 9:01 am

Top

Postby jwei512 » Fri Jan 29, 2010 12:36 pm

What's the error exactly?
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Re

Postby Andy123 » Fri Jan 29, 2010 1:20 pm

I repaired a small one:

InflaterTest:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class InflaterTest extends Activity {
  2.  
  3.  
  4.  
  5.     @Override
  6.  
  7.     public void onCreate(Bundle savedInstanceState) {
  8.  
  9.         super.onCreate(savedInstanceState);
  10.  
  11.         setContentView(R.layout.main);
  12.  
  13.  
  14.  
  15.         Button bt_pus = (Button) findViewById(R.id.Button01);
  16.  
  17.         bt_pus.setOnClickListener(new OnClickListener() {
  18.  
  19.                        
  20.  
  21.                         public void onClick(View v) {
  22.  
  23.                                 ModifText modiftext = new ModifText(InflaterTest.this.getBaseContext());
  24.  
  25.                                 modiftext.change("W");
  26.  
  27.                                
  28.  
  29.                         }
  30.  
  31.                 });  
  32.  
  33.     }    
  34.  
  35. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


ModifText.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ModifText {
  2.  
  3.        
  4.  
  5.         private TextView textview;
  6.  
  7.  
  8.  
  9.         public ModifText(Context context) {
  10.  
  11.                
  12.  
  13.                 final LayoutInflater inflater = LayoutInflater.from(context);
  14.  
  15.                 View v = inflater.inflate(R.layout.main, null);
  16.  
  17.                
  18.  
  19.                 textview = ((TextView)v.findViewById(R.id.txt_view));
  20.  
  21.                
  22.  
  23.         }
  24.  
  25.  
  26.  
  27.         public void change(String changedText) {
  28.  
  29.                
  30.  
  31.                 textview.setText(changedText);
  32.  
  33.                
  34.  
  35.         }
  36.  
  37. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


there is not a result.
and there is not a mistake.
And the prog. does nothing :-(
Andy123
Freshman
Freshman
 
Posts: 2
Joined: Fri Jan 29, 2010 9:01 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests