Populating a Spinner from a static Array - android

I have a static array, and i need to populate the spinner from this array but im getting this error.. "Cannot resolve Constructor..." This is my code..
protected void onPostExecute(String e) {
super.onPostExecute(e);
Spinner spinner = (Spinner)findViewById(R.id.docSpinner);
spinner.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,array));
}
}

The first argument in the ArrayAdapter constructor takes a Context argument, but judging from your question you are calling this constructor inside an AsyncTask, which (unlike, for example, Activity) does not inherit from Context.
If your AsyncTask is a non-static class nested inside a class which extends any sort of Activity e.g Activity , AppCompatActivity etc , you can use ActivityName.this. Otherwise, you will need to find some way to get a Context to your AsyncTask.

Related

RunOnUiThread in static method

How can I use RunOnUiThread in a static method within an Activity to be accessed by another class ?
In MainActivity i have a method for update listview with values from class UtilDoc.cs.
The error:
Error CS0120: An object reference is required for the non-static field, method, or property 'Android.App.Activity.RunOnUiThread(System.Action)' (CS0120) (MyProject)
MainActivity.cs:
public static void UpDateValue(string value)
{
RunOnUiThread(() => {
//updateListview
});
}
UtilDoc.cs
public void SendValueListView()
{
string value = GetValue();
MainActivity.UpDateValue(value);
}
The short answer is you can't.
Instead you could (but probably shouldn't) make UpDateValue(string value) a non-static method and provide UtilDoc with a reference to MainActivity so that you can call UpDateValue.
The better solution would be to create an Adapter for the ListView and tell that to update the data.
For more information on ListView and Adapters: http://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/

how check doinbackground(Void.. ar) method is completed in android

I am creating a program that fetch the value from online . I have use AsynTask class for background process. i am fetching the some values from web and set value in class variables.My problem is that values are setting in variables in very late. and my execution processed further and find variables values null. How can i check that doInBackground() method is processed completely and values are sets in variable. so that i can use these variables values.
One of the best method is to use a kind of observer design pattern.
So first create an Interface with contain a method (depend on your return value, etc) :
public interface OnTaskCompleted{
void onTaskCompleted(String xml);
}
Then you need to implement this interface to your activity wich instantiate your asynctack, see:
public class MainActivity extends SherlockFragmentActivity implements OnTaskCompleted
Then to finish, you gonna pass the instance of MainActivity (wich by polymorphisme is also OnTaskCompleted) to your asyntask. Here my asyntask class handler is named XMLParser
public class XMLParser {
private OnTaskCompleted listener;
public XMLParser(OnTaskCompleted mainActivity, String url){
getXMLFromURL(mainActivity,url);
}
[......]
//here when the work is done you call the OnTaskComplete Method
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//CALL HERE
listener.onTaskCompleted(result);
}
}
I hope it'll help you. Works like a charm for me.

How to set a TextView from another class (not extending to Activity.class)

I have a problem about setting a TextView from a class which is not a child class of Activity. This class is basically used for handling registration and REST request with 3rd party server.
After getting textfield info from 3rd Party server, it is too late to set TextView in the Main Activity.
I can't use SharedPreferences to set this info, because MainActivity has already started.
I can't pass this info with Bundle since my java class is not an activity class.
How can I pass this info and set the TextView in the MainActivity? Is there any way to do this?
The proper way of doing this this is to create a listener.
Create an interface :
public interface OperationCompletedListener{
void onOperationCompleted(String resultValue);
}
Then in your class which calls Rest services, create a variable for this listener and a method to set it.
private OperationCompletedListener mListener;
public void setOperationCompletedListener(OperationCompletedListener listener){
mListener=listener;
}
Then when the your rest service completed call like below :
if(mListener!=null){
mListener.onOperationCompleted("your value to be passed");
}
Then in your activity class which contains the TextView, create an object of OperationCompletedListener and set it to the other class using the set method that we created earlier. Then in the onOperationCompleted method, set the text view with your value and you are done.
private OperationCompletedListener mOperationCompletedListener=new OperationCompletedListener() {
#Override
public void onOperationCompleted(String resultValue) {
yourTextView.setText(resultValue);
}
};
restServiceClassObject.setOperationCompletedListener(mOperationCompletedListener);
You can create an static method which update textview in your activity class . Then call this method from your other class whenever you want.
Try to pass the Activity to the non-Activity class when you instantiate it. For example:
public class NonActivityClass {
private Activity parentActivity;
public NonActivity(Activity parentActivity) {
this.parentActivity = parentActivity;
}
}
Or you can just pass the Activity to a static method in your NonActivityClass if you don't want to instantiate it (it's abstract). Then, you can inflate the TextView or do a findViewById from the parent and set the text.
From my experience, you should never use a static non-final variable to maintain a reference across activities. When you restart the app or the phone, or when Android kills your app's process, the reference and state of the variable becomes lost and may cause your app to crash.

