Extracting values from strings.xml - android

I have a doubt.I have an activity which contains an autocompletetextview.The contents for autocompletetextview have been declared in strings.xml file as a string array.I hav one more string array in my strings.xml file.What i want is that when i select an item from autocompletetextview it should display a value from the second string array in the form of a toast.Is it possible.Plz help me

For Array::
String[] myarray =getResources().getStringArray(R.array.array);
For String::
String myString =getResources().getString(R.string.str);

Check with this,
package mytest.projects;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class example 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);
}
}

Related

How to add item to list view

Hi I'm new to Android i want to create application similar to app given in this link
http://www.androiddom.com/2011/02/android-shopping-cart-tutorial.html?m=1
Only difference is that instead of list view in catalog activity I have image buttons.so wen I click any image button it goes to product details activity(same as example in link).Other activities are same as example in link.
Only thing I want is how to add item to list view by using button add to cart if there are image buttons in catalog activity.
Please do provide me help how to go about.
Main Layout File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainListView">
</ListView>
Activity File
package com.example.android;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SimpleListViewActivity extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// Create and populate a List of context names.
String[] context = new String[] { "context1", "context2", "context3", "context4","context5"};
ArrayList<String> context = new ArrayList<String>();
contextList.addAll( Arrays.asList(context) );
// Create ArrayAdapter using the context list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, contextList);
// Add more contexts. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "context8" );
listAdapter.add( "context9" );
listAdapter.add( "context10" );
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
}
}

Listview is empty

I have the following codes as below. I am trying to run a simple list view all with not error and output is empty white screen.
MainActivity.java
public class MainActivity extends ActionBarActivity {
String[] stateArray = {"jh","kd"};
private ListView stateListView;
private ArrayAdapter<string> arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stateListView = (ListView) findViewById(R.id.state_list);
arrayAdapter = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1);
stateListView.setAdapter(arrayAdapter);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.lab4_1.MainActivity" >
<ListView
android:id="#+id/state_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
Al works fine no error and the emulator just show empty which screen nothing inside. What could be missing?
Try this way
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stateArray);
Passed your Array as third argument of ArrayAdapter
For more information go to AndroidListView/Vogella
You have not passed your actual data in your adapter that is why its showing listview empty. Simple pass your stateArray as last parameter of your adapter as below:
Change your adapter line as below:
String[] stateArray = {"jh","kd"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, stateArray);
Make your <string> as <String> you need to make your 'S' capital.
Note: The <String> part of it all means that the ArrayAdapter will be working with String[] data (the paths parameter). In other words, each element in the array will be a String.
It is empty because you are not submitting the dataset to the Adapter
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
Change it in
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stateArray);
Here you can find the documentation
Change
arrayAdapter = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1);
to
arrayAdapter = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,stateArray );
Use like this
adapter.setAdapter(new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,stringarray));
Try this
arrayAdapter = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,stateArray);
this is empty because you are not passing any data to adapter, the adapter acts as a bridge and is responsible of making the view see this See Adapter
See these examples Examples this will help you.
Best of luck!
First of all you definition of the generics on the ArrayList are wrong. It needs to say ArrayList<String>. Note the capital S.
You need to add Items to you list. The UI Designer is adding sample items automatically, that might have confused you into thinking there would already be items.
Try adding something like arrayAdapter.add("Test"); at the end of you onCreate().
If that works you can either add your stateArray using:
arrayAdapter.addAll(stateArray)
Full MainActivity.java:
package com.example.test;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
String[] stateArray = {"jh","kd"};
private ListView stateListView;
private ArrayAdapter<String> arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stateListView = (ListView) findViewById(R.id.state_list);
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
stateListView.setAdapter(arrayAdapter);
arrayAdapter.add("Test");
arrayAdapter.addAll(stateArray);
}
}

how to read items from string-array on android

I got a problem with reading items from string-array one by one. For example, i got string-array with 10 items:
<string-array name="arr">
<item>First</item>
<item>Second</item>
<item>...</item>
<item>Tenth</item>
</string-array>
So i know how to display items randomly, im using this code
Resources res = getResources();
myString = res.getStringArray(R.array.arr);
int length=myString.length;
int index=rgenerator.nextInt(length);
String q = myString[index];
tv = (TextView) findViewById(R.id.text);
tv.setText(q);
And in TextView on every button click it displays random item from array.
Problem is, how to make display item from string-array not randomly. Like, it starts from displaying First, then on click it displays Second, and so on untill end of array.
Please help!
You can't initialize your testArray field this way, because the application resources still aren't ready.
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);
}
}
Declare a variable
int currentIndex=0;
outside this onClick method.
In the
onClick(View v)
{
//Verify if only that btn is clicked
{
tv.setText(myString[(currentIndex++)%(myString.length)]);
}
}
Hope it works.
try
int i=0;
String q = myString[i];
i++;
strings.xml
<string-array name="month">
<item>Jan</item>
<item>Feb</item>
<item>Mar</item>
<item>Apr</item>
<item>May</item>
<item>Jun</item>
<item>Jul</item>
<item>Aug</item>
<item>Sep</item>
<item>Oct</item>
<item>Nov</item>
<item>Dec</item>
</string-array>
java
for (int i = 0; i < getResources().getStringArray(R.array.month).length; i++) {
Log.e("Array",""+getResources().getStringArray(R.array.month)[i]);
}

