I have a custom array adapter which has a imageview, 2 textviews an edittext and a checkbox. I need to get the text from my textviews and my edittext then send that data to another activitya textview when the checkbox is checked.
How can I set up a method to check if the edittext is empty as I need the checkbox only to trigger when there is a value in the edittext.
How can I get the data sent through to another activity.
Thanks
Take your EditText variable and call getText() on that variable to check if it is empty.
E.g.
if (myEditText.getText().toString() == "") {
// trigger checkbox
}
To pass data from one activity to another you use intents. See here: How do I pass data between Activities in Android application?
Related
I have a cart program, in activity is simple, there was a recyclerview and a button. In recycler view I can edit its stock.
Now, I want to get that product_name and product_stock from that I've ever clicked and make changes.
Now, when every stock has been clicked, I want click button on activity, so I want that data I've ever clicked on recyclerview stored to array, so that button can do their action / function. Can you guys lead me, how to import data from recyclerview to its activity itself.
It was Android program to do shopping cart, so I click the data in recycler view it stored to activity array.
I don't have any idea how to store that from recyclerview to activity's array.
My expected result, should be, when I click buy button on activity, every changed stock on recyclerview is shown.
You can simply implement an interface in your adapter like this for getting the values from clicked position in recycleview.
public interface FetchRecyclerViewItems{
void getItems(String product_name,String product_stock);
}
And simply create a setter method for this interface in adapter like this,
private FetchRecyclerViewItems fetchRecyclerViewItems;
public void setFetchRecyclerViewItems(FetchRecyclerViewItems fetchRecyclerViewItems){
this.fetchRecyclerViewItems = fetchRecyclerViewItems;
}
Then set the values like this on your click like this in OnBindViewHolder,
your_view.setOnClickListener(new View.SetOnClickListener)....{
....
fetchRecyclerViewItems.getItems(product_nameFromPosition,product_stockFromPosition);
}
And implement this interface in your activity and you will get the product_name,product_stock values there.
Make sure to initialize the FetchRecyclerViewItems in your activity.
I have 2 preference pages. Depending on a checkbox displayed on the preference page "one" I want to display a CheckBoxPreference or not display it on the page "two". I know that I should add on the activity of the page "two" a way to handle if the checkbox in the page "one" is checked or not. But i don't know how to refer on that checkbox.
Save the checkbox's state by getting a reference to it programmatically, save it's state as a preference, pass it in an intent (via intent.putExtras();) or save it in SharedPreferences, then on the second activity check if the CheckBoxPreference.isChecked() then conditionally load preferences. Check here for more information.
You could use a class named StaticValues and add a variable that it is boolean.
public static boolean isCheckBoxChecked;
When the checkbox is checked set this variable true:
StaticValues.isCheckBoxChecked = true;
When you are displaying preference page 2 you can check this variable and deside if you diplay the next checkbox or not.
I have a XML layout which has two edit text fields, one for "title" and the the other for "story". When the user enters his data in these text fields and presses the back button the entry gets saved in a list view as the title set. The list view is present in an activity say A1. Now A1 extends Activity.
Whenever an item in the list is "long clicked" a context menu appears with edit, delete and read buttons. If the edit button is pressed I need to open another activity which can edit the data entered in the text fields corresponding to the item clicked. Also I'd be needing the id and the position of the item clicked in the list.
I am using list variable of type ListView to add my adapter. Also I am checking the edit, delete and read options of the context menu in the `public boolean onContextItemSelected(MenuItem item)' method.
How can I get the id and the position of the item clicked from here?
in adapter you make a getter and setter of your item. When long click listener, put setter your item in there.
You should store your "title" and "story" in database and you can get it form database in a new activity
I would think the best way to do this is to create an instance variable (declared after class definition and before onCreate) for the listview and assign the list view to it in onCreate(). Then you will be able to access the listview from your onContextItemSelected() method and pass them to your new activity
If you could post some of your code we should be able to help more.
I second Th0rndike's comments above. It is much easier to help someone if the question is readable and there is a good chance the answer will be accepted.
Everything in my Activity is working fine. I'm able to load all the values fine and show in every EditText except one. The sequence of the actions on onCreate is
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.rule_editor);
initComponents(); //references all private objects to each object in activity layout
populateCategorySpinner(); //populates a category spinner
loadRule(); //loads the rule from a singleton on the basis of an extra data passed from parent activity
attachEvents(); //attaches event to each object
Log.d("", txtIdentifierString.getText().toString());
}
The problem is in one of the EditText. The reference in code is txtIdentifierString. The values that I set (by setText()) on it is not showing up in activity on runtime.
Logcat shows up a value from the Log.d method call in last line of the onCreate method, but its not visible in the EditText txtIdentifierString. The EditText box is visible, I can focus on it and type in a value as well.
Does anyone has any idea about it?
I'm answering my own question.
I debugged the codes and found out that OnItemSelectedListener on a spinner had to reset the EditText in question. The change is triggered before I Logged the value
I created a spinner populated with values from a string array in my "strings.xml. When a user selects an item in the spinner I would like it to redirect them to that page, and then be able to return to the main page via a back button.
How do you send them to another page via a spinner selection?
Add an on tem selection listener. In the listener's onItemSelected method, you'd use position to determine the selected position in the array. Based on the position, you know which string was clicked. Then you'd create an Activity for each "page" and start the appropriate activity.