I have a mapview, with itemizedoverlays, exactly like in the example of android developers guide: http://developer.android.com/resources/tutorials/views/hello-mapview.html
on the itemizedoverlay, i have a personalized dialog, with a button. all fine until here, but now i have problems trying to add the functionality to the button. I need that the button start's a new activity, but i can't achieve that.... ¿why? because on this line: i = new Intent (NyItemizedOverlay.class, Locate.class); i have the current Intent class as first parameter, and the target intent class at second parameter.
MyItemizedOverlay is not a Intent class... it's ItemizedOverlay extension, then it doesn't compile when i try to launch the intent, i am trying to pass a normal class as first argument, and it needs an intent class. I have to put the launcher class, but the launcher class is not an intent :S
If i try to put another intent class on the first argument, i got this error: No enclosing instance of the type AllActivity is accessible in scope .... (AllActivity is a public activity class of my app)
How i can solve this?
full code here:
public class MyItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private ArrayList<String> permissions = new ArrayList<String>();
private Context mContext;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
public int size() {
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void addOverlay(OverlayItem overlay,String permission) {
mOverlays.add(overlay);
permissions.add(permission);
populate();
}
public MyItemizedOverlay(Drawable defaultMarker, Context context) {
//super(defaultMarker);
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void clear()
{
mOverlays.clear();
permissions.clear();//lista de permisos de cada usuario, ya que hay dos campos, el email (snippet) y el permission, una lista.
}
protected boolean onTap(int index) {
try{
OverlayItem item = mOverlays.get(index);
if (permissions.size()==0)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
}
else
{
//set up dialog
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.personal_dialog);
dialog.setTitle(item.getTitle());
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
//set up text
TextView DialogEmail = (TextView) dialog.findViewById(R.id.DialogEmail);
TextView DialogPermission = (TextView) dialog.findViewById(R.id.DialogPermission);
DialogEmail.setText(item.getSnippet());
DialogPermission.setText(permissions.get(index));
final String userName=item.getTitle();
final String email=item.getSnippet();
final int cont=index;
//set up button
Button button = (Button) dialog.findViewById(R.id.DialogButton);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do something
Bundle bundle = new Bundle(); //bundle is like the letter
bundle.putString ("user",userName ); //arg1 is the keyword of the txt, arg2 is the txt
bundle.putString ("email", email);
bundle.putString ("permission", permissions.get(cont));
Intent i=null;
i = new Intent (MyItemizedOverlay.class, Locate.class);
i.putExtras(bundle);
startActivity(i);
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
}catch(Exception e){}
return true;
}
}
The Intent javadoc clearly show that the first arg has to be a Context (extended by activity) and the second one the class of the activity you're trying to launch. In your case, you'll need to do :
Intent intent = new Intent(mContext, AllActivity.class);
mContext.startActivity(intent);
generally, Intent gets in one of its constructor Context as first parameter and an Activity class as the second one. So, do the following:
Intent intent = new Intent(mContext, AllActivity.class);
mContext.startActivity(intent);
To an intent constructor it should pass A Context of the application package implementing this class, check this. it is not the intent.
It must be some Activity, say MyClass that using your MyItemizedOverlay class. Best thing to do is declare a static Context say myContext there and declare its value as myContext = MyClass.this; in onCreate. Then you can declare intent like this
new Intent (MyClass.myContext , Locate.class);
Related
I want create list menu simple : About , Contact us And more ( about 30-40 button )
After click on this buttons , i want start new activity
I want this code in use class and out of mainactivity
My xml code : http://i.stack.imgur.com/3moSl.png
clcAbout , clcContact and clcFB my buttons create by linearlayout
Thx
Is this what you are looking for?
public void setUpButtons(final Context context){
Button clc = (Button) findViewById(R.id.clcFB);
clc.setOnClickListener(new MyOnClickListener(0, this));
// etc...
}
private class MyOnClickListener implements OnClickListener{
private int index;
private Context context;
public void MyOnClickListener(int index, Context context){
this.index = index;
this.context = context;
}
#Override
public void onClick(View v){
Intent intent;
switch(index){
case 0:
intent = new Intent(context, MyActivity0.class);
break;
case 1:
intent = new Intent(context, MyActivity1.class);
break;
// etc...
}
if(intent != null){
context.startActivity(intent);
}
}
}
If the problem is the lack of a context variable for creating the intent, you have to previously pass a context (as a parameter for the constructor for example) to that class out of mainactivity then use that context variable in the Intent constructor, like this:
Intent intent = new Intent(mycontext, (activity class name).class);
mycontext.startActivity(intent);
Edit: For OnClickListeners (I'm not sure I know what you want to do though):
Intent intent = new Intent(MainActivity.this, (activity class name).class);
MainActivity.this.startActivity(intent);
I am new to android and I need a small help. I am developing an application much like google maps. I am able to create a map interface and display markers along with on tap(int index) method to show the text when clicked on the markers. Now I need to start another activity when the text (which comes after clicking on the marker by using ontap method) is clicked much like how we see reviews and other things about a location in google map. I searched and found an implementation of some balloonitemized overlay class.
My question is whether there is another way to do the same thing without using this balloonitemized class?
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem>{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
You could do something like this:
LayoutInflater cancelInflater = getLayoutInflater();
dialogView = cancelInflater.inflate(R.layout.DIALOG, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setView(dialogView);
dialogView.setOnClickListener(mViewClickListener);
You'll need to create a layout file for your dialog, R.layout.DIALOG
You can then findViewById() for your separate widgets in your layout and populate them accordingly.
Then create an onClickListener to do whatever needs to be done when the dialog is clicked.
I'm trying to add a dialog box which will pop up when a user selects a delete button which will delete a specified row. However I can't seem to get the appropriate syntax for the program to compile. The error occurs at this line;
MODULEDATABASE.deleteRow(rowId);
Intent intent = new Intent(this, MyCourses.class);
Any suggestions would be much appreciated.
public class ViewCourse extends Activity implements OnClickListener{
Cursor cursor;
database MODULEDATABASE;
String rowId;
Button deleteModule;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_course);
Intent intent = getIntent();
rowId = intent.getStringExtra(MyCourses.TEST);
MODULEDATABASE = new database(ViewCourse.this);
MODULEDATABASE.openToRead(ViewCourse.this);
cursor = MODULEDATABASE.getRow(rowId);
TextView text_modulecode = (TextView)findViewById(R.id.viewModuleCode);
TextView text_modulename = (TextView)findViewById(R.id.viewModuleName);
text_modulecode.setText(cursor.getString(cursor.getColumnIndex(database.KEY_MODULECODE)));
text_modulename.setText(cursor.getString(cursor.getColumnIndex(database.KEY_MODULENAME)));
deleteModule = (Button)findViewById(R.id.deleteButton);
deleteModule.setOnClickListener(this);
}
public void onClick (View deleteModule)
{
Dialog(rowId);
}
public void Dialog (String rowId) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.confirmDelete)
.setPositiveButton(R.string.confirmDelete, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MODULEDATABASE = new database(ViewCourse.this);
MODULEDATABASE.deleteRow(rowId);
Intent intent = new Intent(this, MyCourses.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.confirmDelete, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
}
}
You don't say what the error is, but I suspect you need to change this line:
Intent intent = new Intent(this, MyCourses.class);
to:
Intent intent = new Intent(ViewCourse.this, MyCourses.class);
(The problem is that at that point in the code, this refers to the anonymous OnClickListener class.)
EDIT - Declare the rowId parameter to the method to be final:
public void Dialog (final String rowId) {
. . .
I am creating a home automation app that has allows plugin views. I have been able to create a class as a sample plugin in a separate project (apk):
public class MyTestClass_IRDroidUIPlugIn extends Button implements IRDroidInterface{
Context mContext;
public MyTestClass_IRDroidUIPlugIn(Context context) {
super(context);
mContext = context;
setText("I was loaded dynamically! (1)");
setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// how do I show the dialog from here?
Activity.showDialog(1);
}}
);
}
public Dialog buildConfigDialog(int ID){
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage("Click the Button...(1)")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
return builder.create();
}
}
I can load this class at run time and create an instance of it:
try {
final File filesDir = this.getFilesDir();
final File tmpDir = getDir("dex", 0);
final DexClassLoader classloader = new DexClassLoader( filesDir.getAbsolutePath()+"/testloadclass.apk",
tmpDir.getAbsolutePath(),
null, this.getClass().getClassLoader());
final Class<View> classToLoad =
(Class<View>) classloader.loadClass("com.strutton.android.testloadclass.MyTestClass_IRDroidUIPlugIn");
mybutton = (View) classToLoad.getDeclaredConstructor(Context.class).newInstance(this);
mybutton.setId(2);
main.addView((View)mybutton);
} catch (Exception e) {
e.printStackTrace();
}
setContentView(main);
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
return ((IRDroidInterface) mybutton).buildConfigDialog(id);
}
return null;
}
I want the plugin to be able to show a configuration dialog. Is there a way I can pass the Activity object to this class so it can use .showDialog(ID). This would be ideal so that the dialog life cycle can be managed properly.
Thanks in advance.
Maybe I'm missing something, but why can't you do something like this?
public class MyTestClass_IRDroidUIPlugIn extends Button implements IRDroidInterface{
Activity mContext;
public MyTestClass_IRDroidUIPlugIn(Activity context) {
super(context);
mContext = context;
...
setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mContext.showDialog(1);
}}
);
.....
}
When you're creating this class, you're already passing Activity into this class - so just use it.
I am new to Android.I am building one Tabbed app having 5 Tabs.I use different ViewGroups, each tab have there respactive ViewGroup according to as per need & there child Activities.First tab is TabGroupHome having different child Activities.TabGroupHome first start GoogleMapActivity (having overlayItems) as its child activity. these overlays reprasents different users. I get the data of these users from one JSONArray which is returned by one php file on server having data of differnt users. when i launch my App its look->
launching_view!
it have one actionbar on the top having different buttons(e.g., List, profile & refresh). when i click on List it show me one list of all users ->
list of Users! & user_profile!!
& when i click on listItem it show me complete profile of respactive User pointed above
Where i stuck off is-> ??? i want to show this profile of user when i tap on MapPin...
my code to do this is->
public class GoogleMapActivity extends MapActivity implements ActionBar {
Button btnOnMapList, btnProfileHome, btnRefresh;
Intent intent;
private JSONArray jArray;
private JSONObject jFan_Data;
private ItemBean bean;
private FansData fansdata;//reference of FansData class that return me JSONArray
HelloItemizedOverlay itemizedOverlay;//..........
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
btnProfileHome = (Button) findViewById(R.id.btn_profile_home);
btnOnMapList = (Button) findViewById(R.id.btn_list_home);
btnRefresh = (Button) findViewById(R.id.btn_refresh_home);
super.onCreate(savedInstanceState);
setContentView(R.layout.googlemapactivity);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.displayZoomControls(true);
// mapView.setSatellite(true);
/**TG_1 -> Here i write code to get my current location(e.g., through LocationManager)
* after getting location, i write my location`s latitude & longitude into
* shearedPreference */
//geading preference to get my unique id
SharedPreferences myUid=GoogleMapActivity.this.getSharedPreferences("uid", MODE_WORLD_READABLE);
String myId=myUid.getString("myId", "");
Log.i("MyUid", myId);
/** calling FansData class to get all users data. Currently i am providing my hard codded location but i have to get it from LocationManager */
fansdata=new FansData();
jArray=fansdata.jFanDataArray(1000, 12.9716060, 77.5903760, "h9ow0");
System.out.println(jArray.toString());
/** to showing Users on map as pins */
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(
R.drawable.small_football_icon);
HelloItemizedOverlay itemizedOverlay = new HelloItemizedOverlay(
drawable, getParent());
for(int i=0;i<jArray.length();i++){
try {
jFan_Data=jArray.getJSONObject(i);
GeoPoint geoPoint = new GeoPoint((int) (jFan_Data.getDouble("lat")* 1E6),
(int) (jFan_Data.getDouble("lang")* 1E6));
OverlayItem overlayitem = new OverlayItem(geoPoint, jFan_Data.getString("name"),
jFan_Data.getString("uniqid"));
itemizedOverlay.addOverlay(overlayitem);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mapOverlays.add(itemizedOverlay);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
#Override
public void onHomeList(View view) {
Intent in = new Intent(getParent(), MapPinsList.class);
TabGroupActivity prnt = (TabGroupActivity) getParent();
prnt.startChildActivity("MapPinsList", in);
}
#Override
public void onHomeProfile(View view) {
Intent in = new Intent(getParent(), ProfileActivity.class);
TabGroupActivity prnt = (TabGroupActivity) getParent();
prnt.startChildActivity("ProfileActivity", in);
}
#Override
public void onHomeRefresh(View view) {
// TODO Auto-generated method stub
}
#Override
public void onListMap(View view) {
// TODO Auto-generated method stub
}
#Override
public void onListProfile(View view) {
// TODO Auto-generated method stub
}
}
My HelloItemizedOverlay Class is->
public class HelloItemizedOverlay extends ItemizedOverlay {
private static final int VIEW_PROFILE = 1;
private static final int SEND_MASSAGE = 2;
OverlayItem item;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public HelloItemizedOverlay(Drawable defaultMaker, Context context) {
// super(defaultMaker);
super(boundCenterBottom(defaultMaker));
mContext = context;
}
#Override
public boolean onTap(int index) {
// Option to select on clicking pin
final String[] option = new String[] { "View Profile", "Send Massage",
"Cancle" };
// to hold option
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,
android.R.layout.select_dialog_item, option);
//OverlayItem item = mOverlays.get(index);
item = mOverlays.get(index);
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(item.getTitle());
// builder.setMessage(item.getSnippet());
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int i) {
// TODO Auto-generated method stub
if (i == 0) {
String fId=item.getSnippet();
Toast.makeText(mContext, "Profile View is on Progress...!"+fId,
Toast.LENGTH_SHORT).show();
Intent in = new Intent(mContext, FanProfile.class);
Bundle fBundle= new Bundle();
fBundle.putString("fanId", fId);
in.putExtras(fBundle);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.getApplicationContext().startActivity(in);
} else if (i == 1) {
Toast.makeText(mContext,"Send Massage View is on Progress...!",
Toast.LENGTH_SHORT).show();
} else if (i == 2) {
dialog.cancel();
}
}
});
builder.show();
// AlertDialog dialog= builder.create();
return true;
}
public void addOverlay(OverlayItem overlayItem) {
// for(int i=0;i<=size();i++){
mOverlays.add(overlayItem);
populate();
// }
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
}
FanProfile in "onTap" is the same activity which i call when i click on listItem of user`s list!
above code works but it start the view over my tabView which hides my Tabs...
[this] http://i.stack.imgur.com/lgT2G.png
i am not getting where i commit mistake or doing WRONG.
Your suggestions are valuable for me!!!
I would greatly appreciate pointers or sample code to where I get the solution of this problem...
One possible solution is to add Tabs to the FanProfile activity which are same as the Previous activity
here after a long R&D i get the solution the correct way to show FanProfile when i tap on MapPin is
#Override
public void onClick(DialogInterface dialog, int i) {
// TODO Auto-generated method stub
if (i == 0) {
//getting the unique-id of fan, whose pin is clicked on map`s pins
String fId=item.getSnippet();
Toast.makeText(mContext, "Profile View is on Progress...!"+fId,
Toast.LENGTH_SHORT).show();
//getting the parent context(cast the context of GoogleMapActivity into its Parent e.g.,"TabGroupActivity")
/** i cast the mContext into its parent Activity`s context which extends ViewGroup */
tga=(TabGroupActivity)mContext;
//1.intent that start my "FanProfile" Activity
Intent in = new Intent(tga, FanProfile.class);
//2.Bundle that hold data which i want to send to "FanProfile" Activity.
Bundle fBundle= new Bundle();
//3.putting values into bundle
fBundle.putString("fanId", fId);
//4.Binding the bundle with the intent
in.putExtras(fBundle);
//5.Starting intent
tga.startChildActivity("FanProfile", in);
} else if (i == 1) {
Toast.makeText(mContext,
"Send Massage View is on Progress...!",
Toast.LENGTH_SHORT).show();
} else if (i == 2) {
dialog.cancel();
}
}
This Works well & now i am happy that i get which need -> this!
Tanks a lot #Rajdeep Dua for youe`s Valuable suggestion!!! I will again thankful to "StackOverFlow" & you all & hoping the same cooperation & suggestions from all...
StackOverFlow is really a nice fantastic -> fabulous ->
fantabulous ... :)