I need help with my first Spinner!! Not Displaying values - java

This is the first time I am trying to use a spinner and I need some help.. I made a layout with a spinner object on it and then I also made a array.xml with the numbers that I would like in the spinner in it. I run the following code and the screen displays without any values in my spinner??
public class SpinnerExaple extends Activity {
private Spinner numbersSpinner;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.numbersSpinner = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> numbersArray =
new ArrayAdapter<String>(this, R.layout.main,
getResources().
getStringArray(R.array.numbers));
}
}

You got the reference to the spinner, you got your AdapterArray set correctly- but you didn't attach the adapter to your spinner.
Add the line:
numbersSpinner.setAdapter( numbersArray );

You never set your numbers array into the spinner. Try adding the following at the end of the method:
numbersSpinner.setAdapter(numbersArray);
Also check out the hello-spinner tutorial.

Related

How to perform clicking on the first item in an ArrayList by Espresso

I have tried to click on the first item of my arraylist for few hours, I have read a lot of questions on Stack Overflow, tutorials and I have tried to implement them in many ways, but it doesn't work.
clickView(getSearchActivity().hintListView);
onData(anything())
.onChildView(withId(R.id.hintLayout))
.atPosition(0)
.perform(click());
onData(allOf(is(instanceOf(String.class))))
.atPosition(0)
.perform(click());
onData(instanceOf(ArrayList.class))
.atPosition(0)
.perform(click());
onData(instanceOf(String.class))
.atPosition(0)
.perform(click());
onData(anything())
.inAdapterView(withId(R.id.hintLayout))
.atPosition(0)
.perform(click());
onData(anything())
.inAdapterView(allOf(withId(R.id.hintLayout), isCompletelyDisplayed()))
.atPosition(0).perform(click());
I have to click on the first item of the ListView, that take ArrayList
id of this ListView is hintLayout.
Does anybody know what is the problem ? and where can it be ?
A ListView must be filled with an adapter. After your adapter is finished just add it to your ListView like this:
private void setListAdapter(ListDataAdapter adapter){
lvYourListView.setAdapter(adapter);
}
In order to get the data back out on the OnClick event you need to something like this in you onCreate() method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lvYourListView = (ListView) findViewById(R.id.lvListview);
lvYourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListData data = new ListData();
data = (ListData)parent.getItemAtPosition(position);
doSomethingWithData(data);
}
});
If you need help with the adapter - just let me know. I will need to see more of your code in order to do that.

Using 1 spinner to dynamically change 2 spinners with if functions and notifyDataSetChanged?