spinner cannot be resolved or is not a field

I am studying ApiDemos sample in SDK
and trying to separate Menu example(App/Menu/Inflate from Menu-MenuInflateFromXml.java).
package my.android.Menu;
import my.android.Menu.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MyMenu extends Activity {
/**
* Different example menu resources.
*/
private static final int sMenuExampleResources[] = {
R.menu.title_only, R.menu.title_icon, R.menu.submenu, R.menu.groups,
R.menu.checkable, R.menu.shortcuts, R.menu.order, R.menu.category_order,
R.menu.visible, R.menu.disabled
};
/**
* Names corresponding to the different example menu resources.
*/
private static final String sMenuExampleNames[] = {
"Title only", "Title and Icon", "Submenu", "Groups",
"Checkable", "Shortcuts", "Order", "Category and Order",
"Visible", "Disabled"
};
/**
* Lets the user choose a menu resource.
*/
private Spinner mSpinner;
/**
* Shown as instructions.
*/
private TextView mInstructionsText;
/**
* Safe to hold on to this.
*/
private Menu mMenu;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a simple layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// Create the spinner to allow the user to choose a menu XML
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sMenuExampleNames);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner = new Spinner(this);
// When programmatically creating views, make sure to set an ID
// so it will automatically save its instance state
mSpinner.setId(R.id.spinner);//Error Line
mSpinner.setAdapter(adapter);
setContentView(R.layout.main);
}
}
IDE show this error "spinner cannot be resolved or is not a field", i can't go head.
i did't see any XML file which include "spinner".
As per Tim's comment above:
put this line in your onCreate()
mSpinner = (Spinner) findViewById(R.id.spinner);
You should move your setContentView(R.layout.main); right below of the super.onCreate(savedInstanceState); It's necessary to set the content of your activity before using its Views and you don't need to instanciate the spinner View programmatically. Simply use the Spinner defined at the main.xml file (I Assume you have a Spinner inside this file).
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a simple layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// Create the spinner to allow the user to choose a menu XML
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sMenuExampleNames);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//mSpinner = new Spinner(this);
// When programmatically creating views, make sure to set an ID
// so it will automatically save its instance state
// mSpinner.setId(R.id.spinner);//Error Line
mSpinner = (Spinner) findViewById(R.id.you_spinner_id); //you_spinner_id must be defined at main.xml
mSpinner.setAdapter(adapter);
}

Adapter for list view

please guide me with this program. Why do we need to use an array adapter to show the list? What is this "adapter", and can we display things directly in the ListView, without an adapter? Like, can we set setListAdapter(names) instead of setListAdapter(adapter);? Thanks.
Here is the code:
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class Episode7 extends ListActivity {
String[] names = {
"Elliot","Geoffrey","Samuel","Harvey","Ian","Nina","Jessica",
"John","Kathleen","Keith","Laura","Lloyd"
};
/** 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;
/* 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,
names);
// Assign the adapter to this ListActivity
setListAdapter(adapter);
}
}
From the android API reference,
An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.
It basically a set of interfaces that determine how the data will be handled by the list. You can use different pre-made adapter classes in your lists or create your own if you want to present custom data.
Take a look at this page in the Dev Guide: http://developer.android.com/guide/topics/ui/binding.html
Lars Vogel has a nice tutorial also: http://www.vogella.de/articles/AndroidListView/article.html
The Adapter acts as both a container for the information you want to display, and allows you to change how it is displayed by over-riding the getView() method of the adapter. Normally, by default, the adapter will call the toString() method of the Object used to create the Adapter and set the text in the TextView that is referenced in the layout provided by android.R.layout.simple_list_item_1... but by over-riding the adapter's getView(), you can have a more complicated layout display for the list.
To answer the initial question... you must use an adapter with a ListView.
This is how I do it and it works for me:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListViewDemo extends Activity {
// --------- Create your string array, adapter and ListView
String[] items = {"Cars", "Money","Vacation","Electronics",
"Shoes","Jewelry", "Buku bucks","Cash","Ham","Swag","Straight Cash","Homies","Roll Dawgs","Nate Dogg","Wiz Khalifa","Mac Miller","Chitty Bang",
"Sam Adams","Technine","Kanye West","Rims","Escalade","Spreewells","Chrome Rims","24's",
"Lebron James","Dwayne Wade","Andre Iguodala","Allen Iverson","Jodi Meeks",
"Levoy Allen","Mo Williams","Eric Snow","Alien Iverson","Laptop","Phone","Tablet"};
ArrayAdapter<String> adapter;
ListView cashList;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cashList = new ListView(this);
// create the array adapter<String>(context, layout, array)
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
// add the adapter to the list
cashList.setAdapter(adapter);
// set the list as the content view
setContentView(cashList);
}
}

Categories

Resources