how to make my checkbox unchecked - android

I've got big problem with checkbox. I've got chceckbox in my MainAdapter(not Activity), I'm checking it and go to the next actvity by clicking on button. Then I return from DefaultActivity to MainActivity and I want checkbox to be unchecked. I also add that checkbox's logic is in Adapter in ViewHolder like this :
class ViewHolder {
TextView tv1;
TextView tvC;
ImageView ivT;
CheckBox chb;
}
and all logic is in getView method.If you don't understand something and you want me to help. Just ask what you want to get.

When you start your new Activity, do it calling startActivityForResult(). This will call a callback method once you close your second activity, so this way you make sure you'll enter that method once you finish() your newly opened Activity.
Once in there, simply find your view by id, and uncheck it. This is a sample code:
final Intent intent = new Intent(YourActivityThatContainsListViewDefinition.class, YourNewActivity.class);
startActivityForResult(intent, 1);
Afterwards, just override the onActivityResult() method.
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case 1:
CheckBox cb = (CheckBox) findViewById(R.id.your_checkbox_id);
cb.setChecked(false);
break;
}
}
---- EDIT ----
All this code would be outside your Adapter implementation - so, if you look at the code I provided, in the Intent, the first parameter is the context of the Activity that opens the second one. That means that those two overrides, you have to implement them in the Activity that calls the other (in your case, the MainActivity).
In your second Activity (DefaultActivity), you need to do nothing, just notify the first Activity (MainActivity) that it has to unckeck the CheckBox. To do so, you simply do something like this when you want to close DefaultActivity:
Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();
This way, you're notifying MainActivity that it should fire onActivityResult() and there's where you uncheck that CheckBox.

To uncheck your checkbox do:
chb.setChecked(false);

if is in adapter you can retrieve it in when getView() is called...
e.g.
holder.chb.setChecked(false);
with this the checkbox will be unchecked every time that de adapter is notified or created again

Related

How can I update the item of recyclerview when I come back to list in android

In my app I have an Activity and in this Activity, it shows a list of items with the help of RecyclerView, Adapter and fetches the items from a database.
In the database I have a table called Place with 7 Fields one of the fields is interest with Default Value 0 and Integer data type.
When I click an item in the list, this opens new Activity with all details of that item. One of the details is interest. The c
value of interest with Star icon. If the Value is 0 icon is Black otherwise it is Gold.
That part works fin. When I click the Star icon, the icon and the value of interest field in database successfully changes. However, my issue is :
When I come back to the Activity displaying the list of items and go back to that particular item, the changes of Star icon are not reflected. Example: first the icon is black, I change the icon by clicking on it to change to gold and update the value of interest field to 1. Only when I close the app completely and re-open again, the changes are shown correctly.
Any ideas?
You have to pass back changed Place object from All Details Activity to items list Activity and then notify and change the item in the adapter.
First, you have to start an activity with listening on results:
Intent intent = new Intent(this, AllDetailsActivity.class)
intent.putExtra("place", place);
startActivityForResult(intent, 1000)
When you change and confirm changes on All details screen you have to return changed object and result like this:
setResult(Activity.RESULT_OK, new Intent().putExtra("returned_place", place));
Then you have to implement onActivityResult inside Activity where you have a list of objects like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(request == 1000 && resultCode == Activity.RESULT_OK) {
adapter.changeItem(position, (Place) data.getExtras().get("returned_place"))
}
}
And adapter function for changing items should be something like this:
public void changeItem(int position, Place place) {
items.set(position, place);
notifyItemChanged(position);
}
You also have to save the position of the clicked Place object to know which cell you have to update.
You can call the notifyDataSetChanged() method from your adapter if you are not sure at what position of your data in the recyclerView you are in.
It is however better to call the notifyItemChanged(position) method from your adapter to only update the view for that specific recyclerView object.

Update recyclerview adapter onResume from Different Activity