Pretty new to Java in general. I have 3 Spinners in my code, and my 2 spinners would display lists depending on 1 main spinner ( which has 2 selections). After reading a few threads I read about refreshing the lists using notifySetDataChanged(); but the spinner lists never changed. Few questions :
Am I using the notifySetDataChanged correctly?
Is there another way to populate the lists?
Is the IF function the appropriate method?
Here's the code upto the onCreate method.
public class MainActivity extends Activity {
private Spinner spinner1, spinner2, spinner3, spinner4;
private Button convertButton;
private EditText from;
private List <String> list1 = new ArrayList<String>();
private List <String> list2 = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
from = (EditText) findViewById(R.id.amount);
//spinners for units
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner4 = (Spinner) findViewById(R.id.spinner4_main);
List<String>list4 = new ArrayList<String>();
list4.add("Distance");
list4.add("Weight");
//adapter for main scale
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter <String> (this,
android.R.layout.simple_spinner_dropdown_item, list4);
dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner4.setAdapter(dataAdapter4);
//adapter for "from" currency
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list1);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter1);
//adapter for "to" currency
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter2);
Object choice = spinner4.getSelectedItemPosition();
if (choice.equals("Weight")) {
//units to convert from
dataAdapter1.clear();
dataAdapter1.add("Milligrams");
dataAdapter1.add("Grams");
dataAdapter1.add("Kilograms");
dataAdapter1.add("Metric Ton");
//units to convert to
dataAdapter2.clear();
dataAdapter2.add("Milligrams");
dataAdapter2.add("Grams");
dataAdapter2.add("Kilograms");
dataAdapter2.add("Metric Ton");
}
else //(spinner4.getSelectedItem().toString().equals("Distance"))
{
//spinner1 = (Spinner) findViewById(R.id.spinner1);
dataAdapter1.clear();
dataAdapter1.add("Millimeter");
dataAdapter1.add("Centimeter");
dataAdapter1.add("Meter");
dataAdapter1.add("Kilometer");
dataAdapter2.clear();
dataAdapter2.add("Millimeter");
dataAdapter2.add("Centimeter");
dataAdapter2.add("Meter");
dataAdapter2.add("Kilometer");
}
dataAdapter1.notifyDataSetChanged();
dataAdapter2.notifyDataSetChanged();
}`
If anyone could explain what's wrong please enlighten this newbie. =)
U are approaching this wrong. Here is what you should do..
Define Spinner 1, 2 and 3.
Define arrayAdapters for spinner 1,2,3.
Populate ArrayAdapter and define this as the adapter for Spinner 1 (spinner1.setadapter(arrayadapter1)
Then call the spinner1.setOnItemSelectedListener. In the method onItemSelected, populate the logic to populate the array for spinner 2 and spinner 3 as per your requirement. Then call spinner2.setadapter(arrayadapter2) and spinner3.setadapter(arrayadapter3).
This should work.
So initially, spinner2 and spinner3 will not have any content. even if user clicks, there will be nothing in the dropdown. But once Spinner1 is selected by user, spinner2 and spinner3 will have dropdown values.
Please read up on setOnItemSelectedListener. Lots of tutorials are available. You can refer here to get a start.
If this works for you, please accept my answer.
Object choice = spinner4.getSelectedItemPosition();
This can't work.
getSelectedItemPosition()
returns an int (documentation) which won't equal your String as in
if (choice.equals("Weight")
Never ever (except in rare occasions) use the Object class as a reference, as any and every object in Java will be an Object (duh!) so you probably compare apples with oranges (which both are fruits). Seems like you're coming from a more weakly typed language, eh? ;)
Solution: Compare the positions:
if (spinnerX.getSelectedItemPosition == 0)...
Also, are you sure you want to change the content of spinner1 if something is selected on spinner4?

Android ListView: How to add a row with a buttonclick

I have a listview. In it, each row had a text saying 0:00. But now, I added a button on my actionbar but then I got stuck. I don't know how to make the button create a new row, displaying 0:00
This is my code for the data in a row.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RowData rowdata_data[] = new RowData[]
{
new RowData("0:00")
};
RowdataAdapter adapter = new RowdataAdapter(this,
R.layout.listview_item_row, rowdata_data);
listView1.setAdapter(adapter);
listView1 = (ListView)findViewById(R.id.listView1);
}
this is my RowData class:
public class RowData {
String title;
public RowData(String title) {
this.title = title;
}
}
So how should I implement a button click to add another row?
Under addtionbutton: should be the method.
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle presses on the action bar items
switch (item.getItemId())
{
case R.id.additionbutton:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I don't know how to make the button create a new row
With your current solution it's not possible (in efficient way do it) because you're using static array of objects - it means array has fixed size (it means that you're assuming that size won't be changed at runtime).
But your goal is "slightly" different. You want after each click on Button increase number of objects by one. It means you don't know exactly how many rows you can have (or can be changed at runtime).
Due to things mentioned above you should (have to) use dynamic array with changeable size. For this reason you need to use as source of data List that is representation of dynamic array.
Basic algorithm:
Create new List with zero (or you can have by default one row at
start).
Then create public method in your adapter that will add new item to
collection and send request that Adapter should be refreshed (after you added new row into collection).
Assign OnClickListener to your Button and in onClick() method you'll use created method for adding new row into ListView.
Solution:
How to initialise ListAdapter:
// Activity-level variable scope
private List<RowData> items = new ArrayList<RowData>();
private RowdataAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView1 = (ListView)findViewById(R.id.listView1);
// adding first item to List, it's optional step
items.add(new RowData("0:00"));
adapter = new RowdataAdapter(this, R.layout.listview_item_row, items);
listView1.setAdapter(adapter);
}
Method for adding new row into ListAdapter:
public void addRow(RowData newRow) {
// items represents List<RowData> in your Adapter class
this.items.add(newRow);
// sends request to update ListAdapter
notifyDataSetChanged();
}
How to update Adapter after click on Button:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// add new row to Adapter
adapter.addRow(new RowData("0:00"));
}
});
Hope i helped you to solve your problem.
you need to create a dataset that you can change so you need to make your array a class wide variable so you can add to it when you need to
when you add the new row you need to notify the adapter that something changed so you do this
adapter.notifyDatasetChanged();
and that should update your list

Having an issue with a NullPointer and ListView

This is the code which is causing the error. Sorry I changed so much from my original post, but I have placed all the code in one place now. Tell me if this is a terrible idea.
I get my error on the line that sets the myArrayAdapter for my ListView
public class DisplayCaf extends Activity implements OnNavigationListener {
private static final String STATE_SELECTED_NAVIGATION_ITEM = null;
//Holds items.
ArrayList<String> menuArray;
//String adapter for ListView
ArrayAdapter<String> myArrayAdapter;
//set listView
ListView listView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pager_caf);
listView = (ListView)findViewById(R.id.listView);
menuArray = new ArrayList<String>();
//set up the adapter for listView
myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuArray);
//connect adapter to feed info to listView
listView.setAdapter(myArrayAdapter);
}
}
It's hard to tell what the problem is with the limited information you provided: the fact you have posted a lot of code, as you said, doesn't mean you have provided the right one (for instance you pasted that big switch that probably doesn't matter, and you didn't pasted the layout...).
My best guess is that the call to
public Object instantiateItem(View collection, int position);
happens right after the call:
setContentView(R.layout.pager_caf);
and therefore the adapter never gets a chance of being initialized.
If you want to have a clearer picture of what's happening, help yourself first: place a couple of Log.d here and there (right at the start of instantiateItem and before and after setContentView might be a good start...), reduce your problem to the essential parts (bisecting it until only the bug remains) and post the code that remains (as well as the logs).

Dynamic Spinners in Android (General Workflow Question)

Like this one, I've seen a few how-to's on this site, but truthfully, I'm not really getting it.
I want one spinner's content to be based upon a previous spinner selection, like in a States and Cities scenario. In general terms, what is the workflow? Are the results of the second spinner filtered based on the first spinner, or is the second spinner pointing to a completely different list based on the first spinner?
For my own simple learning project, I've built several string-arrays in the strings.xml (AL-Cities, AK-Cities, AR-Cities, etc). I'd like the city spinner to populate from the correct array based on a choice from the state spinner. But I'm wondering if instead I should just have one large multidimensional array of "Cities" that have a state abbreviation as an additional identifier, and then point the second spinner at that using the state abbreviation as a filter. It would seem like the former would provide better performance.
Any help (and code examples) would be greatly appreciated. I'm not new to programming (php mostly, so I guess scripting is more accurate), but I am new to java. My code so far with the spinners not linked is below with the second spinner pointing to an undifferentiated city_array.
Thank you!
public class Example1 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example1);
Spinner spinState = (Spinner) findViewById(R.id.spin_state);
ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(
this, R.array.state_array, android.R.layout.simple_spinner_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinState.setAdapter(adapter3);
Spinner spinCity = (Spinner) findViewById(R.id.spin_city);
ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(
this, R.array.city_array, android.R.layout.simple_spinner_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinCity.setAdapter(adapter4);
}
}
You could try and get the position from your first spinner, the one you've selected and then populate your second spinner after retrieving the proper array based on that position.
You have to listen for your first adapter being changed:
spinner. setOnItemSelectedListener(new MyOnItemSelectedListener());
class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String choice = parent.getItemAtPosition(pos).toString();
populateSecondSpinnerMethod(choice)
}
}
public void onNothingSelected(AdapterView parent) { // Do nothing.
}
}

Categories

Resources