i was watching thenewboston's tutorial about fragments and i came across this line of code..
#Override
public void sendtex(String top, String bottom) {
BottomFregment_class bottomFregment = (BottomFregment_class) getSupportFragmentManager().findFragmentById(R.id.Main);
bottomFregment.finale(top,bottom);
}
this was to change TextView by getting text from another fragment! and "sendtext is implemented method from that fragment"
i replaced
BottomFregment_class bottomFregment = (BottomFregment_class) getSupportFragmentManager().findFragmentById(R.id.Main);
bottomFregment.finale(top,bottom);
with
BottomFregment_class bottomFregmentClass = new BottomFregment_class();
bottomFregmentClass.finale(top,bottom);
and everything worked fine!
i want to know that is there any difference in between these two codes?
or will this cause any performance issues?
In the first, you fetch an existing fragment, maybe with some data in there, on the other one you create an empty one.
Related
I am working on stripe-terminal-android-app, to connect to BBPOS 2X Reader device,
wanted to click-item from list,(recyclerView).
I am trying to do:
when list of devices appears(readers), I am checking if readers.size()==1, then click first-device from list,else show recyclerView();
I have very less experience in Android(coming from JS, PY), :)
After going through debugger to understand flow of program-running, I used F8 key, or stepOver the functions one by one,
and where value is assigned to convert in displayble-format in adapter as here.
public ReaderAdapter(#NotNull DiscoveryViewModel viewModel) {
super();
this.viewModel = viewModel;
if (viewModel.readers.getValue() == null) {
readers = new ArrayList<>();
} else {
readers = viewModel.readers.getValue();
if(readers.size() == 1){
Log.e(TAG, "readers.size() is 1 "+ readers.size());
}
}
}
then in ReaderHolder-file, values are bind() as
void bind(#NotNull Reader reader) {
binding.setItem(reader);
binding.setHandler(clickListener);
binding.executePendingBindings();
}
}
I tried assigining button and manually clicking when only-one device appears, by clicing on reader[0], can't do that by findViewById inside Adapter file, to call onClick() method manually,
I tired another StackOverflow's answer but didn't understood, from here.
Main fragment is discovery-fragment,
how can I click first-device by checking readers.size()==1, then click onClick()?
my final-goal is to automate, whole stripe-terminal-payment process on android.
extra-info:
I am fetching data from python-odoo server, then using url, will open app through browser, (done this part), then device will be selected automatically as everytime-no any devices will be present except one,
so will automatically select that from recyclerView, then proceed.
I have asked for help in detailed way on GitHub-issues, and started learning Android's concepts for this app(by customizing stripe's demo app, which works great, but I wanted to avoid manually clicking/selection of devices).
I am programming an app for Android. I uploaded it to GitHub: app
I have a ViewPager (MainActivity.java) controlling two Fragments. On the first Fragment (FirstFragment.java) you can add People (People.java) which appears on the RecyclerView (also on FirstFragment.java). When you click one of the list items on the RecyclerView its details (name and id) appear on the second fragment (SecondFragment.java). The SecondFragment.java also contains a button you can delete the selected People with.
To store the People objects I used a List of People and managed it with the methods in PeopleLab.java. The program was working fine: I could add/remove People objects to the list and it appeared on the RecyclerView fine.
After that, I decided to replace the List with a database. It only meant creating the database (the 3 files in database folder) and editing the already existing and two new methods in PeopleLab.java. The other files remained untouched.
The database is working as expected (checked it with sqlite3), I can add/remove People like before and the queries work. My only problem is that the changes don't appear on the RecyclerView. But if I close and reopen the app, the changes appear.
It's like the RecyclerView doesn't care about the database in runtime, only do when the app starts (or closes, not sure).
Do you have any idea what could cause the problem? My only guess is I miss something about how Android apps handle databases.
P.S.: sorry for my English.
You do call notifyDataSetChanged() on the adapter but you don't provide any new data for that adapter.
In your FirstFragment :
private void updateUI() {
PeopleLab peopleLab = PeopleLab.get(getActivity());
List<People> peoples = peopleLab.getPeople();
if(mAdapter == null) {
mAdapter = new PeopleAdapter(peoples);
mRecyclerView.setAdapter(mAdapter);
} else {
// You actually have to change your dataset
mAdapter.changeDataSet(peoples);
}
}
And in your Adapter :
public void changeDataSet(List<People> people) {
this.mPeoples = people;
notifyDataSetChanged();
}
This some brutal way to do it though.
It would be better to notify your adapter on insertion / removal calling notifyItemInserted(int itemPosition) or notifyItemRemoved(int itemPosition). (And refreshing your dataset, by the way)
It will not work automatically.You can either use to notify the adapter the underlying data has been changed so that adapter can fetch and reload the data.It can be done using adapter.notifyDataSetChanged()
Or use can use CursorLoader to achieve the same
i have a lot of recycler views, they all use the same adapter, i used to update them with arraylists of objects that id make as i went like this
ADAPTER
private List<CardWriter> cardMakerList;
public CardAdapter(List<CardWriter> cardMakerList){
this.cardMakerList = cardMakerList;
// this.mContext = context;
}
ACTIVITY
public List<CardWriter> cardMakerList = new ArrayList<>();
public CardAdapter cardAdapter;
recyclerView.setAdapter(cardAdapter);
CardWriter cardWriter = new
CardWriter(getResources().getDrawable(R.drawable.happy),"HAPPY","happy ");
cardMakerList.add(cardWriter);
THE PROBLEM
Ive now switched to using databases for each recycler view (using greenDao see this question) so my adapter now uses the below code (addNewCard refers to the generated java class made by greendao)
public List<addNewCard> leaseList = new ArrayList<>();
but now to get my second database(2) its the exact same properties but greendao makes a different file so i should use this in my adapter
public List<addSimpleCard> leaseList = new ArrayList<>();
but this would mean a new adapter for every database and im going to need a lot of them. All the databases im making are just lots of the same object with different properties (an image and 2 strings) but each database needs to be managed individually.
my last question (in the link above) asks how i can make all the same databases with a different title which i think would also solve my problem.
WHAT IVE TRIED
I've tried just creating an static reference to the array list and passing it the new one eg:
public static CardAdapterDB cardAdapterDB;
cardAdapterDB = new CardAdapterDB(leaseList);
this works but trying to change it for a new database
cardAdapterDB = new CardAdapterDB(simpleleaseList);
gives me an error
CardAdapterDB(java.util.List<greendao.addNewCard>) in CardAdapterDB cannot be applied to (java.util.List<greendao.addSimpleCard>)
addSimpleCard is the name of my other java file generated by greendao so this makes perfect sense but how can i get around this without creating loads of new adapters and switching them all the time?
since asking this ive tried multiple ways to get this to work but have yet to find something suitable
My apologies
Hi, I am a newbie to the advance level programming. I have been working around with the basics and the syntax of the language.
I have searched enough
The problem I am having is, that each time, I write the code to handle the events in my App, it gets stopped. I have tried following step by step coding of the video from Treehouse tutorial of using the onClick and I have tried to follow the Android's official developer documents too. I have also looked into the codes at some sites (including and specially Stack Overflow).
All in vain
But each time I run the code, app works and when it has to execute the methods and functions, it gives me an alert saying App (Number Converter) stopped Unexpectedly. I am creating an app to convert the numbers from and to the 4 number systems, decimal, binary, octal and hexadecimal.
But, I am not able to get to the point why and why every time I write the code that is same, doesn't work and app stops.
Here is the code I am trying:
Button frmDeciToOct = (Button) findViewById(R.id.fromDecToOct);
/* here is the method that is the onClick */
frmDeciToBin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* getting the edit text view */
EditText edtTxt = (EditText) findViewById(R.id.decimalValue);
/* to string... */
String value = edtTxt.getText().toString();
/* conversion to int, I saw this method on SO's answer */
int value2 = Integer.valueOf(value);
/* createBinOfDeci is a method, I will post its code after this */
String result = createBinOfDeci(value2);
/* get the text view */
TextView txtView = (TextView) findViewById(R.id.fromDeciRes);
/* set the text as a result */
txtView.setText(result);
}
});
Here is the code for my method createBinOfDeci():
public String createBinOfDeci (int deci) {
String result = "";
result = Integer.toBinaryString(deci);
return result;
}
The only thing that I am getting is that, I am using Windows and it gets crashed due to less RAM amount here! I am truly out of guesses and answers. Any help would be appreciated.
As #CommonsWare suggested, to check the LogCat, and I saw that it showed me a few errors.
Which was a clear sign of saying: The values are not correctly matching up.
Then I came up to the point, that the values weren't actually filled. They were empty, and I was running the code on a null hehe.
I just added the value and it worked!
Thanks guys (for the LogCat suggestion).
I have a little problem with a SignalStrength Android class. Is there a way to use methods from this class without referencing an object from this class in a onSignalStrengthsChanged() method?
From all the examples I saw, it seems to me that this is an Event Class and that object is created when an event (change of gsm signal strength) occurs and that this is the only way to call methods from this class. Am I right?
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
String power= String.valueOf(signalStrength.getGsmSignalStrength());
gsmStat.setText("GSM cinr: " +power);
}
What if I want to call methods from this class (and read gsm signal properties) in some other case, for example when I click a button object?
Thank you very much for any help, I'm really stuck with this.
BR,
Z
Thank you for clearing this up. I tried to use getNeighbouringCellInfo but for some reason this return 0. And yes I've searched and tried different examples.
Tel = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
public void onClick(View v) {
List<NeighboringCellInfo> neighCell = null;
neighCell = Tel.getNeighboringCellInfo();
NeighboringCellInfo thisCell = neighCell.get(1);
int thisNeighRSSI = thisCell.getRssi();
String rssi=Integer.toString(thisNeighRSSI);
tvRSSI.setText("Test: " +rssi);
}
You can't. The only way to get a relevant SignalStrength object is in the onSignalStrengthChanged method.
You can, however, use the TelephonyManager.getNeighbouringCellInfo method to list surrounding cells and get the signal RSSI power (getRssi).
See: getNeighboringCellInfo() returning null list
getNeighboringCellInfo()
This doesn't seem to work on Samsung devices.
Sorry to put this as an answer, but I cannot add a comment to an existing answer.
As for my answer, you can track the RSSI yourself in a service so that when you press the button, the last recorded RSSI value is returned. Having said that, I've found that on the Samsung Galaxy I work with, the RSSI value is actually the number of antenna bars (ie. I only get 4 values) and not the actual RSSI.