Android ArrayAdapter.Add method not working - android

The ArrayAdapter.add() method is not working for me. I am using Eclipse Helios 3.6 with ADT Plugin, Target Source is a Froyo 2.2 emulator and 2.2 HTC Evo 4g. Here is my java class
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] entries = {"List Item A", "List Item B"};
ArrayAdapter<String> arrAdapt=new ArrayAdapter<String>(this, R.layout.list_item, entries);
arrAdapt.setNotifyOnChange(true);
arrAdapt.add("List Item C");
}
}
And here is my layout for the list item (list_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:padding="10dp"
android:textSize="12sp"
</TextView>
It is giving me and error in the LogCat that says
Caused by:
java.lang.UnsupportedOperationException
at
java.util.AbstractList.add(AbstractList.java:411)
at
java.util.AbstractList.add(AbstractList.java:432)
at
android.widget.ArrayAdapter.add(ArrayAdapter.java:178)

I'm just learning, but if I'm reading the source correctly, ArrayAdapter's constructor doesn't copy references to each of the elements in the array or list. Instead, it directly uses the list that's passed in, or for an array uses asList() to treat the original array as a list. Since the list returned by asList() is still just a representation of the underlying array, you can't do anything (such as resize) that you couldn't do with an array.
Try passing a list like ArrayList instead of an array.
ArrayList<String> entries =
new ArrayList<String>(Arrays.asList("List Item A", "List Item B"));
ArrayAdapter<String> arrAdapt=
new ArrayAdapter<String>(this, R.layout.list_item, entries);
arrAdapt.setNotifyOnChange(true);
arrAdapt.add("List Item C");

Related

Android changing activity issues, crashes on selection

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

Android Spinner not displaying array, but showing places

I've been trying to figure out how to make my spinner work for days straight now, and can't find anything to help me. My spinner doesn't show the values. The spinner opens up, and the spaces for each value is there, but they're all blank. I have tried using an ArrayList<String>, and also a String[] array, neither of which made a difference, but do I need to use one over the other? I have tried many different types of ArrayAdapters, but none of them have worked. The one in my code below is the one that works the closest, (as described above), and the other 3 are suggestions I saw online. I think my main problem is in my array adapter, but I'm not certain.
Here is my java class
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
engineSpinner = (Spinner)findViewById(R.id.rockets);
engineNameList = StagesData.getEngineNameList();
engStrList = engineNameList.toArray(new String[engineNameList.size()]);
adapter = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_spinner_item, engStrList);
// adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.array_engines);
// adapter = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_dropdown_item_1line, R.id.rockets, engStrList);
// adapter = ArrayAdapter.createFromResource(InfoActivity.this, android.R.layout.simple_spinner_item, R.array.engines);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
engineSpinner.setAdapter(adapter);
engineSpinner.setSelection(0);
Here is my xml spinner setup
<TableRow>
<TextView
android:text="#string/rocket"
android:padding="3dp"/>
<Spinner
android:id="#+id/rockets"
android:gravity="right"
android:entries="#array/engines"
android:clickable="true"
android:textColor="#color/colorblack"
android:spinnerMode="dropdown"
android:backgroundTint="#color/colorPrimary" />
</TableRow>
And finally, this is my string-array file I wanted to pull from (Do I put my string-array in it's own file in the values folder, or does it go inside the string file, I've seen both and don't know which, Thanks)
<?xml version="1.0" encoding="utf-8">
<resources>
<string-array name="engines">
<item>Ant</item>
<item>Dart</item>
<item>Dawn</item>
<item>Flea</item>
<item>Hammer</item>
<item>Kickback</item>
<item>MainSail</item>
<item>Mammoth</item>
<item>Nerv</item>
<item>Poodle</item>
<item>Puff</item>
<item>R.A.P.I.E.R</item>
<item>Reliant</item>
<item>Rhino</item>
<item>Skipper</item>
<item>Spark</item>
<item>Spider</item>
<item>Swivel</item>
<item>Terrier</item>
<item>Thud</item>
<item>Thumper</item>
<item>Twin Boar</item>
<item>Twitch</item>
<item>Vector</item>
</string-array>
</resources>
So to recap my questions, do I need to use ArrayList<String> or String[], how do I set up my ArrayAdapter, and where do I need to put my string-array?
Thank you in advance!
I recreate your sample and works fine for me changing
engStrList = engineNameList.toArray(new String[engineNameList.size()]);
adapter = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_spinner_item, engStrList);
to
engStrList = new String[]{"a","b","c"};
adapter = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_spinner_item, engStrList);
So my guess is that you should check what StagesData.getEngineNameList() is returning.
Also if you what to use your string array this is the right way
ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.engines));
Hope this helps

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

Android ListView Tutorial with onclick and images

i want to make a listview having images on each row and if one item is clicked, user will be taken to another activity
i have 26 activities-
Activity_a
Activity_b
Activity_c
Activity_d
and so on...
and i want to have alphabet images at each row (i already have the images)
i found this tutorial on http://www.ezzylearning.com/tutorial.aspx?tid=1659127
Here is how my app should look like http://www.imagesup.net/?di=413818360350
And here is my .java file
package com.Rohit.MyApp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView1 = (ListView) findViewById(R.id.listView1);
String[] items = { "Milk", "Butter", "Yogurt", "Toothpaste", "Ice Cream" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
}
}
You should only use one activity and start it with a bundle. 26 Activities is just plain wrong.
You can use an OnItemSelectedListener on the List view and start an activity with:
Intent i = new Intent(this,ABCActivity.class);
i.putExtra("LETTER",selectedLetter);
startActivity(i);
That's assuming you figured out how to get the selectedLetter.
I think you are looking for a good tutorial link on Custom Array Adapters, here you go. And then just setOnClickListener for the image view.
For good coding practice, don't ever use so many activities. Consider reusing some of them or use Fragments

Categories

Resources