Java - Reference outer class from handlers
I have the following code
this.spin.setOnItemSelectedListener( new
AdapterView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View arg1, int
arg2,long arg3){
Gradebook.this.pd =
ProgressDialog.show((Context)Gradebook.this, (CharSequence)"",
(CharSequence)"Loading Grade Book...", true, true);
Gradebook.this.gradecatitems.clear();
new Thread(new Runnable() {
public void run() {
//I need to reference the top-most
level class, GradeBook's members in
here
}
}).start();
});
How do I reference the top-level class member from within my Thread? Keep
in mind that my thread is an anonymous class already within an anonymous
class.
Boisclair
Thursday, 3 October 2013
Wednesday, 2 October 2013
Android ActivityNotFoundException failing to catch on Intent in OS 4.1.2 (works on 4.0.3)?
Android ActivityNotFoundException failing to catch on Intent in OS 4.1.2
(works on 4.0.3)?
Thanks for taking a look. I am a very old programmer but absolute newbie
to Android/Java. Although I haven't found a way to make code colorful and
pretty here yet ;).
I am starting an activity to open a file with a bogus/fake extension (mime
type) so I can force this ActivityNotFoundException exception (to test).
Somehow, this exception never happens on my Samsung Galaxy S3 (OS 4.1.2).
Adobe Reader opens up and errors that the file is not a PDF (it's not).
However on an older ASUS Transformer tablet running 4.0.3 the exception is
caught properly.
The code in question is like so:
// this file exists (check omitted)..
File docFile = new File("/path/to/file.bogusextension");
// intent
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
// getting doc Uri
Uri fileUri = Uri.fromFile(docFile);
// getting doc mimeType
MimeTypeMap mime = MimeTypeMap.getSingleton();
String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
String mimeType = mime.getMimeTypeFromExtension(extension);
intent.setDataAndType(fileUri, mimeType);
// this is for an adobe native extension, just getting FREContext
// in the class below and mapping functions
DocLauncherExtensionContext extensionContext =
(DocLauncherExtensionContext) context;
Activity activity = extensionContext.getActivity();
try
{
activity.startActivity(intent);
returnValue = FREObject.newObject(true);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(context.getActivity(), "This never happens!",
Toast.LENGTH_LONG).show();
}
Is there anything blatent wrong with this code that would allow a file
with any extension what-so-ever to be constantly thrown at Adobe Reader? I
have cleared my defaults (Settings->Application Manager->Reset).
Everything I startActivity is just hurled at Adobe Reader.
Any thoughts appreciated! Thank you!
(works on 4.0.3)?
Thanks for taking a look. I am a very old programmer but absolute newbie
to Android/Java. Although I haven't found a way to make code colorful and
pretty here yet ;).
I am starting an activity to open a file with a bogus/fake extension (mime
type) so I can force this ActivityNotFoundException exception (to test).
Somehow, this exception never happens on my Samsung Galaxy S3 (OS 4.1.2).
Adobe Reader opens up and errors that the file is not a PDF (it's not).
However on an older ASUS Transformer tablet running 4.0.3 the exception is
caught properly.
The code in question is like so:
// this file exists (check omitted)..
File docFile = new File("/path/to/file.bogusextension");
// intent
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
// getting doc Uri
Uri fileUri = Uri.fromFile(docFile);
// getting doc mimeType
MimeTypeMap mime = MimeTypeMap.getSingleton();
String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
String mimeType = mime.getMimeTypeFromExtension(extension);
intent.setDataAndType(fileUri, mimeType);
// this is for an adobe native extension, just getting FREContext
// in the class below and mapping functions
DocLauncherExtensionContext extensionContext =
(DocLauncherExtensionContext) context;
Activity activity = extensionContext.getActivity();
try
{
activity.startActivity(intent);
returnValue = FREObject.newObject(true);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(context.getActivity(), "This never happens!",
Toast.LENGTH_LONG).show();
}
Is there anything blatent wrong with this code that would allow a file
with any extension what-so-ever to be constantly thrown at Adobe Reader? I
have cleared my defaults (Settings->Application Manager->Reset).
Everything I startActivity is just hurled at Adobe Reader.
Any thoughts appreciated! Thank you!
Determining the element a drag stops over
Determining the element a drag stops over
I have a jsfiddle _ http://jsfiddle.net/stevea/US54g/9/ - with four
draggable boxes. I would like to determine the element that a drag stops
at. For example, if I drag box 2 and drop it on box 3, I'd like the
stopDrag handler to be able to tell that the drag stopped over box 3. Or
if I dropped it over the body, where there were no boxes, I'd to know
that. But I don't anything in the event object or the ui object that tells
me the element at those coordinates. Am I missing it somewhere?
Thanks.
<div id="box1" class="box">box1</div>
<div id="box2" class="box">box2</div>
<div id="box3" class="box">box3</div>
<div id="box4" class="box">box4</div>
I have a jsfiddle _ http://jsfiddle.net/stevea/US54g/9/ - with four
draggable boxes. I would like to determine the element that a drag stops
at. For example, if I drag box 2 and drop it on box 3, I'd like the
stopDrag handler to be able to tell that the drag stopped over box 3. Or
if I dropped it over the body, where there were no boxes, I'd to know
that. But I don't anything in the event object or the ui object that tells
me the element at those coordinates. Am I missing it somewhere?
Thanks.
<div id="box1" class="box">box1</div>
<div id="box2" class="box">box2</div>
<div id="box3" class="box">box3</div>
<div id="box4" class="box">box4</div>
are there alternatives to variadic template
are there alternatives to variadic template
i have some code like this:
// Factory.h
typedef std::uint8_t FactoryId;
const FactoryId FACTORY_ID_MAX_VALUE = UINT8_MAX;
template <typename TBaseProduct>
class IFactory {
public:
virtual TBaseProduct* create() = 0;
};
template <typename TProduct, typename TBaseProduct>
class Factory : public IFactory<TBaseProduct> {
public:
virtual TBaseProduct* create() override {
return new TProduct;
}
};
template <typename TProduct, typename TBaseProduct>
class FactoryProduct : public TBaseProduct {
public:
static Factory<TProduct, TBaseProduct> factory;
};
template <typename TFactoryTable, typename TBaseProduct>
class FactoryTable {
public:
typedef IFactory<TBaseProduct> BaseFactory;
typedef BaseFactory* BaseFactoryPtr;
FactoryTable(): factorys(nullptr) {}
~FactoryTable() { delete[] factorys; }
BaseFactory& get(FactoryId factoryId) {
if (factoryId > maxFactoryId) {
throw std::exception("out of range");
}
return *factorys[factoryId];
}
protected:
template <std::size_t factoryCount>
void init(BaseFactoryPtr (&factorys)[factoryCount]) {
init(factorys, factoryCount);
}
void init(BaseFactoryPtr* factorys, std::size_t factoryCount) {
assert(factorys != nullptr);
assert(factoryCount > 0 && factoryCount - 1 <= FACTORY_ID_MAX_VALUE);
this->factorys = new BaseFactoryPtr[factoryCount];
std::memcpy(this->factorys, factorys, sizeof(BaseFactoryPtr) *
factoryCount);
this->maxFactoryId = factoryCount - 1;
}
private:
BaseFactoryPtr* factorys;
FactoryId maxFactoryId;
};
// Foo.h
class BaseFoo {};
class Foo1 : public FactoryProduct<Foo1, BaseFoo> {};
class Foo2 : public FactoryProduct<Foo2, BaseFoo> {};
class FooFactoryTable : public FactoryTable<FooFactoryTable, BaseFoo> {
public:
FooFactoryTable() {
IFactory<BaseFoo>* table[] = {
&Foo1::factory,
&Foo2::factory,
};
init(table);
}
};
in order to provide a factory array to init FactoryTable<>, i have to
manually create an array (IFactory* table[]) in FooFactoryTable. so i use
variadic template to avoid this:
template <typename TFactoryTable, typename TBaseProduct, typename...
MTProduct>
class FactoryTable {
// the visible of the two init() is changed from protected to private.
// except this, the only changed member is the constructor
FactoryTable(): factorys(nullptr) {
IFactory<BaseFoo>* table[] = {
(&MTProduct::factory)...
};
init(table);
}
};
so that i can simply implement "FooFactoryTable" like this, and hide
FooFactoryTable's knowledge about "FactoryProduct<>::factory"
class FooFactoryTable : public FactoryTable<FooFactoryTable, BaseFoo,
Foo1,
Foo2> {};
my question is, is there another way to implement "FactoryTable" without
using variadic template? because variadic template is not supported by
visual studio 2012(v110), and "'Microsoft Visual C++ Compiler Nov 2012
CTP' is for testing purposes only.". i worry other compilers may also have
the some problem as visual studio 2012(v110). the "other compilers" is
those compilers that targeting android or iphone.
i have some code like this:
// Factory.h
typedef std::uint8_t FactoryId;
const FactoryId FACTORY_ID_MAX_VALUE = UINT8_MAX;
template <typename TBaseProduct>
class IFactory {
public:
virtual TBaseProduct* create() = 0;
};
template <typename TProduct, typename TBaseProduct>
class Factory : public IFactory<TBaseProduct> {
public:
virtual TBaseProduct* create() override {
return new TProduct;
}
};
template <typename TProduct, typename TBaseProduct>
class FactoryProduct : public TBaseProduct {
public:
static Factory<TProduct, TBaseProduct> factory;
};
template <typename TFactoryTable, typename TBaseProduct>
class FactoryTable {
public:
typedef IFactory<TBaseProduct> BaseFactory;
typedef BaseFactory* BaseFactoryPtr;
FactoryTable(): factorys(nullptr) {}
~FactoryTable() { delete[] factorys; }
BaseFactory& get(FactoryId factoryId) {
if (factoryId > maxFactoryId) {
throw std::exception("out of range");
}
return *factorys[factoryId];
}
protected:
template <std::size_t factoryCount>
void init(BaseFactoryPtr (&factorys)[factoryCount]) {
init(factorys, factoryCount);
}
void init(BaseFactoryPtr* factorys, std::size_t factoryCount) {
assert(factorys != nullptr);
assert(factoryCount > 0 && factoryCount - 1 <= FACTORY_ID_MAX_VALUE);
this->factorys = new BaseFactoryPtr[factoryCount];
std::memcpy(this->factorys, factorys, sizeof(BaseFactoryPtr) *
factoryCount);
this->maxFactoryId = factoryCount - 1;
}
private:
BaseFactoryPtr* factorys;
FactoryId maxFactoryId;
};
// Foo.h
class BaseFoo {};
class Foo1 : public FactoryProduct<Foo1, BaseFoo> {};
class Foo2 : public FactoryProduct<Foo2, BaseFoo> {};
class FooFactoryTable : public FactoryTable<FooFactoryTable, BaseFoo> {
public:
FooFactoryTable() {
IFactory<BaseFoo>* table[] = {
&Foo1::factory,
&Foo2::factory,
};
init(table);
}
};
in order to provide a factory array to init FactoryTable<>, i have to
manually create an array (IFactory* table[]) in FooFactoryTable. so i use
variadic template to avoid this:
template <typename TFactoryTable, typename TBaseProduct, typename...
MTProduct>
class FactoryTable {
// the visible of the two init() is changed from protected to private.
// except this, the only changed member is the constructor
FactoryTable(): factorys(nullptr) {
IFactory<BaseFoo>* table[] = {
(&MTProduct::factory)...
};
init(table);
}
};
so that i can simply implement "FooFactoryTable" like this, and hide
FooFactoryTable's knowledge about "FactoryProduct<>::factory"
class FooFactoryTable : public FactoryTable<FooFactoryTable, BaseFoo,
Foo1,
Foo2> {};
my question is, is there another way to implement "FactoryTable" without
using variadic template? because variadic template is not supported by
visual studio 2012(v110), and "'Microsoft Visual C++ Compiler Nov 2012
CTP' is for testing purposes only.". i worry other compilers may also have
the some problem as visual studio 2012(v110). the "other compilers" is
those compilers that targeting android or iphone.
Tuesday, 1 October 2013
how to expose class names when serializing with Gson
how to expose class names when serializing with Gson
My scenario is very complicated but here's a summary:
I'm trying to understand source of a compiler -- and to understand what
each AST node represents, I'm generating JSON serializations of ASTs of
different programs and later inspect the visualized JSON output.
It works great except one problem is that in Gson generated JSON data
class names isn't mentioned, so it still doesn't help much. Is there a way
to add class names to Gson output without much effort? (without adding
some method to every AST node etc.)
My scenario is very complicated but here's a summary:
I'm trying to understand source of a compiler -- and to understand what
each AST node represents, I'm generating JSON serializations of ASTs of
different programs and later inspect the visualized JSON output.
It works great except one problem is that in Gson generated JSON data
class names isn't mentioned, so it still doesn't help much. Is there a way
to add class names to Gson output without much effort? (without adding
some method to every AST node etc.)
How to check if a function parameter is constant?
How to check if a function parameter is constant?
This may be easy and I could be just missing what is right in front of me.
However, I cannot figure out how to ensure a function parameter, passed by
reference, can be modified. Essentially i need the following:
bool calculate(double lat, double lon, double dep,
double &x, double &y, double &z)
{
if (x, y, AND z are NOT const)
{
perform the proper calculations
assign x, y, and z their new values
return true;
}
else //x, y, or z are const
{
return false;
}
}
The "if" statment check is really all i need
Again I apologize if this is already on this site or if it's a standard
library function that i'm missing right in front of me. I come here all
the time and virtually always get a good answer but I could not find
anything on this already on here.
This may be easy and I could be just missing what is right in front of me.
However, I cannot figure out how to ensure a function parameter, passed by
reference, can be modified. Essentially i need the following:
bool calculate(double lat, double lon, double dep,
double &x, double &y, double &z)
{
if (x, y, AND z are NOT const)
{
perform the proper calculations
assign x, y, and z their new values
return true;
}
else //x, y, or z are const
{
return false;
}
}
The "if" statment check is really all i need
Again I apologize if this is already on this site or if it's a standard
library function that i'm missing right in front of me. I come here all
the time and virtually always get a good answer but I could not find
anything on this already on here.
Trouble with libreoffice impress custom animation
Trouble with libreoffice impress custom animation
Hey
In libreoffice impress ,if make some change in custom animation ,its shows
me desired ouput with animation but once I close that file after saving it
,the animation doesn't shows up.So,I have to do the whole process again of
customization...
So,if anyone knew that what to do...plz help ..I have to submit my project
soon..
Hey
In libreoffice impress ,if make some change in custom animation ,its shows
me desired ouput with animation but once I close that file after saving it
,the animation doesn't shows up.So,I have to do the whole process again of
customization...
So,if anyone knew that what to do...plz help ..I have to submit my project
soon..
Subscribe to:
Comments (Atom)