Android listview: Resource Not Found Exception - android

I've got problem with Listview in Android.
Then I try to set adapter to a listview I got Resource Not Found Exception.
My code:
public class MyActivity extends Activity {
ArrayList<String> list;
public void onCreate(Bundle savedInstanceState) {
list = new ArrayList<String>();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list.add("first");
listView = (ListView)findViewById(R.id.CheckpointList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.CheckpointList, list);
listView.setAdapter(adapter);
}
}
And in my main.xml I've got:
<ListView android:id="#+id/CheckpointList" android:layout_height="wrap_content" android:layout_width="fill_parent"></ListView>
I've tried to clean and refesh project - no effects...
How to solve this problem?
Cheers.

The resource you hand over to the ArrayAdapter should not be the id of the ListView. It should be the textViewResourceId - which is basically which TextView-layout-id you want your items, in the list, to be rendered as.
One of the standards is e.g. android.R.layout.simple_list_item_1.
Here's an example of a simple ListView:
public class ListviewExample extends Activity
{
private ListView listView;
private String listView_data[] = {"Android","iPhone","BlackBerry","AndroidPeople"};
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.ListView1);
// By using setAdapter method in listview we add the string array to the ListView.
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , listView_data));
}
}

The ressource you hand over to the ArrayAdapter should not be the id of the listview. It should be the layout ressource of the textview in the listview. Look at the documentation: ArrayAdapter(Context context, int textViewResourceId, List objects)

Related

Why my app containing listView is crashing?

My app contains only 2 files one is MainActivity.java and activity_main.xml file. I wanted to use to listView to display a list of string but for some my app is crashing continuously.
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> arrayList=new ArrayList<String>();
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Pandey");
arrayList.add("Akshay");
arrayList.add("Akshay");
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,0,arrayList);
ListView listView=(ListView) findViewById(R.id.list_item);
listView.setAdapter(adapter);
}
}
Error
Your app is crashing because you are passing 0 as the second argument to your ArrayAdapter constructor. This is the "layout resource id" that your list will use for each item, so it needs to be something real.
The Android platform comes with some resources for this purpose built in, like android.R.layout.simple_list_item_1. You could try that. Change this line:
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,0,arrayList);
to this instead:
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
More information can be found in the developer guide: https://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews
You can't set the second argument of ArrayAdapter to 0. The constructor expects a valid resource. You need to set it to something like this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.my_resource_view, arrayList);

xml string-array to listView not working

public class Menu extends Activity {
String[] categories;
ListView lv;
Cursor cursor;
Context context;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
categories = getResources().getStringArray(R.array.Categories_Array);
lv = (ListView) findViewById(R.id.listViewCategories);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.activity_main,
getResources().getStringArray(R.array.Categories_Array));
lv.setAdapter(adapter);
}
...
}
I keep getting an "unfortuneately Bible CYB has stopped working" when I run the app
The problem you have is that you are giving your ArrayAdapter a full Activity's layout to inflate. What you want to do is pass it the row's layout it should use for each of the individual rows it should inflate:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, // or other basic row layout
getResources().getStringArray(R.array.Categories_Array));
You have set the activity_main.xml to the activity and use the same in the constructor of ArrayAdapter. To display the a single string in each row
Change this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.activity_main,
getResources().getStringArray(R.array.Categories_Array));
to
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.simple_list_item_1,
categories);
where simple_list_item_1.xml is the layout from the android framework
http://androidxref.com/4.4.3_r1.1/xref/frameworks/base/core/res/res/layout/simple_list_item_1.xml
And the constructor
public ArrayAdapter (Context context, int resource, T[] objects)
Added in API level 1 Constructor
Parameters context The current context. resource The resource ID for a
layout file containing a TextView to use when instantiating views.
objects The objects to represent in the ListView.
Instead of using a array adapter try using a list adapter.. it wud give it a custom layout u designed ..
lv = (ListView) findViewById(R.id.listViewCategories);
ListAdapter adapter = new SimpleAdapter(this,
/*id for your custom layout for ex: R.layout.activity_main*/,
getResources().getStringArray(R.array.Categories_Array),/*Here write the id of the text view where u want to display the info.. forex: R.id.tvExample*/);
lv.setListAdapter(adapter);

List View not showing up

I want to know why this SIMPLE list view is not showing up. The error says(in short)-your content must have LisView whose id is "android.R.id.list"...
public class MyListView extends ListActivity {
static final String[] PENS = new String[]{
"MONT Blanc",
"Gucci",
"Parker",
"Sailor",
"Porsche Design",
"Rotring",
"Sheaffer",
"Waterman"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, PENS);
setListAdapter(adapter);
}
}
In your XML, change the ID attribute of your ListView to the following:
android:id="#android:id/list"
Since you're using a ListActivity, Android expects your Layout to contain a ListView with a specific ID. If it's not there, you will get a RuntimeException.

ListAdapter error

Could someone tell me what is wrong with this piece of code?
It is supposed to be a ListView from an XML file that is then referred to in Java.
Alas, it crashes my application every time it enters the Menu class.
public class Menu extends ListActivity {
String Name_for_classes[] = {"- 1-9 Tabels -", "- 10-19 Tabels -", "- 20-29 Tabels -" };
String Tabel_classes[] = {"First", "Second", "Third"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.menu, Name_for_classes));
ListView list = getListView();
list.setTextFilterEnabled(true);
}
}
Okay, let's assume you have your ListView in an XML file called my_listview.xml.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_listview.xml);
ListView list = (ListView)findViewById(R.id.list);
ArrayAdapter<String> yourAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Name_for_classes);
list.setAdapter(yourAdapter);
}
I'm not sure what is in R.layout.menu, but guessing by the naming its the activity layout. This should be used with setContentView(R.layout.menu) in onCreate. The layout that is passed into the ArrayAdapter is the TextView you are using to populate the listview.
You have forgotten to call the setContentView in your onCreate method, so your listview is not referenced yet.
When you use setContentView, it is equivalent as sayin 'for this activity I want to use the template 'myTemplate.xml'
After that, you have to 'linked' your java ListView attribute to the listview declared in your template.

Android ListView understanding

I have a question about the ListView and how to use it. My Prolem is that my listView is only a part of the view and I am not sure how to do this.
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView) findViewById(R.id.ListView01);
String[] strings = new String[]{"Test1","Test2"};
ArrayAdapter<String> myArrayAdapter= new ArrayAdapter<String>(this, R.id.ListView01,strings);
myListView.setAdapter(myArrayAdapter);
I think the problem is the "this" in myArrayAdapter!?
The layout resource id you're supposed to pass to ArrayAdapter is a layout that's used to render each item in the list, not the layout for the list itself. Android provides some layout resources for the common cases. Try using:
ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);

Categories

Resources