Trying to explain a Java method in Android Studio with Intent - java

I'm trying to do a written report on some code and I found one on youtube. However I don't understand what is going on exactly. I understand that it get's the ID of a certain object which then opens in a new Java class but if someone could breakdown what is happening it would be greatly appreciated.
private void setUpOnclickListener()
{
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l)
{
Supplier selectSupplier = (Supplier) (listView.getItemAtPosition(position));
Intent showDetail = new Intent(getApplicationContext(), DetailActivity.class);
showDetail.putExtra("id",selectSupplier.getId());
startActivity(showDetail);
}
});
}

at first read some doc, what IS an Activity, then read how to start new one, and why Intent is needed for this
in short: this Intent is carrying information what dev want to start/open (will start new "window" - DetailActivity) and what will be passed to this component (some id)

The code gets the supplier that is clicked on in the listview in order to gain their supplier id. That ID is then given to the DetailActivity page using an intent, whereby it will be used to display the details that relate to the specific supplier. On the DetailActivity page, the intent and its extra information, namely the supplier id, will be retrieved, and then can be used to display the details for the specific supplier that was clicked on on the previous page.

Related

Passing Objects Between Intents (intent.putExtra), with Realm Objects

I have realm objects with Primary keys here's an example:
#RealmClass
public class Person extends RealmObject(){
#PrimaryKey
Int personID;
String name;
Int age;
}
The Person onjects are viewed in a recycler view, where the viewholder has an OnClick method. The OnClick method is fully functional, in the sense that screens change and everything inside it executes. However the Data from the specific Person object selected does not pass through.
Here is the code inside the OnClick method:
Intent intent = new Intent(this, PersonStats.class);
intent.putExtra("I don't know what to put here!");
startActivity(intent);
Where PersonStats is the class that will show all the stats of the Person object selected from the recycler view. I've been messing around with the intent.putExtra function but I can't get it to work. How would I go about this?
Edit: I'm asking for how to get the specific Realm Object that I selected from the recycler view so I can pass it through putExtra. As RecyclerView postion != PrimaryKey.

How to retrieve the position in a ListView with Firebase results

I'm not using FirebaseAdapter or any Firebase-UI dependency, I have made this completly native for now, but I'm getting a problem at this line trying to pass extras:
mRootDatabase = getAdapter(mContext).getItem(position);
Required DatabaseReference , Found UserPojo
public void clickListItems(ListView listView,final DatabaseReference mRootDatabase) {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(mContext, "Clicked: " + getmList().get(position), Toast.LENGTH_SHORT).show();
mRootDatabase = getAdapter(mContext).getItem(position);
Intent intent = new Intent(mContext, UserEdit.class);
intent.putExtra("uid", mRootDatabase.getKey());
intent.putExtra("Name",getAdapter(mContext).getItem(position).getName());
intent.putExtra("Email", getAdapter(mContext).getItem(position).getEmail());
intent.putExtra("Pay", getAdapter(mContext).getItem(position).getPay());
intent.putExtra("LastCon", getAdapter(mContext).getItem(position).getLastCon());
intent.putExtra("FirstCon", getAdapter(mContext).getItem(position).getFirstCon());
mContext.startActivity(intent);
}
});
}
In FirebaseListAdapter you can use getRef() in order to do this faster, but im wondering how to approach the same without it because my app does not have implemented any FirebaseAdapters , and I dont want to redo all my code again
Edit:
This is how I got my adapter
public ArrayAdapter<UserPojo> getAdapter(Context adapterContext) {
return new ArrayAdapter<UserPojo>(adapterContext,android.R.layout.simple_list_item_1,getmList());
}
Thanks!
To solve this, you need to change the following line of code:
mRootDatabase = getAdapter(mContext).getItem(position);
to
mRootDatabase = adapter.getRef(position);
In which adapter variable is the actual adapter that you are using to display the data.
Edit:
You cannot use getRef() method, unless you are using Firebase-UI library. You cannot get the DatabaseReference object from the ArrayAdapter object. None of the methods inside this class return a DatabaseReference object. The only way in which you can achieve this is to use Firebase-UI library.
It looks like you're trying to assign a Pojo to a Database Reference, instead you would need to assign it to the actual pojo and reference it that way
Something like this
UserPojo User = adapter.getItem(position);

Wrong position of items after using a customAdapter

