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);
}
}
Related
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);
So, what I'm trying to do is have a main menu and then press a button, it loads a list of countries.
now I've seemed to have set it up right, no errors I can see, but the app crashes before I load this activity (the lists) the main menu is fine, and I added another button with a blank activity which loads just fine.
logCat is giving me these errors
E/ArrayAdapter: You must supply a resource ID for a TextView
D/AndroidRuntime: Shutting down VM
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
and I'm not sure what it's asking for beyond the textView
heres my XML:
<ListView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#f897"
android:dividerHeight="1dp"
android:listSelector="#0f0"/>
and my Java:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class countries extends AppCompatActivity {
ListView simpleList;
String countryList[] = {"India", "China", "Australia", "Portugal", "USA","England", "NewZealand", "Germany", "France","South Africa"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countries);
simpleList = (ListView)findViewById(R.id.text1);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_countries, countryList);
simpleList.setAdapter(arrayAdapter);
}
}
any help?
change:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_countries, countryList);
to:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,countryList);
your code not work because you use default ArrayAdapter with custom item layout
You array adapter expects the layout to be a TextView, but it look like R.layout.activity_countries is an activity layout?
You should create a custom adapter and custom view for each listview's row. Check it here: Custom Adapter for 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 );
}
}
I am having problem with the auto complete text view, when I type the drop down does not show suggestion as I type in.
What I used:
added app compact activity
created another project in android studio to see what was the problem;
I used API 2.3 and when i run the application as I type the suggestion drops down
package com.busticketing.stallionexpress;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class AutoCompletetab_1 extends AppCompatActivity {
//auto complete text view
AutoCompleteTextView autoCompleteTextView;
String[] city = {"Nairobi", "Mombasa", "Lamu", "Malindi"};
//** end of auto complete text view
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,city);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,city);
AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.Android);
autoCompleteTextView.setThreshold(3);
autoCompleteTextView.setAdapter(adapter);
}
}
First of all you don't have to declare adapter two times.
You are declaring variable named as acTextView and setting adapter to autoCompleteTextView which seems undefined as your codes given above.
Give the proper id using camel case and pascal case not like Android you are giving to AutoCompleteTextView.
Your corrected code as it is
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,city);
AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.Android);
acTextView.setThreshold(3);
acTextView.setAdapter(adapter);
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);
}
}