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..
Related
I would like to call a method in an activity and pass an argument to it from a non activity regular class in android.
As i understand, i cant simple use the following code, plus it does not work:
int mySound = 0;
SoundsActivity soundsActivity = new SoundsActivity();
soundsActivity.playSound(mySound);
That code is located in a regular class called "MyAdapter".
There are a few ways you can do this. I can't be specific since you didn't really show any code.
You can't do what you're trying to do though. Activities can't be instantiated like that (as well as anything extending Context), and it won't do what you want.
Use a broadcast.
This will require that you have a Context object passed into your Adapter, which you can do simply by modifying the constructor and adding a global variable:
private Context context;
public MyAdapter(Context context) {
this.context = context;
}
Then you can use that Context to send a local broadcast with your own action:
Intent intent = new Intent("my_custom_action");
intent.putExtra("sound_type", 0);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
And receive that action in your Activity to call your method: See Context-registered Receivers
When you construct the Adapter, pass a Context object into it. If you're constructing from an Activity (hopefully SoundsActivity), use this:
MyAdapter adapter = new MyAdapter(this);
Use a callback.
Delcare an interface somewhere:
public interface AdapterCallback {
void onRequestPlaySound(int type);
}
Implement that interface in your Activity:
public class SoundsActivity extends Activity implements AdapterCallback {
//...
#Override
public void onRequestPlaySound(int type) {
playSound(type);
}
//...
}
Add the interface as a parameter in your Adapter's constructor:
private AdapterCallback callback;
public MyAdapter(AdapterCallback callback) {
this.callback = callback;
}
And then use callback.onRequestPlaySound(0); from wherever you need.
When you construct the Adapter, pass your SoundsActivity instance into it. This will only work if you're constructing the Adapter from SoundsActivity:
MyAdapter adapter = new MyAdapter(this);
Pass SoundsActivity directly.
This isn't the cleanest way, nor is it the recommended way, but it will work. In your Adapter:
private SoundsActivity activity;
public MyAdapter(SoundsActivity activity) {
this.activity = activity;
}
And from SoundsActivity:
MyAdapter adapter = new MyAdapter(this);
Then just call activity.playSound(0); where you need to.
I need to start AsyncTask in UI thread, but the Constructor has (MainActivity parentActivity)
parametr. I don't really understand why it should be implemented and how I must pass it.
Here Eclipse says "Cant resolve MainActivity to a variable." Same for Activity.MainActivity.
new DownloaderTask(MainActivity).execute();`
And the constructor.
public DownloaderTask(MainActivity parentActivity) {
super();
mParentActivity = parentActivity;
mApplicationContext = parentActivity.getApplicationContext();
}
Change this line...
new DownloaderTask(MainActivity).execute();
to this...
new DownloaderTask(MainActivity.this).execute();
And you are passing Context of MainActivity not the activity...so in DownloaderTask() constructor, the parameter will be Context type not MainActivity...The constructor should look like as below...
public DownloaderTask(Context context) {
super();
mApplicationContext = context;
}
you can call like following if you are calling directly from the MainActivity
new DownloaderTask(this).execute();
or if you are callling from an inner class you can call like
new DownloaderTask(MainActivity.this).execute();
VPAdapter.java
public class VPAdapter extends PagerAdapter
{
public static String[] titles;
public final Context context;
public int[] scrollPosition;
JSONArray categories = null;
JSONArray newstype = null;
JSONObject json;
DatabaseHandler db = new DatabaseHandler(context)//error:The blank final field context may not have been initialized
...
}
DatabaseHandler.java
public class DatabaseHandler extends SQLiteOpenHelper {
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
On VPAdapter.java I wanted to access DatabaseHandler anywhere, but there is problem with the constructor. What is the proper way I should write them?
You cannot pass a variable that was not initialize.
On your second line of the function you declare the context variable but you don't assign any value to it.
The last line should be written in the constructor of VPAdapter. The constructor should get a context variable. When you call your constructor you probably want to use the application context, but you might send also an activity (Activity inherit from context) but this is usually not recommended (But it really depends on your code)
Because your Context is null first initialize your context than you can pass that context to your database handler constructor.
Context context = getApplicationContext();
Or try below code
For example initialize your Context with your activity context.
Create constructor of your APAdapter class and call that constructor from your activity. Same way as you create for database handler.
public APAdapter(Context context) {
this.context = context;
}
than pass that context to your database handler.
I have class MyCustomAdapter
public class MyCustomAdapter extends SimpleCursorAdapter{
}
I have other class called DbAdapter, where I have functions with sql query. I'd like to access functions from class MyCustomAdapter
DbAdapter db and next I have to add
db = new DbAdapter(this)
but it doesnt work. I tried
Contex contex
db = new DbAdapter(contex) but then I have java.lang.NullPointerException
Is any way to access my DbAdapter within MyCustomAdapter?
Make your MyCustomAdapter-class accept a Context-object as a parameter:
public class MyCustomAdapter extends SimpleCursorAdapter{
private final Context context;
public MyCustomAdapter(Context c){
this.context = c;
}
}
In the calling activity, you can then simply pass this for the context.
You're getting this error because the DVM tries to invoke a SimpleCursorAdapter(Context)-constructor, which does not exist.
The solution to your problem is either explicitly calling a valid constructor of SimpleCursorAdapter by using super(...) or making the MyCursorAdapter-constructor accept the same parameters as one of the SimpleCursorAdapter-constructors.
What way to go relies on what you want to do with your Cursor-adapter. If you have constant values for certain constructor-arguments, you can use those, if you don't, you'll need to include them in your MyCursorAdapter-constructor.
As an example, this assumes you don't have constant values for any constructor-argument:
public class MyCustomAdapter extends SimpleCursorAdapter{
private final Context context;
public MyCustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to){
super(Context context, int layout, Cursor c, String[] from, int[] to); // Call the super-
// classes constructor
this.context = context; // Save the context for further use
}
}
Some further information on this might be found here.
hi i am Using ArrayAdapter in ListView with custom Class object,HERE IS MY CODE
private static class NewsDetailAdapter extends ArrayAdapter<clsNewsItem>
{
private final Activity context;
List<clsNewsItem> newsList = null;
public NewsDetailAdapter(Activity context, ArrayList<clsNewsItem> clsNewsObjects) {
super(context, R.layout.listview_cell, clsNewsObjects);
this.context = context;
this.newsList = clsNewsObjects;
}
public void clear()
{
newsList.clear();
}
while i am working with this code AdapterObj.NotifyDatasetchanged() not working Due to i have not implemented addAll() method for this class,i cant understand how to write this Method so how can i Write Add All method for this ArrayAdaper class..can Any one help me please
Do you have specific need for subclassing? If you need to have some logic better have one ArrayAdapter as member instance and proxy its methods. Because the original ArrayAdapter already has all these methods readily available