This is the tutorial I have used: http://www.tutorialsbuzz.com/2014/08/filter-custom-listviewbaseadapter.html
and I implemented to it an onItemClickListener
OnItemClickListener myListViewClicked = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String member_name = countrylist.get(position).getName();
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
if (member_name.equals("aa")) {
Intent i = new Intent(Listview.this, Start.class);
startActivity(i);
displayInterstitial();
} else {
prName.show();
}
}
};
lv.setOnItemClickListener(myListViewClicked);
}
so, let us suppose my original list is
Aa
BB
Ca
DD
and I type 'a' in the filter, then the list becomes
Aa
Ca
But when I click Ca, I get redirected into BB using further action that depends on .equal for each list item.
My code for my list and custom adapter is the same as in the tutrial, but the onclickitem listener, i have implemeneted it, so I think im missing something in it. I tried searching and found couple of same quesiton, but none answered, each with self-answer that was different than mine and i couldn't apply it to my cusotm adapter.
it is mostly because your are accessing the dataset directly.
String member_name = countrylist.get(position).getName();
During the filtering you are probably instantiating and new Collection overriding the the reference you are using in your Activity.
Use
String member_name = ((Country)parent.getItemAtPosition(position)).getName();
and it will work.
parent.getItemAtPosition(position) accesses the underlying dataset in your Adapter, if your getItem is correctly implemented
You can use
String memberName=(YourObject)parent.getAdapter().get(postion).getName;
its easy.

Android remove and add to ArrayList in between activities

I'm making my first android app and here's where i'm stuck.
I have an activity A which requires 4 players to be picked.
I'm passing to the activity PickPlayer 1,2,3,4 according to which player i want to fill.
ImageButton addp1 = (ImageButton)findViewById(R.id.player1);
addp1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent(getApplicationContext(), PickPlayer.class);
i.putExtra("playersList", playersList);
startActivityForResult(i, 1);
}
});
On the PickPlayer activity i have a list which is populated and each item receives a listener.
final ArrayList<Player> playersList = (ArrayList<Player>)getIntent().getSerializableExtra("playersList");
lv.setAdapter(new PlayerItemAdapter(this, android.R.layout.simple_list_item_1, playersList));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
player = playersList.get(position);
playersList.remove(position);
Intent intentMessage = new Intent();
intentMessage.putExtra("player", player);
intentMessage.putExtra("playersList", playersList);
setResult(RESULT_OK, intentMessage);
finish();
}
});`
The above works fine by creating the playersList on activity A and passing it through from one to another each time and removing the player from the playerList on click.
Problem is if a player is chosen by mistake he needs to be put back into the list again once replaced by someone else.
Any suggestions on implementing this ?
One way i thought of is to pass from activity A to PickPlayer the player ( if one is assigned already at his position ) and readding him to the playerList again but i'm sure there's a better way for it.
I'm new to android so i have no idea about resources and best practises.
( example passing an object through activities or an id and run a db query).
Thanks
IMO, you'll achieve best results with a singleton class (or methods on Application instance - here a good stackoverflow question about it).
Your array would be an internal member of the singleton and have an boolean attribute to indicate if a player is already picked or not. Some methods using this attribute can be implemented like:
List<Player> getPickedPlayers()
List<Player> getNotPickedPlayers()
void setPlayerPicked(Player player)
void setPlayerNotPicked(Player player)
and so on...
Hope it helps!
When you send object through an Intent's bundle (i.putExtra("playersList", playersList);), it is marshalled and then unmarshalled on the other side (the new activity). This mean you have 2 instances of ArrayList and its content (one in each activity). If you wish to share data between activity A and activity B, I suggest you store it on an Application instance or by using a singleton.
If your data is coming from a database, you can pass the id through the intent, and get the list of players and the special player with a database query.
Not sure if this is the best way to accomplish this but i'm going to share with you.
I move the arrayList with the players back and forth in between the activities.
Once the player is sent back it's removed from it and kept in an object player1,player2,player3 etc etc.
So if the user clicks the button that has already a player assigned to it i simply add that player again into the list and pass the arrayList as i would do if it was empty.

How to pass an object without intent

Currently I am able to pass an object called Exhibit to another Activity by putting it into putExtras and starting the intent. Now, what if I want to pass the object to another object?
For example I send like this:
public void onListItemClick(ListView parent, View v, int position, long id) {
Intent i = new Intent(this, ExhibitOpen.class);
Bundle bundle = new Bundle();
bundle.putSerializable("MyClass", (Serializable) exhibits.get(position));
i.putExtras(bundle);
startActivity(i);
}
Then I receive:
Intent i = getIntent();
dene = (Exhibit)i.getSerializableExtra("MyClass");
Here you can see that I am passing exhibits.get(position) to certain class and start the class as new activity, then the new activity receives it. So, how can I pass the object to another class (not this class) without starting it?
Thanks a lot!
If you don't want your object to be persisted during app launches, you can just set it as a static instance variable of your application class:
public class MyApp extends Application {
private static Exhibit sExhibit;
public static void setExhibit(Exhibit exhibit) {
sExhibit = exhibit;
}
public static Exhibit getExhibit() {
return sHexhibit;
}
}
// To set the object:
MyApp.setExhibit(myExhibit);
// To retrieve it
Exhibit myExhibit = MyApp.getExhibit()
If you don't want to extend your application, you can just do it in any class, or in your Exhibit model, wherever it would make the more sense.
If you want it to persist in between app launches and it's serializable, I would use the Shared Preferences to store it: http://developer.android.com/guide/topics/data/data-storage.html#pref
You can just call a method on the object:
otherObject.someMethod(dene);
Please check this LINK1 , LINK2 and LINK3

Categories