Cannot resolve symbol 'createFromResource' - android

I'm completely confused as to what this means. Ive done some searching but so far havent found any help.
I'm using android studio and have a dialog fragment I'm setting up a spinner in
Spinner systemFontSpinner = (Spinner) view.findViewById(R.id.system_font_spinner);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter.createFromResource(getActivity(),
R.array.system_fonts, android.R.layout.simple_spinner_dropdown_item);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
systemFontSpinner.setAdapter(arrayAdapter);
createFromResource is highlited in red and when I hover it it says "Cannot resolve symbol 'createFromResource'"
my array
<resources>
<string-array name="system_fonts">
<item>Helvetica</item>
<item>Helvetica-Neue</item>
<item>Impact</item>
</string-array>
</resources>
my layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/system_font"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" />
<Spinner
android:id="#+id/system_font_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"/>
Any help would be greatly appreciated, thanks!

Just so Spidy don't take all the glory :)
You're calling a static method that will return you an arrayadapter so you shouldn't be using new because you're not instantiating anything.
You should be doing it this way:
ArrayAdapter<String> arrayAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.system_fonts, android.R.layout.simple_spinner_dropdown_item);
For reference: http://developer.android.com/reference/android/widget/ArrayAdapter.html#createFromResource(android.content.Context, int, int)
Now I shall take all the glory :)

Related

TwoWayView setAdaptor is not allowing to set the adaptor(HorizontalListView)

I tried using TwoWayView. My xml contains
<org.lucasr.twowayview.TwoWayView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rewardListView"
style="#style/TwoWayView"
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
tools:context=".RewardActivity" />
I have created the Adapter which extends RecyclerView.Adapter and properly implemented every methods. Now in the activity, on the instance of TwoWayView, I try to set the adapter, but its not allowing me to set it up.
mRewardListView = (TwoWayView) findViewById(R.id.rewardListView);
adaptor = new RewardListViewAdaptor(mRewardList, this);
mRewardListView.setAdapter(adaptor);
I am getting this error "setAdapter(android.widget.ListAdapter) in TwoWayView cannot be cast to RewardListViewAdaptor".
Any help will be highly appreciated.

Android - How to use custom spinner

I have extended the spinner class in order to customize it, but i can't figure out how to use it in my code.
public class mySpinner extends Spinner {
......
}
I have an existing activity that uses a spinner and I want to change it to use mySpinner class.
Originally:
Spinner spinner = (Spinner) textEntryView
.findViewById(R.id.account_spinner);
and
<Spinner
android:id="#+id/account_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/ADALabel1"
android:layout_alignLeft="#+id/newassetsymbol"
android:layout_below="#+id/assetclass_spinner" />
first I tried:
mySpinner spinner = (Spinner) textEntryView
.findViewById(R.id.account_spinner);
compile error (makes sense).
then I tried:
mySpinner spinner = (mySpinner) textEntryView
.findViewById(R.id.account_spinner);
runtime cast error.
Then I changed the layout with the previous line:
<mySpinner
android:id="#+id/account_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/ADALabel1"
android:layout_alignLeft="#+id/newassetsymbol"
android:layout_below="#+id/assetclass_spinner" />
01-12 06:50:02.664: E/AndroidRuntime(597): Caused by: java.lang.ClassNotFoundException: android.view.mySpinner in loader dalvik.system.PathClassLoader[/data/app/org.sample.sample-1.apk]
This does not seem thst hard but I am missing something. Can someone help?
Thank you.
When using custom View components in XML, you need to use the complete class path.
<com.example.appname.mySpinner />

Android ArrayAdapter/ListView does not update in Fragment