So I have a fragment with recyclerview in it which is a direct child of my first Activity. In my recyclerview rows I have imagebutton. So for my second activity I have a feature where it can change the image of the imagebutton from my first activity's recyclerview. Can somebody help me?
Update:
My application has an addtocard feature. So in the first activity there's individual imagebutton for every row of my recyclerview. When i tap the imagebutton, the image will change and it will be added to the cart. I also have a database for the cart so the ID's of the product get inserted when it is added. If i tap the recyclerview row it will call the second activity and will display the product. So in the second Activity I also have the add to card feature. The problem is when I add the product in the cart inside the second activity i want to update the recyclerview in my first activity to indicate that it was already added.
You need some kind of shared state where the fragment can read from and the second activity can write into. Since I don't known what you use case is I would suggest that you use SharedPreferences for the first POC implementation.
In the onBindView of the adapter of the fragment you check the preferences for the image to use. It can be a R.whatever.id or a URI pointing to the image. In the second activity you set the key to the image resource depending on your requirements.
Or, this might be a better solution actually, pass the reference to the image in your data source of the adapter. Then whenever you need to update the image write the value in the data source and notify the adapter that there are some changes. If you don't use a content provider and are not observing the data source, then reload the contents in the onResume method of the fragment.
But to provide a more accurate answer we need more informations about your use case and the existing code.
Use Callback or Interface. Tigger it when you click and use that interface method to set the Change the Image. Actually I don't Understand Your Question properly. Can you please provide the Code. So i can give proper Answer. What i understand i gave the Answer.
You should pass your model object and list position to second activity.
FirstActivity.java
private static final int REQUIEST_ITEM_DETAILS = 150;
private void showItemDetails(Item item, int position) {
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra(SecondActivity.EXTRA_ITEM, item);
intent.putExtra(SecondActivity.EXTRA_POSITION, position);
context.startActivityForResult(intent, REQUIEST_ITEM_DETAILS);
}
SecondActivity.java
private Item item;
private int position;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
item = getIntent.getParcelableExtra(EXTRA_ITEM);
position = getIntent.getIntExtra(EXTRA_POSITION, -1);
}
/*
* finish your activity with onBackPressed function. so android device's
* Back button and your finishing operation will be same function.
*/
#Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra(EXTRA_ITEM, item);
intent.putExtra(EXTRA_POSITION, position);
setResult(RESULT_OK, intent);
super.onBackPressed();
}
and in your FirstActivity.java catch onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ITEM_DETAILS) {
if (resultCode == RESULT_OK) {
int position = data.getIntExtra(SecondActivity.EXTRA_POSITION, -1);
Item item = data.getParcelableExtra(SecondActivity.EXTRA_ITEM);
/* put setItem function to your adapter class. it will just replace given item with in your list item */
adapter.setItem(position, item);
adapter.notifyItemChanged(position);
}
}
}

how to pass position of listview items to another listview adapter and set text of textview according to position

i have Two list-view activity.
In first list-view activity i have link to open second list-view activity. In Second list-view i have 5 rows. i want to pass the position of second list-view item to first first activity and according to position set text for only second row.
i can pass static data form second list-view activity to first list-view activity with finish(), but i don't know how to set it to selected row and i don't know how to pass position of second list-view with finish().
Anyone can help then please write code. for this
i want to set text for second row of first list-view.also write for this.
Thanks.
You could utilize startActivityForResult() to launch the second activity. By calling that instead of startActivity() your first activity will be noticed when the second one completes.
In your FirstActivity, launch the second activity using this:
Intent launch2ndActivity = new Intent(this, SecondActivity.class);
startActivityForResult(launch2ndActivity, 610);
Then on your SecondActivity:
// Do what do you need to do here.
//I assume you would need to get the position of your ListView upon item click.
..
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent resultIntent = this.getIntent();
resultIntent.putExtra("clicked_row", position);
SecondActivity.this.setResult(RESULT_OK, resultIntent);
finish();
}
});
The last thing you'd need to do is specify how your FirstActivity should react upon recieving the result from SecondActivity:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case 610:
if (resultCode == RESULT_OK) {
Bundle res = data.getExtras();
int clickedRow = res.getInt("clicked_row");
// Do what you want with that value..
}
break;
}
}

Android - Save text and launching an Activity with the same button