Concept of introduce class, object?

i'm new here so still very blur with some certain things here.
& i'm a bit confuse with following codes.
public class SmsActivity extends ListActivity {
private String[] mSmsReceiver;
public SmsActivity(){
mSmsReceiver = new SmsReceived();
setListAdapter(new ArrayAdapter<String>(this, R.layout.main,mSmsReceiver));
my understanding: (should be wrong)
line 1: Class SmsActivity under a superclass ListActivity
line 2: i introduce a string array term name:mSmReceiver
line 3: calling method SmsActivity()
line 4: inside SmsActivity method, mSmsReceiver(a string array) call method SmsReceived
line 5: ArrayAdapter(in string form, loaded with the info. of mSmsReceiver) loaded into setListAdapter
My question:
pls correct my understanding upon code above.
line 5, what is this refers to?
(i checked on internet & books, it always says context. but i'm totally no idea what is context exactly means, anyone can explain what is context refering here?)
full codes:
import...
....
public class SmsActivity extends ListActivity {
private String[] mSmsReceiver;
public SmsActivity(){
mSmsReceiver = new SmsReceived();
setListAdapter(new ArrayAdapter<String>(this, R.layout.main,mSmsReceiver));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
//---method is call when listitem is clicked---
listView.setOnItemClickListener(new OnItemClickListener() {edit later});
}
private class SmsReceived extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{..... }
}
}
Basically this is a definition of a class named SmsActivity.
You are right about line 1 and line 2. More precisely, mSmReceiver is a private number of class SmsActivity.
Line 3 should be the constructor which I am not sure because I'm not an android developer and I heard it use onCreate instead in Activity. But anyway it wouldn't be calling the method just definition of it. The constructor will be used to initialize the class.
And line 4 mSmsReceiver(a string array) call method SmsReceived. Not the case, it would be initialize mSmsReceiver with an object, which is an instance of class SmsReceived.
Line 5 this refers to the class SmsActivity. In classes this almost always refers to the class it's in. And this provide a context so you can use this.someMumber or this.someFunction.
The keyword "this" in Java is basically a reference to the Class that its in. For example:
public class MyClass {
MyClass myVar = this;
}
This will put an instance of the class MyClass in that variable. It gives you an instance of whatever class your in. If you call it in a method:
public void myMethod() {
MyClass m = this;
}
This will give you an instance of whatever class invoked myMethod. Weather its an instance of MyClass or an instance of a subclass of MyClass. Whatever instance used to invoke the method will be placed in the m variable.
So when you call "this" in an Activity it gives you an instance of that Activity.

How to save List<UserDefinedClass> of between Activity switching?

How to save List<UserDefinedClass> of between Activity switching ? I get data in onClickView function. Does UserDefinedClass need to implements some interface ?
I believe the best way would be to keep the list in the Application context.
In any of your activitys call yourApp using:
((yourApp)getApplication()).//any public function
public yourApp extends Application{
private List<UserDefinedClass> yourList;
//create functions to manipulate the list example:
public void add(UserDefinedClass a){
//add this to list
}
//finally function to access list
public List<UserDefinedClass> getList(){
return yourList;
}
}
//example:
List<UserDefinedClass> aList = ((yourApp)getApplication()).getList();
It is best to only pass an identifier to the next Activity instead of passing your entire list.
UserDefinedClass should not implement some interface, rather UserDefinedClass should be the class type of the List. In other words, every item in your list should be of UserDefinedClass. That can be a String or even a custom class.
Yes you can pass it if it implements Parcelable. However if its a large amount of data a database or file may work out better for you.

Categories

Resources