How to Get String Array from Arrays.Xml File

How to get String Array from arrays.xml file

You can't initialize your testArray field this way, because the application resources still aren't ready.

Just change the code to:

package com.xtensivearts.episode.seven;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class Episode7 extends ListActivity {
String[] mTestArray;

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Create an ArrayAdapter that will contain all list items
ArrayAdapter<String> adapter;

mTestArray = getResources().getStringArray(R.array.testArray);

/* Assign the name array to that adapter and
also choose a simple layout for the list items */
adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
mTestArray);

// Assign the adapter to this ListActivity
setListAdapter(adapter);
}
}

Android: How do you access a string-array from strings.xml in a custom class?

Pass the context to the constructor of custom class and use the same

new CustomClass(ActivityName.this);

Then

Context mContext;
public CustomClass(Context context)
{
mContext = context;
}

use the context

String[] foo_array = mContext.getResources().getStringArray(R.array.foo_array);

Also keep in mind

Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)

http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html

Also check this

android getResources() from non-Activity class

Edit:

Change this

public class CustomClass(Context context) 
{
}

To

public class CustomClass
{
Context mContext;
public CustomClass(Context context) // constructor
{
mContext = context;
}
}

How to get a String array with java code from string-item list in arrays.xml in Android?

I don't have an IDE in front of me right now, but I think you should be able to do something like R.res.arrays.YOUR_ARRAY_OF_STRINGS.

Get String Array from String.xml in ListView

Here is working my code please use this code:

String.xml

 <string-array name="titles">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
</string-array>

.java file

public class MainActivity extends Activity {

ListView lv;
String[] title;
CategoryImageAdapter adapter;

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

lv=(ListView) findViewById(R.id.listView1);

title=getResources().getStringArray(R.array.titles);

adapter=new CategoryImageAdapter(MainActivity.this, title);
lv.setAdapter(adapter);

}

public class CategoryImageAdapter extends BaseAdapter implements OnClickListener {

private Activity activity;
private String[] data;
private LayoutInflater inflater = null;

public CategoryImageAdapter(Activity a, String[] d) {
activity = a;
data = d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
return data.length;
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public void onClick(View v) {

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView == null){
vi = inflater.inflate(R.layout.row_lv, null);
holder = new ViewHolder();
holder.text = (TextView)vi.findViewById(R.id.textView1);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();

holder.text.setText("Title " + data[position]);

return vi;
}
}
public class ViewHolder {
public TextView text;

}

}

How Do I call String-array value in xml layout

Just to giva a full answer to your problem:

There is no way of accessing an array directly, but you could do something like this:

<string name="books_titles_en_1">Smoking Effeccts on Your Body</string>
<string name="books_titles_en_2">Smoking - effects on your body</string>
<string name="books_titles_en_3">How Tobacco Smoke Causes Disease</string>
<string name="books_titles_en_4">patterns of use, health effects, use in smoking cessation and regulatory issues</string>
<string-array name="books_titles_en">
<item>@string/books_titles_en_1</item>
<item>@string/books_titles_en_2</item>
<item>@string/books_titles_en_3</item>
<item>@string/books_titles_en_4</item>
</string-array>

and then:

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/frag_1_text_pad"
android:textSize="@dimen/frag_1_text_size"
android:paddingRight="@dimen/frg_1_text_right_pad"
android:id="@+id/book_link0"
android:text="@string/book_title_en_1"/>

So you basically reference the neccessary strings directly, that are referenced in your string array. ;)

How to get string from string-array in strings.xml

To get String from strings.xml you should have key for that String.But in the above you have key for String array so you have to get String array from that you have to get required String.

CurrentWebpage = getResources().getStringArray(R.array.link_categories)[0];

Hope this will helps you.

Having a trouble getting List of Strings from array.xml file and parsing them to array of floats android

Try to use string-array instead of array



Related Topics



Leave a reply



Submit