I'm currently working in an Activity which saves text from three EditViews and constructs a SQL query. After that, the query is given to another activity to search and display the results.
Right now I've got two buttons, one to save the query, inside an onClickListener and another button to start the second activity:
searchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//First the data from the editviews is saved
EditText searchName = (EditText) findViewById(com.pedro.books.R.id.search_name);
search_name = searchName.getText().toString();
searchName.setText("");
EditText searchAut = (EditText) findViewById(com.pedro.books.R.id.search_aut);
search_aut = searchAut.getText().toString();
searchAut.setText("");
EditText searchYea = (EditText) findViewById(com.pedro.books.R.id.search_yea);
search_yea = searchYea.getText().toString();
searchYea.setText("");
//here i construct the query
});
And with an onClick in the xml code, I start the activity with another button:
public void ReadResults(View view){
//The query is given to the ReadActivity to display the results
Intent intent = new Intent(this, ReadActivity.class);
intent.putExtra(ReadActivity.EXTRA_QUERY, query);
startActivity(intent);
}
I've tried with the same button for both without changing anything and it obviously doesn't work, and I've also tried to start the activity inside the onClickListener, but I got this error: "The constructor Intent(new View.OnClickListener(){}, Class) is undefined"
Is there a way to start the activity inside the onClickListener or to stop the second activity to start until the query is saved?
Thanks in advance!!
if you are putting the intent in the onClick of a button you cannot use this you need to use YourActivity.this to properly get context.
this in your button is your OnClickListener like the error says
Where/How do you call your ReadResults method? On a side note - does it need to be public? If you call it inside the clickListener handler then that's wrong.
1) Extract your code out of the clickListener handler and have it into a private method within your activity class.
2) Your clickListener should only call that private method.
3) You could have your 3 editboxes as memebers of your activity and instantiate them onCreate() instead of getting them each time you click the button, it's expensive parsing the UI if not necessary.

Difficulty ticking checkbox inside BaseAdapter

I have a ListView with each row consisting of a TextView and a CheckBox.
The user is allowed to click on each TextView, and once he does a Dialog is presented to him where he is expected to either choose Yes or No.
When he chooses Yes, another activity is presented to him where he needs to enter data.
I am implementing all this inside a base adapter class, so inside the base adapter I created the AlertDialog and handled it's OnClickListeners.
Here is the problem: I need to use startActivityForResult in order to get back the data that the user will enter in the new activity, and like I said above, I have done so in the BaseAdapter. Now, how can I get the data from the new activity back inside the BaseAdapter? I researched various sources and found out that one cannot start an Intent directly from a BaseAdapter class, but instead needs to reference the Intent to the calling activity like below:
((Activity) mContext).startActivityForResult(intent, 1);
This would then result in having the the onActivityResult() method in the adapter Activity and not inside the BaseAdapter.
I need to leave the code inside the BaseAdapter for various reasons.
The value I need to retrieve is a simple boolean that if it results to true, will tick the CheckBox next to the selected TextView.
How could I implement this? What alternatives do you guys suggest? I tried creating a method inside the BaseAdapter so that I can call it from the "main" Activity at the OnActivityResult() but the CheckBox that I need to tick is returning null at that point; the reason being quite obvious.
I would appreciate any help on this matter.
Inside BaseAdapter class
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Materials");
builder.setMessage("Did you require any materials to fix this error?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
String clickedError;
clickedError = holder.text.getText().toString();
Intent intent = new Intent(mContext, Material.class);
intent.putStringArrayListExtra("materialList", materialList);
intent.putExtra("clickedError", clickedError);
intent.putExtra("repairID", repairID);
((Activity) mContext).startActivityForResult(intent, 1);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
if(checkbox.getTag() == v.getTag())
{
checkbox.setChecked(true);
}
}
});
builder.show();
// Method to tick the checkbox.
public void TickBox(CheckBox cb)
{
cb.setChecked(true);
}
The main activity containing OnActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode == RESULT_OK)
{
boolean moreThanOne = data.getBooleanExtra("moreThanOne", false);
if(moreThanOne)
{
CheckBox cb = adapter.checkbox;
adapter.TickBox(cb);
}
else
{
// ....
}
}
}
Create a Callback listener inside your BaseAdaper, inside that method write Intent code,
For updating it later Create a method inside the BaseAdapter which will gets called from onActivityResult.
u can update ur checkbox from ur activity.... TRY this
when user click the listview send the listItem position to ur new activity.AND return the same value in ur old activity in onActivityResult.
then in onActivityResult **
use the same layout that u r using when creating listView
RelativeLayout itemLayout = (RelativeLayout)mListViewObject.getChildAt(i);
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.checkBox);
cb.setChecked(RESULT);

Categories

Resources