xml string-array to listView not working - android

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);

Related

Constructor cannot be resolved

I am trying to create an array adapter to populate my list view.
Below is the code I have however the Array Adapter gets the error "Cannot resolve constructor".
Can anybody please help me with why this might be happening?
Thanks.
private void populateListView(){
//Create list of items'
String[] myItems = {"Blue", "red", "green", "purple"};
//Build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.deal_items, myItems);
//Configure the list view
ListView list = (ListView) findViewById(R.id.listViewMain);
list.setAdapter(adapter);
}
The first parameter to the ArrayAdapter constructor needs to be a Context. You appear to have implemented this method in a Fragment, which is not a Context. If so, then replace this in the ArrayAdapter constructor with getActivity().

How to display the imageview and textview using listview without baseadapter in android?

i want to display the imageview and textview using listview .But without using base adapter in listview.is this possible using ArrayAdapter?
final String[] menuitems = new String[] {
"Settings",
"Notifications",
"Logout"
};
int[] menuiicons = { R.drawable.logo, R.drawable.logo, R.drawable.logo };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mainListView = (ListView) findViewById( R.id.mainListView );
ArrayList<String> menuitemList = new ArrayList<String>();
ArrayList<Integer> menuiconsList = new ArrayList<Integer>();
listAdapter = new ArrayAdapter<String>(this, R.layout.listview_home, menuitemList);
mainListView.setAdapter( listAdapter );
I think it is not, because you need to populate the ListView through a a getView(..) method, and this is a BaseAdapter method.
Is there a reason why you don't want to use BaseAdapter?
of course you can use ArrayAdapter just override its getView method
Yeah you can add text and images in arrayAdapter .
ArrayAdapter(context, resource, textViewResourceId, objects)
like this you can have lot few more methods .. her reource id your image.
and also have a look at this..
http://developer.android.com/reference/android/widget/ArrayAdapter.html

Populating ListView with ArrayAdapter force closes

I am trying to use the following code to populate a ListView using a predefined array of strings:
String[] schedule_names = getResources().getStringArray(R.array.test_schedules);
// Populate the ListView using the array of schedule names
ArrayList<String> als = new ArrayList<String>(Arrays.asList(schedule_names));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.listView, als);
adapter.add("Test");
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
But it force closes unless I comment out listView.setAdapter(adapter); (which obviously means the ListView isn't populated at all). It seems the reason is a NullPointerException.
Why is this?
This line is wrong...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.listView, als);
...the second parameter of the constructor should not be your ListView, it should be a layout with a TextView.
Try replacing R.id.listView with android.R.layout.simple_list_item_1

Android listview: Resource Not Found Exception

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)

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