Cannot use values from other packages - android

Is it possible to access or use methods from different packages (or package libraries) in an Android project?

You can import other packages, in order to change values you should define set-functions within the package you are going to import.
e.g
public void setString (String sNew) // in case its a string
{
YOURVALUE = sNew; // YOURVALUE should be the value you want to change
}
in order to get the value you should use a get-function
public String getString ()
{
return YOURVALUE;
}
just change the type to the one you need.
after that you are able to call these functions like :
PACKAGENAME.setString("This is the new Value");
System.out.println(PACKAGENAME.getString());

Related

Access variable name in lambda function

I'm trying to access the name of a variable inside an iterator
listOf(someClassVariable, anotherClassVariable, yetAnotherClassVariable).forEach {
if (it.foo()) {
map.add(it, ::it.name)
}
}
but getting unsupported [references to variables aren't supported yet] error at ::it.name. Any ideas/workarounds?
You could do it vice-versa, i.e. having a list of references to your class variables and iterate over them and then get the actual value by calling invoke on it:
listOf(::someClassVariable, ::anotherClassVariable, ::yetAnotherClassVariable).forEach { varRef ->
val varValue = varRef() // assignment optional... you can also just do it the way you want ;-)
if (varValue.foo())
map.add(varValue, varRef.name)
}

Change ObjectBox LiveData Query

I have an ObjectBoxLiveData object with a query that is set at runtime:
private ObjectBoxLiveData<MyObject> myObjectLiveData;
public ObjectBoxLiveData<MyObject> getMyObjectLiveData(Box<MyObject> myObjectBox, String filterTerm)
{
if (myObjectLiveData == null)
myObjectLiveData = new ObjectBoxLiveData<>(myObjectBox.query().equal(MyObject_.filterProperty, filterTerm).build());
return myObjectLiveData;
}
But I also need to be able to change the filterTerm at runtime. My thinking is that I can make a private String currentFilterTerm; object in MyViewModel to see if I need to update the filter term in the LiveData object, but is there a correct way to update the filter term? I worry that setting myObjectLiveData = new ObjectBoxLiveData<> again will leave a memory leak for the previously defined myObjectLiveData or anything tied to it, but I don't see any graceful way to dispose of it or update the query once defined. Is there a way to redefine my query once defined?

Error: You must use startAt(String value), endAt(String value) or equalTo(String value) in combination with orderByKey()

I am using FirebaseRecyclerAdapter to display a list and I want to display only those keys which have the value true. (Note that the value of a key is stored as boolean and not string)
I tried using a query as follows:
mAdapter = new FirebaseRecyclerAdapter<Boolean, PollAdapterViewHolder>(Boolean.class, R.layout.ongoing_poll_list, PollAdapterViewHolder.class, ref.orderByKey().equalTo(true)) {
#Override
public void populateViewHolder(final PollAdapterViewHolder pollViewHolder, Boolean model, int position) {...}
where,
ref = FirebaseDatabase.getInstance().getReference().child("subscriptions/" + FirebaseAuth.getInstance().getCurrentUser().getUid());
However, I am getting this error:
java.lang.IllegalArgumentException: You must use startAt(String value), endAt(String value) or equalTo(String value) in combination with orderByKey(). Other type of values or using the version with 2 parameters is not supported
Is there any way to solve this problem?
It looks like you're using equalTo(boolean). The error message says to use equalTo(String).
I suspect that you don't actually want to use orderByKey(). It looks like you might want to use orderByValue() instead.

android - get a resource (a string) from its unique integer

I want to do the following:
I want to make a very simple gallery application. So I'd like to select a path for the images and set it as a resource. I set it in String.xml.
So I have another class, which needs the selected path to load all the images from it.
class ImageHolder
{
public ImageHolder()
{
this(R.string.image_dir);
//problem is here - R.string.image_dir returns a unique int, while what I really need is the string. How can I get it...
}
public ImageHolder(String path)
{
.........standart procedure.....
}
}
Use getString(resID) but you'll need a Context Object though.
It's a Function of Context, so within an Activity you can write this.getString(R.string.image_dir); or you can skip this altogether...

How to use a single intent with multiple buttons

I have an android context menu with various selections and depending on the user selection I want to start an intent. The intent starts the same activity for all the buttons but will contain different String variables depending on the selection. I am currently using a switch, case methodology for my click listener but keep running into 'duplicate local variable' problems as I try to eliminate code repetition! If anyone could provide a-bit of pseudo-code that would be even better!
It's hard to tell without seeing some code, but "duplicate local variables" together with "switch case" makes me think you're declaring a variable in one of the cases with the same name as a variable from another case.
Code within different cases of the same switch is all in the same scope, unless you surround the code within a case with brackets, like this:
switch(VALUE) {
case A: {
String string = "";
}
case B: {
//Same variable name, possible since it's in a different scope now.
String string = "";
}
}
So either use brackets, or simply make sure you're using different variable names across the cases.
you can use intent.putExtra(String name, String value) and push it to the other activity.
Pseudo code:
Button1.value = "X" ;
Button2.value = "Y" ;
onClickListner(View v) {
Intent intent = new Intent() ;
intent.putExtra("ButtonValue",
v.value() ) ;
// extra code goes here...
}
Hope this is what you were looking for..
VInay
I like to use set/getTag(Object), as you can put any type you like into it (as long as you're careful about getting it out again):
button1.setTag(MyClass.STATIC_INT_1);
button2.setTag(MyClass.STATIC_INT_2);
button1.setOnClickListener(Click);
button2.setOnClickListener(Click);
private OnClickListener Click(View v) {
Intent intent = new Intent() ;
intent.putExtra("Value", Integer.parseInt(v.getTag().toString()) ) ;
ยทยทยท
}

Categories

Resources