I am trying to define and set this adapter in an oncreate method. The stockArray is an ArrayList defined in the main activity (not the same class). I am getting an error which says my constructor is undefined.
ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(SearchActivity.this, android.R.layout.simple_list_item_1, MainActivity.stockArray);
This is how the array is defined in main activity:
public static AsyncTask<ArrayList<Object>, Void, ArrayList<Object>> stockArray;
you are getting an error because your arrayadapter is of type "ArrayList<Object>" and you are passing an instance of type AsyncTask<ArrayList<Object>, Void, ArrayList<Object>>
Try passing an instance of type ArrayList<Object> to your constructor.
Hope this helps.
stockArray in your case is not an array but this
AsyncTask<ArrayList<Object>, Void, ArrayList<Object>>
In last argument you need to pass an object of type ArrayList<Object>;
If your MainActivity is an Activity object then you can't access an Array in that way you have done. You can keep your Array as an static object in another class , then use in your Activity with static access.
public class Constants {
public static final Object [] myArray = { object1 , object2 } ;
}
then in your Activity
ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(SearchActivity.this, android.R.layout.simple_list_item_1, Constants.myArray);
Hope it helps.
Related
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.
I have to make an application where I have to show a list of names in popup.
I have used array-list to fetch the values from database, but I cannot put it in array-adapter.
here is my code:
public class Calculator_new_Pop extends Dialog implements View.OnClickListener{
... // rest of the code
ArrayList<String> wallAreas=new ArrayList<String>();
wallAreas=GenericDAO.getWallAreas(room_id);//to fetch the values from databases
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(Calculator_new_Pop.this,android.R.layout.simple_list_item_1,wallAreas);
_ltvw.setAdapter(new_adapter);
... // rest of the code
}
the error is
"The constructor ArrayAdapter(Calculator_new_Pop, int, ArrayList) is undefined"
Can anyone help me out?
Use activity context
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(ActivityName.this,android.R.layout.simple_list_item_1,wallAreas);
You can pass the activity context to the constructor of Calculator_new_Pop and use the same instead of Calculator_new_Pop.this
Edit
Context mContext;
public Calculator_new_Pop(Context context)
{
mContext = context;
}
Then
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,wallAreas);
Have a look at the constructor of ArrayAdapter.
http://developer.android.com/reference/android/widget/ArrayAdapter.html
Refer the link
link
ArrayAdapter needs a context as a parameter, you are supplying Dialog instance which is not the Context type, this is the reason why an error is shown. Instead of Dialog instance get the activity context.
You can pass the activity context in the constructor of the dialog, and hence supply that context in the ArrayAdapter.
public class Calculator_new_Pop extends Dialog implements View.OnClickListener{
Context mContext =null;
public Calculator_new_Pop(Context c ){
this.mContext = c;}
.............
.............
.............
ArrayList<String> wallAreas=new ArrayList<String>();
wallAreas=GenericDAO.getWallAreas(room_id);//to fetch the values from databases
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,wallAreas);
_ltvw.setAdapter(new_adapter);
.......
}
When instantiating dialog from activity, pass this.
i am getting 'null' in "mContext"
so i tried this..
public Calculator_new_Pop(Activity parent) {
// TODO Auto-generated constructor stub
super(parent);
this._act = parent;
//
}
_ltvw.setAdapter(new ArrayAdapter<String>(_act,android.R.layout.simple_list_item_multiple_choice,data));
it worked..
I am trying to pass the complete arraylist from one activity to another.
i have tried like this way..
arraylist=new ArrayList<HashMap<String,Object>>();
Intent i= new Intent(ListActivity.this,search.class);
i.putExtra("arraylist", arraylist);
startActivity(i);
Could somebody help me out #thanks
This will not work because the Object class in Java is not serializable. See this question for an explanation as to why.
The Intent.putExtra() method requires a type that implements the serializable interface, Object does not implement this so consequently it will not work. I would suggest rather than having a HashMap<String,Object> you replace the Object with a more specific type that implements the Serializable interface. See this tutorial for how to do this.
UPDATE
If the data you are passing is large there could be a fairly significant overhead associated with serializing and deserializing. Consequently it might be worth using a Static Singleton class to store the arraylist. The code sample below shows how you could implement this:
public class DataStore {
private static final DataStore instance = new DataStore ();
private arraylist = new ArrayList<HashMap<String,Object>>();
//Private constructor
private DataStore () {}
//Class is only accessible through this method
public static Singleton getInstance() {
return instance;
}
//Accessors for your data
private ArrayList<HashMap<String,Object>> getArrayList()
{
return arraylist;
}
private void setArrayList(ArrayList<HashMap<String,Object>> value)
{
arraylist = value;
}
}
For reference here is a tutorial on static singletons.
textview and button in my listview by using adapter class. When i click on that button i have to call AsyncTask passing parameters i.e String of that perticular position in adapter class getview method .Here am created my Asynctask is another class i.e an activity class. Please provide some examples.
Thanks in advance.
Your AsycnTask takes an Array of some sort - Strings for instance, so when you instantiate the AsyncTask, you just pass it an Array like so:
String[] arr = new String[] {"A string to pass..."};
MyAsyncTask task = new MyAsyncTask();
task.execute(arr);
Full example on how to use it:
http://www.android-ever.com/2012/10/android-asynctask-example.html
Create public class in different file or within your activity. If you are creating within Activity then define like this way
public class static MyAsync extends AsyncTask<String, Void, String>{
}
and then used anywhere like this
YourActivity.MyAsync myAsync = new YourActivity.MyAsync();
for passing the value to the your Async class use this way
myAsync.execute(yourstring);
access in doInBackground like this way
public String doInBackground(String... param){
String s = param[0]; // here you can access you string like this way
}
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.