I am facing a problem while updating a ListView via an ArrayAdapter in a Android Fragment.
I have a method handling a .csv that is retrieved from a server. It parses the .csv and does the following:
for(int i =0; i< lines.length; i++){
String[] words = lines[i].split(",");
for(int j = 0;j<words.length; j++)
if ( j%6 == 1 || j%6 == 2)
getArrayAdapter().add(words[j]);
Using breakpoints, I can see that "words[j]" is a valid String. The method getArrayAdapter is the following:
private ArrayAdapter<String> getArrayAdapter(){
if (aa == null){
ListView lv = (ListView) getCurrentActivity().findViewById(R.id.list);
aa = new ArrayAdapter<String>(
getCurrentActivity(),
android.R.layout.simple_list_item_1);
aa.setNotifyOnChange(true);
lv.setAdapter(aa);
}
return aa;
}
If the "getArrayAdapter().add(words[j]);" is called, method "notifyDataSetChanged()" in ArrayAdapter is called, as it should:
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
mNotifyOnChange = true;
}
Via its parent it reaches DataBaseObservable that does this:
public void notifyChanged() {
synchronized(mObservers) {
// since onChanged() is implemented by the app, it could do anything, including
// removing itself from {#link mObservers} - and that could cause problems if
// an iterator is used on the ArrayList {#link mObservers}.
// to avoid such problems, just march thru the list in the reverse order.
for (int i = mObservers.size() - 1; i >= 0; i--) {
mObservers.get(i).onChanged();
}
}
}
The array of observers had an "AdapterDataSetObserver" that contains all kind of references to my adapter and ListView. I looked what happens in the onChanged() of AdapterDataSetObserver, but I do not really know what to look at.
My table_fragment.xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/top">
<ListView android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="2"
android:numColumns="2"
android:columnWidth="30dp"
android:id="#+id/list">
</ListView>
</LinearLayout>
The xml of the activity containing the Fragment looks like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<fragment android:name="com.example.TableFragment"
android:id="#+id/table_fragment"
android:layout_weight="0"
android:layout_width="match_parent"
android:layout_height="0dip" />
</LinearLayout>
When looking in the log, nothing strange is there. All seems to go fine, but the screen does not show any of the strings.
What am I missing here? Probably missing something trivial, since I am a newbie to Android.
android:layout_height="0dip"
this is wrong. You should let get the ListView all the available space. Change it in fill_parent
then:
aa = new ArrayAdapter<String>(
getCurrentActivity(),
android.R.layout.simple_list_item_1);
you need to submit to the Apdater the dataset you want to show. For this purpose you should use this constructor
It turns out that layout_height of the ListView can be 0 (it auto expands). However, in the xml of the activity that contains the fragment, the layout_height could could not be set to 0 (it does not auto expand). I had put that on 0dip as well (after suggestion of Eclipse). Changing to "match_parent" did the job (despite the Eclipse warnings).
#Jos, the answer you selected is not the correct answer. The ListView height can, and should be, 0dp. In order for the ListView to function in its default manner you need to change the Id of the ListView to exactly android:id="#id/android:list" and you can also add a TextView or ProgressBar to show when the list is empty, also the default function of ListView, something like the following:
<TextView
android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="#+string/empty_list_message" />
Again the id must match exactly, everything else can change to suit your needs.
Here is what the complete xml would look like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:listSelector="#drawable/list_selector"/>
<TextView
android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="#string/empty_list_message" />
</LinearLayout>
The layout_weight and listSelector are particular to my program, you may or may not need these.

Android: Listview NullPointerException

I've written a simple android list view, but it is throwing a java.lang.NullPointerException during run time. Checked it in debug mode and it seems the findViewbyId is returning null.
So when I get to the setAdapter it throws the exception. But the list does exist in the R.java file and the main.xml. How can I fix this?
setContentView(my.namespace.R.layout.main);
String[] a={"asd","asdsad"};
ListView lt;
lt = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,my.namespace.R.layout.rowitem, a);
lt.setAdapter(adapter);
Main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Is your activity extends ListActivity? You dont need a separate layout if you extend ListActivity. See this tutorial for further reference.
Wirte this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,my.namespace.R.layout.simple_list_item_1, a);
Refer THIS

Android AutoCompleteTextView onClick problem

I've created an AutoCompleteTextView to search through a list of course titles (obtained from an sqlite db) and what I want to do is, when the user clicks on a title from the drop-down menu, the whole information from the database about his selection appears in a text view created below the AutoCompleteTextView.
I am pretty new to programming, especially for android and I would really appreciate it if someone could explain me how exactly to use setOnItemClickListener to call an instance in the database in the TextView below.
The code for the layout (R.layout.main_courses) is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<AutoCompleteTextView
android:id="#+id/autocomplete_course"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search for a course"/>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/autocomplete_course"
android:hint="Information about the course will appear here" />
</RelativeLayout>
and the code for the AutoCompleteTextView I've written so far is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_courses);
DataBase db = new DataBase(this.getApplicationContext());
db.openDataBase();
ArrayList<String> aCourses = db.getCoursesArr();
db.close();
AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.autocomplete_course);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_courses, aCourses);
search.setAdapter(adapter);
}
First of all you should try using a CursorAdapter instead of getting an array from it. Check this link for more info.
There is a method in AutoCompleteTextView that let you decide how many letters the user must type before the dropdown is shown, setThreshold. The issue is that it only allows >=1 values.
If you check this class src code, the good news is that the variable set by setThreshold() is only used in this method:
public boolean enoughToFilter() {
return getText().length() >= mThreshold;
}
So the first thing I would try is extending AutoCompleteTextView and override that method to always return true.
NOTE: Keep in mind that this might change in the future and it can get broken.

Categories

Resources