I need to populate a listview with an array from string resources. How do I pass the array to my adapter? When I try following I get the error below
ArrayAdapter zooAdapter = ArrayAdapter.createFromResource(this, R.array.animals, android.R.id.text1);
zooList.setAdapter(zooAdapter);
zooList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
}
});
Other than resource not found, I am not sure what to make of the error log. But then all my resources are there, I got no error from eclipse. In any case, what I need to do is load my arrayAdapter with data from an Array resource, does anyone know how to do that?
FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020014
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2184)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2139)
at android.content.res.Resources.getLayout(Resources.java:891)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2306)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1156)
at android.view.View.measure(View.java:15293)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1418)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:709)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:602)
at android.view.View.measure(View.java:15293)
at android.widget.SlidingDrawer.onMeasure(SlidingDrawer.java:270)
at android.view.View.measure(View.java:15293)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:630)
at android.view.View.measure(View.java:15293)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15293)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1418)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:709)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:602)
at android.view.View.measure(View.java:15293)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2425)
at android.view.View.measure(View.java:15293)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1876)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1112)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1301)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1010)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4255)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4849)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.FileNotFoundException:
at android.content.res.AssetManager.openXmlAssetNative(Native Method)
at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:487)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2166)
... 48 more
To add rows to a ListView you need to add it to your layout and implement an IListAdapter with methods that the ListView calls to populate itself. Android includes built-in ListActivity and ArrayAdapter classes that you can use without defining any custom layout XML or code.
Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view. xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.
You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .
Two wrong things I can see in your code:
1 - You don't get the array correctly:
getResources().getStringArray(R.array.animals);
2 - You don't pass a row layout to your Adapter
// Create an ArrayAdapter from the String Array.
final ArrayAdapter<CharSequence> aa =
ArrayAdapter.createFromResource
(
getActivity(), values, R.layout.list_row
);
In this case is a custom row
All together:
// Create an ArrayAdapter from the String Array.
final ArrayAdapter<CharSequence> aa =
ArrayAdapter.createFromResource
(
getActivity(), getResources().getStringArray(R.array.animals), R.layout.list_row
);
You can follow this too:
1) Store your string array in String[] variable, now this is going to be your source data.
2) Define ArrayAdapter, this will let you, how to represent your source data.
Now how to do that?
String[] zooString=getResources().getStringArray(R.array.animals);
ArrayAdapter<String> zooAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, zooString);
zooList.setAdapter(zooAdapter);
zooList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With