I am writing an Android application with a main page being a list of categories, and then beside each category there is an edit button.
When edit button is clicked, an entry form with the "category" properties filled up is supposed to be shown.
How to pass the category ID or name to the new created form so that it could load from database the properties of selected category
I have no issue with getting the selected category name, by using the code below:
public String getSelectedItem(View view) {
LinearLayout vwParentRow = (LinearLayout) view.getParent();
TextView child = (TextView) vwParentRow.getChildAt(0);
return (String) child.getText();
}
//edit button clicked
public void editCategoryHandler(View view) {
Intent i = new Intent(this, EntryCategory.class);
startActivity(i);
}
I am sure you have just passed String array or ArrayList to display inside that Category List, instead you should prepare an ArrayList of Category objects, and then whenever user clicked a particular item from a category list, get that object at that position after implementing OnItemClickListener, and then pass this received object to the desired activity.
In short, get object for a clicked item and then pass this object.
To pass the value to a new activity, you have to use putExtra() and getExtra().
Please refer the following page
How to use putExtra() and getExtra() for string data
Related
I have a little app that I'm working on that displays a list of monsters. You can click on each monster to see more info about that monster.
The list is in my xml layout file in a TableLayout.
But looking at the code, it's easy to see how things can get out of hand the more monsters I add.
I was wondering, if there is a better way of determing which monster was clicked and how to get the needed info passed from the list of monsters view to the monster info view.
Here is my code so far. It works fine, but I know it's not really good because I'd have to add a new case statement for each new monster I add to the list.
public void AddNewView(View view) {
//get selected button view
switch(view.getId())
{
//get textview & imageview monsters from xml
//get monster picture and monster info from xml
case R.id.showMonsterButton1:
iv = (ImageView) findViewById(R.id.monster1_icon);
tv = (TextView) findViewById(R.id.monster1_info);
ShowMonsterInfoView(tv, iv);
break;
case R.id.showMonsterButton2:
iv = (ImageView) findViewById(R.id.monster2_icon);
tv = (TextView) findViewById(R.id.monster2_info);
ShowMonsterInfoView(tv, iv);
break;
case R.id.showMonsterButton3:
iv = (ImageView) findViewById(R.id.monster3_icon);
tv = (TextView) findViewById(R.id.monster3_info);
ShowMonsterInfoView(tv, iv);
break;
case R.id.showMonsterButton4:
iv = (ImageView) findViewById(R.id.monster4_icon);
tv = (TextView) findViewById(R.id.monster4_info);
ShowMonsterInfoView(tv, iv);
break;
}
}
public void ShowMonsterInfoView(TextView tv, ImageView iv) {
Intent intent = new Intent(this, DisplayMonsterInfo.class);
String text_tag = tv.getTag().toString();
String image_tag = iv.getTag().toString();
Bundle extras = new Bundle();
extras.putString("name", text_tag);
extras.putString("avatar", image_tag);
intent.putExtras(extras);
startActivity(intent);
}
Save your list of monsters in a List object and pass it in an ArrayAdapter object which will be used to populate the ListView. In the List object you can dynamically add more monsters on the go using obj.add(monster_name). In the setOnItemSelected method save the the selected monster in a static String object, which can be accessed in any Activity using MonsterListActivity.monster_name. Use it to pull data from file/database.
As per how to get which monster was clicked. In case if you are using RecyclerView and in case if ListView just setOnItemClickListener. By position of clicked item you can get object from list with monsters objects and then get id from there.
Put data about monster in data base and pass id of monster to another activity where you will fetch data from DB.
What you can do is simply save data to DB and assign a unique id to every monster.On the click you pass the id to the next activity and fetch the data about the monster their.
On the other hand if you don't want to save the data, pass the data with the click intent to other activity(i.e if you have the data in the first activity).
As you have to add views, I recommended you use recycler view where you can simply add the items in the list you are providing and call notifyDataSetChanged().Using table layout for this might not be a good idea.
hi i have RecyclerView in that when user click on one row i am making changes in ui and updating view now i want when user click one one icon of that row it opening 2nd activity in that i am passing object of that position . and on 2nd activity if tick it i am not able to update view of that RecyclerView .. any way ? ya by doing static arreylist i can do it but cant do static as there is some problem .. part form static other way
icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent n = new Intent(mContext, PlayerInfo.class);
Players mData = (Players) v.getTag();
((Activity) mContext).startActivityForResult(n, 1111);
}
});
As you code shows,you can start second activity by call startActivityForResult and pass your data to it.
when data changed in 2nd activity,you save the new data in one object and when the activity finishes,you pass it to first activity,and you also need to add some code to handle the new object from 2nd activity in onActivityResult,just copy data to your old data for specific position.
I am working on a CRM app in android, in which, I am showing details of all contacts in a list view. Now, my requirement is when I click on a particular item in the list, it should only display the details about selected contact, such as, name, address, email, etc.. The data is coming from XML file, which I am parsing using SAX Parser. How can I query a XML to get selected data?
You are filling the ListView using Adapter right? Now you can get the item at the selected view inside the ListView and pass this item to an Activity.
E.g. inside your Adatper class implement the onItemClickListener:
public void onItemClick(AdapterView<?> a, View v, int position, long l) {
// Remembers the selected Index
Data item =getItem(position);
Intent intent = new Intent(getApplicationContext(), DetailedActivity.class);
intent.put("object",item);
startActivity(intent);
}
Note: the item "Data" class should implement the Parsable interface so it can be passed to the Activity in your DetailedActivity onCreate method get that object and update the UI based on its Values.
I am making an android app in which i have an activity X that displays a list and a button. Activity X calls a listview to display that list. Each list item has a number(textview) and a checkbox. I used a setonclicklistener on the checkbox, so whenever the checkbox is checked i am storing the number associated with it in a string. Now i want that whenever i click the button the msg activity should start and the numbers to be sent are the ones that are checked.
I am using the following code to start the msg activity in my X activity.
Intent msgIntent = new Intent(Intent.ACTION_VIEW, Uri
.fromParts("sms", msgnumbers, null));
msgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(msgIntent);
Now "msgsnumbers" variable is present in my listview. How do I pass it to this activity X??
I found the same question here but with no appropriate solution.
-Thanks in advance
Intent in = new Intent(Quote.this, Purchase Ysn.class);
in.putExtra("price", salesprc);
public static String price = "price";
if (getIntent().getExtras().containsKey(price)) {
purces_nbcpy = getIntent().getExtras().getDouble(price);
}
onItemClickListener for ListView has a param position that tells you what position has been clicked.
so if you are using an ArrayList (for eg) to provide values for listItems in adapter you can use this inside onItemClickListener
MyBeanObject object=arraList.get(position);
//use getters of object to retrieve values and pass it as intent
//where arrayList may be your list of objects MyBeanObject
I've listed some items in list that extends Activity and the list are listed using Custom Adapter. My question is, this is my xml File I've added some items to this spinner. How can i get the spinner values to next layout. Anyone knows then please tell me? Advance thanks.
I'm not clear on what you're actually asking here, but as a guess there are two possible things you're asking
How to get the currently selected value out of the Spinner
How to set the same value to a spinner in the next layout
1.
Is simple enough
((Spinner)findViewById(R.id.spinner1)).getSelectedItem()
Will return the object selected by your spinner.
Is slightly more complex, you'll need to determine what index in the data supplied corresponds to the result you get from getSelectedItem(), for example if you had an array of Strings then you could search through until you found the index and then set it on the new spinner.
For example:
String[] options = new String[] {"One","Two","Three","Four"};
String val = (String)((Spinner)findViewById(R.id.spinner1)).getSelectedItem();
//.......pass this to a layout/activity etc.........
for (int i=0; i<options.length; i++)
{
if (options[i].equals(test))
{
((Spinner)findViewById(R.id.spinner2).setSelection(i);
break;
}
}
But your best bet would be to try and explain more clearly what you're asking.
first you have to select data from spinner using
spinnerobject.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id)
{
Spinner spinnerobject = (Spinner) findViewById(R.id.Spinner02);
string value = (String)spinnerobj.getSelectedItem();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
then u hava to use intent for sending it to next activity..
Use
Intent.putExtra(..):
intent.putExtra("keyName", "somevalue");
This method is overloaded and takes various types as second argument: int, byte, String, various arrays..
To get the data out use appropriate getXYZExtra(). For String this is:
getStringExtra(String keyName)
First thing :: you can pass value from one activity to second activity not one layout to second
second :: if you need to pass value from one activity to second use
first activity::
activity.putExtra("lastpage", lastscore5);
*here lastpage is key which unique for aplication
second activity::
Intent i1 = getIntent();
var = i1.getIntExtra("lastpage", 1);