Having an issue with a NullPointer and ListView - java

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

Related

When press back button listview going top. How to fix that?

In my codes there is two fragment. List page and detail page. There is lot of alphabetic item on list page. And when i clicked some item opening the detail page. But when i press back button, the list doesn't stay same position. It goes to the top. I searched some solution but i think that's not common issue. I couldn't find different answers. Is there anyone to help me?
At first glance, it looks like it's one of your calls to: dictionaryFragment.resetDatasource(source);
Which is resetting the Adapter on your ListView to a brand new ArrayAdapter. This will reset the ListView to the top because the adapter is an entirely new object. The ListView no longer has any idea about the old ArrayAdapter and thus resets to the top.
public void resetDatasource (ArrayList<String> source){
mSource=source;
adapter = new ArrayAdapter<String>(getContext(),R.layout.kelimelistesi, mSource);
dicList.setAdapter(adapter);
}
Rather than calling
adapter = new ArrayAdapter<String>(getContext(),R.layout.kelimelistesi, mSource);
dicList.setAdapter(adapter);
You should do more like this:
public class DictionaryFragment ... {
private Adapter mAdapter;
public View onCreateView(...) {
// Init the layout here
// Put some other ArrayList here if you have one initially, else an empty one to start
mAdapter = new ArrayAdapter<String>(getContext(), R.layout.kelimelistesi, new ArrayList<String>());
dicList.setAdapter(mAdapter);
}
public void resetDatasource(ArrayList<String> source) {
mAdapter.clear();
mAdapter.addAll(source);
mAdapter.notifyDataSetChanged();
}
...
}
This difference now is that the same mAdapter instance is bound to dicList the whole time, but only the underlying data to display changed. Think of a ListView/RecyclerView's Adapter as a class that takes some input data and binds it to some UI elements. In this case, each String in the ArrayAdapter is displayed in some UI element like a TextView or what not.
ALSO important: note that we call notifyDataSetChanged() AFTER we have cleared the old data and added all the new data. This will help avoid flickering and scrolling issues from trying to elements one at a time.

Cannot resolve constructor 'ArrayAdapter(anonymous android.widget.SeekBar.OnSeekBarChangeListener, int, java.util.ArrayList<java.lang.Integer>)

I have a misunderstanding of how to dissect this error message. I know there are simpler and more elegant ways of doing this but I just wanted to do it this way to aid understanding. We are making an app where the seekbar is used to complete a times table. Sorry to dump a bunch of code but I just cant understand what it is trying to tell me.
We already made an audio player seekbar and an arraylist for family members names. I am trying to use that limited experience to create this app. Why is this not working?
The simpler the explanation the better. At this stage it is impossible to be patronising.
Ive read similar questions but its not particular to this problem. For example cannot resolve constructor 'arrayadapter(android.widget.adapterview.onitemselectedlistener, int, java.util.arraylist)'
Here is my code:
//Define Listview and Seekbar in code
final ListView listView = (ListView) findViewById(R.id.myListView);
SeekBar seekBar = (SeekBar) findViewById(R.id.mySeekBar);
//Set max and min Seekbar and get int
seekBar.setMax(20);
seekBar.setMin(1);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//Create Arraylist
ArrayList<Integer> arrayList = new ArrayList<>();
for (int i = 1; i < 15; i++){
arrayList.add(i * progress);
//create adapter and set listview
ArrayAdapter arrayAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
}
new SeekBar.OnSeekBarChangeListener is an anonymous inner class and calling this inside the class references to the SeekBar.OnSeekBarChangeListener class instead of your Activity class.
The first parameter here for the ArrayAdapter is expected to be a Context, but in your case you are passing your OnSeekBarChangeListener instead. So you need to use Activity.this rather than this.
You are using incorrect context as while creating ArrayAdapter as this in below code will refer to SeekBar and not Activity. Use YourActivity.this instead of using only this.
ArrayAdapter arrayAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_list_item_1, arrayList);

Android Studio ListView Custom

I recently started developing for android and now im stuck! I created a listview but the standard colour is black, now i want to be able to show the text in whatever colour i want. This is the activity.
public class DisplayMalePage extends ActionBarActivity {
String[] maleArray = { "a","b","c"};
private ListView maleListView;
private ArrayAdapter maleArrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_male_page);
maleListView = (ListView) findViewById(R.id.maleList);
maleArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, maleArray);
maleListView.setAdapter(maleArrayAdapter);
}
But this (i think due to simple_list_item_1) gives me a black colour. Also i would like the input of my array to be strings so it wil be easier to change language.
Eventually i would like to have a list with 2 top texts, a dividing bar and then the rest of the list (they will all be clickable).
I hope someone understands what i mean haha.
You cannot change any part of the listviews layout using the default ArrayAdapter. You need to define your own CustomArrayAdapter.
http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown
This link is a very useful guide on how to do that.
Create your own layout which you will use in your list item. Make sure it contains a Textview with id text1. Something like this:
layout/my_list_item.xml
<TextView
android:id-"#+id/text1"
android:textColor="#android:color/white"
...... />
Then use this layout in your ArrayAdapter intialization instead of android.R.layout.simple_list_item_1.
maleArrayAdapter = new ArrayAdapter(this, R.layout.my_list_item, maleArray);
Note: If you want something more complex other than showing a simple text, you should use custom ArrayAdapter, as #user3567040 have pointed out.

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.
}
}

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

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.

Categories

Resources