Reading Text From String Array

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

Reading Text From String Array

Postby DaveT » Thu Jul 30, 2009 8:00 pm

I want to pick a random string by its value from an array in Strings.xml and then display the string. I know it's a newbie question but I'd be grateful if someone could help me out.

Thanks in advance.

Dave
DaveT
Freshman
Freshman
 
Posts: 9
Joined: Sun Jul 26, 2009 8:58 pm

Top

Postby Erykgecko » Thu Jul 30, 2009 10:04 pm

In your strings.xml you need to set up the array, something like below

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.    
  2.  
  3. <?xml version="1.0" encoding="utf-8"?>
  4.  
  5. <resources>
  6.  
  7.     <string name="hello">Hello World, test!</string>
  8.  
  9.     <string name="app_name">testing</string>
  10.  
  11.         <string-array name="myArray">
  12.  
  13.                 <item>string 1</item>
  14.  
  15.                 <item>string 2</item>
  16.  
  17.                 <item>string 3</item>
  18.  
  19.                 <item>string 4</item>
  20.  
  21.                 <item>string 5</item>
  22.  
  23.  
  24.  
  25.      </string-array>
  26.  
  27. </resources>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


then in your main activity something like

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.Activity;
  2.  
  3. import android.content.res.Resources;
  4.  
  5. import android.os.Bundle;
  6.  
  7. import android.widget.TextView;
  8.  
  9.  
  10.  
  11. public class test extends Activity {
  12.  
  13.         private CharSequence[] myString;
  14.  
  15.     /** Called when the activity is first created. */
  16.  
  17.     @Override
  18.  
  19.     public void onCreate(Bundle savedInstanceState) {
  20.  
  21.         super.onCreate(savedInstanceState);
  22.  
  23.         setContentView(R.layout.main);
  24.  
  25.         Resources res = getResources();
  26.  
  27.         myString = res.getTextArray(R.array.myArray);
  28.  
  29.         TextView myTV = (TextView)findViewById(R.id.myTV);
  30.  
  31.         myTV.setText(myString[3]);
  32.  
  33.     }
  34.  
  35. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


remember, arrays start at 0, so although you;ve said to select 3, it will actually select "String 4"

Add a little randomizer in there to select the number and youre done
Erykgecko
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sat May 16, 2009 5:32 pm

Postby DaveT » Sat Aug 01, 2009 3:06 pm

Thanks for the response! Very helpful.
DaveT
Freshman
Freshman
 
Posts: 9
Joined: Sun Jul 26, 2009 8:58 pm

Postby DaveT » Sat Aug 01, 2009 3:21 pm

I keep getting an error on:
Resources res = getResources();

"The method getResources() is undefined for the type..."

Any tips?
DaveT
Freshman
Freshman
 
Posts: 9
Joined: Sun Jul 26, 2009 8:58 pm

Postby Erykgecko » Sat Aug 01, 2009 5:05 pm

did you definatly include all the imports I listed?

That code should work. I ran it in eclipse on the emulator and on my phone without any probs
Erykgecko
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sat May 16, 2009 5:32 pm

Postby DaveT » Sun Aug 02, 2009 6:56 pm

I can get it to work fine when running the code on its own, but when I try to add it to my code I get the above error. I'm sure it's some newbie mistake I'm making.

I'm trying to add it to public void run().


Here is my code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. package com.vgmy.clippy;
  5.  
  6.  
  7.  
  8. import java.util.Random;
  9.  
  10. import java.util.Timer;
  11.  
  12. import java.util.TimerTask;
  13.  
  14. import android.appwidget.AppWidgetManager;
  15.  
  16. import android.appwidget.AppWidgetProvider;
  17.  
  18. import android.content.ComponentName;
  19.  
  20. import android.content.Context;
  21.  
  22. import android.content.Intent;
  23.  
  24. import android.content.res.Resources;
  25.  
  26. import android.os.Bundle;
  27.  
  28. import android.widget.RemoteViews;
  29.  
  30. import android.widget.TextView;
  31.  
  32. import android.widget.Toast;
  33.  
  34. import android.app.Activity;
  35.  
  36. import android.content.res.Resources;
  37.  
  38. import android.widget.TextView;
  39.  
  40.  
  41.  
  42. public class HelloWidget extends AppWidgetProvider {
  43.  
  44.        
  45.  
  46.        
  47.  
  48.        
  49.  
  50.         @Override
  51.  
  52.         public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  53.  
  54.                         int[] appWidgetIds) {
  55.  
  56.                 Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();
  57.  
  58.                
  59.  
  60.                 Timer timer = new Timer();
  61.  
  62.                 timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
  63.  
  64.                
  65.  
  66.                 super.onUpdate(context, appWidgetManager, appWidgetIds);
  67.  
  68.                
  69.  
  70.  
  71.  
  72.                
  73.  
  74.         }
  75.  
  76.        
  77.  
  78.        
  79.  
  80.        
  81.  
  82.         private class MyTime extends TimerTask {               
  83.  
  84.                
  85.  
  86.                 RemoteViews remoteViews;
  87.  
  88.                 AppWidgetManager appWidgetManager;
  89.  
  90.                 ComponentName thisWidget;
  91.  
  92.                
  93.  
  94.                 public MyTime(Context context, AppWidgetManager appWidgetManager) {
  95.  
  96.                         this.appWidgetManager = appWidgetManager;
  97.  
  98.                         remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
  99.  
  100.                         thisWidget = new ComponentName(context, HelloWidget.class);
  101.  
  102.                 }
  103.  
  104.                
  105.  
  106.                
  107.  
  108.  
  109.  
  110.                
  111.  
  112.                 CharSequence[] myString;
  113.  
  114.                 public void run(Bundle savedInstanceState) {
  115.  
  116.  
  117.  
  118.                
  119.  
  120.                         //GENERATE A RANDOM NUMBER TO CALL A STRING
  121.  
  122.                         int random = new Random().nextInt(8);
  123.  
  124.                        
  125.  
  126.                         Resources res = getResources();
  127.  
  128.                     myString = res.getTextArray(R.array.quotes);
  129.  
  130.                        
  131.  
  132.                        
  133.  
  134.                         remoteViews.setTextViewText(R.id.widget_textview, "" + myString[3]);
  135.  
  136.                         appWidgetManager.updateAppWidget(thisWidget, remoteViews);
  137.  
  138.                        
  139.  
  140.                        
  141.  
  142.                        
  143.  
  144.                
  145.  
  146.                 }
  147.  
  148.         }
  149.  
  150.        
  151.  
  152.         @Override
  153.  
  154.         public void onReceive(Context context, Intent intent) {
  155.  
  156.                
  157.  
  158.                 // v1.5 fix that doesn't call onDelete Action
  159.  
  160.                 final String action = intent.getAction();
  161.  
  162.                 if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
  163.  
  164.                         final int appWidgetId = intent.getExtras().getInt(
  165.  
  166.                                         AppWidgetManager.EXTRA_APPWIDGET_ID,
  167.  
  168.                                         AppWidgetManager.INVALID_APPWIDGET_ID);
  169.  
  170.                         if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
  171.  
  172.                                 this.onDeleted(context, new int[] { appWidgetId });
  173.  
  174.                         }
  175.  
  176.                 } else {
  177.  
  178.                         super.onReceive(context, intent);
  179.  
  180.                 }
  181.  
  182.         }
  183.  
  184.        
  185.  
  186.         @Override
  187.  
  188.         public void onDeleted(Context context, int[] appWidgetIds) {
  189.  
  190.                 Toast.makeText(context, "onDelete", Toast.LENGTH_SHORT).show();
  191.  
  192.                 super.onDeleted(context, appWidgetIds);
  193.  
  194.         }
  195.  
  196.        
  197.  
  198. }
  199.  
  200.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
[syntax="java"]
DaveT
Freshman
Freshman
 
Posts: 9
Joined: Sun Jul 26, 2009 8:58 pm

Top

Postby myster23 » Mon Aug 03, 2009 8:17 am

How can I create five different radio buttons in one for ?
For example, the following code create one radio button five times:
for(int i=1; i<=5; i++)
{
RadioButton rd=new RadioButton(this);
}

How can I edit it so that it can create five different radio buttons ?
myster23
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Apr 23, 2009 7:53 am

Postby DaveT » Mon Aug 03, 2009 8:20 am

myster23 wrote:How can I create five different radio buttons in one for ?
For example, the following code create one radio button five times:
for(int i=1; i<=5; i++)
{
RadioButton rd=new RadioButton(this);
}

How can I edit it so that it can create five different radio buttons ?


Errrrrm....way to hog a thread much?
DaveT
Freshman
Freshman
 
Posts: 9
Joined: Sun Jul 26, 2009 8:58 pm

Postby myster23 » Mon Aug 03, 2009 10:55 am

what ? I do not understand.
myster23
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Apr 23, 2009 7:53 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests