how to delete an item in recycleview from another activity? - java

I am working on an expense tracker app. When I click on item in recycleview, it takes me to another activity where I have two buttons : update and delete. The issue is I don't get the idea how to delete item from recycleview, which is in another activity, when delete button in that activity is pressed. Actually, I also searched and looked up other similar questions and answers on stack overflow however that didn't help me. If someone can help me on this, I will appreciate it greatly.
This is my code where I am passing the position of my item :
final int pos = holder.getAdapterPosition();
holder.date.setText(MyModel.getDate());
holder.note.setText(MyModel.getNote());
holder.Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context,Update_transaction.class);
i.putExtra("key",myViewsList.get(pos).getId());
i.putExtra("pos",holder.getAdapterPosition());
context.startActivity(i);
}
});
This is the code where I am retrieving the data in another activity :
Bundle bundle = getIntent().getExtras();
Update_db = new DataSaver(this);
if(bundle != null)
{
id = bundle.getString("key");
pos = bundle.getString("pos");
Toast.makeText(this, "pos : " + pos, Toast.LENGTH_SHORT).show();
}

For interaction between activities, you can use the LocalBroadcastManager. You can send a broadcast from one activity and receive it in another.
Send broadcast
val intent = Intent("ACTION")
intent.putExtra("needUpdate", true)
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent)
Receive broadcast
1.Create a listener
val myBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
println("Your value: " + intent?.getStringExtra("needUpdate"))
}
}
Register
override fun onResume() {
// …
val filter = IntentFilter("ACTION")
LocalBroadcastManager.getInstance(applicationContext).registerReceiver(myBroadcastReceiver, filter)
}
Cancel registration
override fun onPause() {
// …
LocalBroadcastManager.getInstance(applicationContext).unregisterReceiver(myBroadcastReceiver)
}
More details: https://developer.android.com/guide/components/broadcasts

Related

How to generate multiple cardview based on user input in android

I'm trying to design a page where address are stored in recycler view -> cardview.
When the user clicks the add address button from the Activity A the user is navigated to the add address page in Activity B. Here the user can input customer name, address line 1 and address line two.
And once save button is clicked in Activity B, a cardview should be created under the add address button in the Activity A.
This design is just like the amazon mobile app add address option.
Example: If a end-user has one address(one cardview will be present) and wants to add one more address(second address). Then another cardview will be created below the existing cardview with the new address.
Actual result as of now: The second address is being populated in the first cardview.
Expected result: When user enters a new address a new cardview should be created below the existing one.
Code in the Activity A
public class ProfileManageAdressFragment extends AppCompatActivity {
RecyclerView recyclerView;
ProfileManageAddressRecyclerAdapter adapter;
ArrayList<ProfileManageAddressGetterSetter> reviews;
private Button addAddress;
private String customer_name, address_one, address_two, city, state, pincode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_profile_manage_adress);
addAddress = findViewById(R.id.addNewAddress);
addAddress.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ProfileManageAdressFragment.this, AddNewAddress.class);
startActivityForResult(intent, 1);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1) {
if(resultCode == RESULT_OK) {
customer_name = data.getStringExtra("customer_name");
address_one = data.getStringExtra("address_one");
address_two = data.getStringExtra("address_two");
city = data.getStringExtra("city");
state = data.getStringExtra("state");
pincode = data.getStringExtra("pincode");
reviews = new ArrayList<>();
reviews.add(new ProfileManageAddressGetterSetter(customer_name, address_one, address_two, city, state, pincode));
recyclerView = findViewById(R.id.addressRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(ProfileManageAdressFragment.this));
adapter = new ProfileManageAddressRecyclerAdapter(ProfileManageAdressFragment.this, reviews);
recyclerView.setAdapter(adapter);
}
}else if(resultCode == RESULT_CANCELED) {
Toast.makeText(ProfileManageAdressFragment.this, "Cancelled", Toast.LENGTH_SHORT).show();
}
}
}
Code in Activity B
public class AddNewAddress extends AppCompatActivity {
private EditText customer_name, address_one, address_two, city, state, pincode;
private Button add_address;
private String sCustomer_name, sAddress_one, sAddress_two, sCity, sState, sPincode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_address);
customer_name = findViewById(R.id.customerName);
address_one = findViewById(R.id.addressOne);
address_two = findViewById(R.id.addressTwo);
add_address = findViewById(R.id.addAddress);
city = findViewById(R.id.city);
state = findViewById(R.id.state);
pincode = findViewById(R.id.pincode);
final ProfileFragment profileFragment = new ProfileFragment();
add_address.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//setFragment(profileFragment);
if(customer_name.getText().toString().equals("") || address_one.getText().toString().equals("") ||
address_two.getText().toString().equals("") || city.getText().toString().equals("") ||
state.getText().toString().equals("") || pincode.getText().toString().equals("")
) {
Toast.makeText(AddNewAddress.this, "Please input all fields", Toast.LENGTH_LONG).show();
}else {
sCustomer_name = customer_name.getText().toString();
sAddress_one = address_one.getText().toString();
sAddress_two = address_two.getText().toString();
sCity = city.getText().toString();
sState = state.getText().toString();
sPincode = pincode.getText().toString();
Intent intent = new Intent(AddNewAddress.this, ProfileManageAdressFragment.class);
intent.putExtra("customer_name", sCustomer_name);
intent.putExtra("address_one", sAddress_one);
intent.putExtra("address_two", sAddress_two);
intent.putExtra("city", sCity);
intent.putExtra("state", sState);
intent.putExtra("pincode", sPincode);
//startActivity(intent);
//startActivityForResult(intent, 1);
setResult(RESULT_OK, intent);
finish();
}
}
});
}
}
Kindly let me know if additional information is required. Million thanks in advance for solutions! :)
There are many things you have to handle
In ProfileManageAdressFragment
Naming Convention matter more extended AppCompatActivity and the class name is ProfileManageAdressFragment.
Button object created globally that not required, make it local obj.
same String customer_name and many more obj you're using inside onActivityResult so don't need to initialize globally make it local Obj.
In onActivityResult every time you reinitialize the recyclerView and adapter that not required. initialize recyclerView and adapter inside onCreate and when you get Data in onActivityResult add data to ArrayList and call adapter.notifyDataSetChange.
In onActivityResult using nested condition for request code and ResultCode make it on single condition Like. if (requestCode == 1 && resultCode == RESULT_OK){ code body }else {}
In AddNewAddress
All globally initialize objects not required it takes memory only. so make it locally if you're not using outside of the method.
Instead checking value like this customer_name.getText().toString().equals("") , android provide one class TextUtil and TextUtil has one method isEmpty use that method.
So, as I think, the problem is in this lines
reviews = new ArrayList<>();
reviews.add(new ProfileManageAddressGetterSetter(customer_name, address_one, address_two, city, state, pincode));
You're creating a new object of ArrayList with one item, instead of updating existing one. After that you're creating another object of adapter, instead of using existing one and refreshing it's data. So that's why you can't see another CardView populated.
Try add to your adapter next function:
void addItems(items: ArrayList<ProfileManageAddressGetterSetter>) {
this.items.addAll(items);
notifyDataSetChanged();
}
And when you receiving a result from ActivityB - insert it to your adapter with help of addItems(items: ArrayList<ProfileManageAddressGetterSetter>) function.
For better usage of adapter keep it's variable as global variable in your Activity class and call it whenever you need.

Pass int to another Activity

Before you flame me:
I know there are uncountable tutorials out there, and I know myself how to pass data to another ACtivity, just like that.
In my case that's diffrent tho. "Usually" data is passed to another activity through Intents, Bundles ecc and the other Activity is started.
Here's my case:
I have an Item with 4 parameters (Image, String,String, int)
In an AdapterClass I have a PopUpView which retakes those 4 parameters.
What I'd like to achieve is the following:
With the click of a button, the 4th parameter, the int should be sent to the Main activity and inserted in a textView inside the MainActivity, without (here's the main diffrence between this and the other questions)launching the Main Activity.
How can this be done?
TIA.
use BroadcastReceiver to send that 4th int to MainActivity
in PopupView do these:
Intent intent = new Intent("SOMEACTION");
intent.putExtra("4th_int", value);
activity.sendBroadcast(intent);'
//In MainActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter("SOMEACTION");
this.registerReceiver(mReceiver , filter);
}
#Override
public void onDestroy() {
super.onDestroy();
this.unregisterReceiver(this.mReceiver );
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == "SOMEACTION") {
// retrieve the 4th int value and update something in MainActivity
}
}
};

How to set OnClickListener to get position of different ArrayLists using same Adapter java class?

I am making an Android app, and currently have 3 different activities. One that displays 'Recent' images from Flickr in a Recyclerview, another that displays 'Interesting' images from Flickr in a Recyclerview, as well as a 'Details' activity which displays a larger image view and more information about a particular photo when it is clicked.
I have two ArrayLists: recentImageList and interestingImageList, which use the same Adapter class to display the different images and their attributes (title, owner etc.) in their Recyclerviews, depending on the activity.
I have these two activities working and showing the correct content, and when I click on a 'recent' photo, it displays the same 'recent' photo on the 'details' page.
However my problem is that when an 'interesting' image is clicked, it passes the position of the 'recent' image to the Intent instead of the position of the 'interesting' image I clicked on. Therefore this displays the 'recent' image in the 'Details' activity, and not the 'interesting' photo I clicked on.
I am unsure how to pass the correct position of the interestingImageList array item in my onClickListener, so that the correct corresponding image is shown in my 'details' activity. Do I need some sort of if statement in my OnClickListener to determine which arraylist position is passed to the intent?
I'd be grateful for any help you guys can give. See the code for my ImageListAdapter class below:
public class ImageListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
ArrayList<ImageInfo> recentImageList;
ArrayList<ImageInfo> interestingImageList;
private int whichActivity;
public ImageListAdapter(Context context, ArrayList<ImageInfo> recentImageList,
ArrayList<ImageInfo> interestingImageList, int whichView) {
this.context = context;
this.recentImageList = recentImageList;
this.interestingImageList = interestingImageList;
whichActivity = whichView;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.cell_image_card, parent, false);
return new ImageViewHolder(itemView);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
if (whichActivity == 0) {
((ImageViewHolder) viewHolder).populate(recentImageList.get(position));
ImageInfo recentInfo = recentImageList.get(position);
} else if (whichActivity == 1) {
((ImageViewHolder) viewHolder).populate(interestingImageList.get(position - recentImageList.size()));
ImageInfo interestingInfo = interestingImageList.get(position);
}
}
#Override
public int getItemCount() {
return recentImageList.size() + interestingImageList.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public TextView imageTitle, ownerTitle, tags;
public NetworkImageView mainImage, ownerImage;
public ImageViewHolder(View itemView) {
super(itemView);
imageTitle = (TextView) itemView.findViewById(R.id.imageTitle);
ownerTitle = (TextView) itemView.findViewById(R.id.ownerTitle);
mainImage = (NetworkImageView) itemView.findViewById(R.id.mainImage);
mainImage.setOnClickListener(onClick);
ownerImage = (CircularNetworkImageView) itemView.findViewById(R.id.ownerImage);
}
public void populate(ImageInfo imageInfo) {
ownerTitle.setText(imageInfo.owner);
imageTitle.setText(imageInfo.title);
mainImage.setImageUrl(imageInfo.url_m, NetworkMgr.getInstance(context).imageLoader);
ownerImage.setImageUrl(imageInfo.ownerPic, NetworkMgr.getInstance(context).imageLoader);
}
View.OnClickListener onClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, DetailsActivity.class);
int position = ImageViewHolder.this.getLayoutPosition();
intent.putExtra("PHOTO_POSITION", position);
context.startActivity(intent);
}
};
}
}
You need two different instances of your adapter, each one for each list this seems like a wrong implementation
Each recyclerview should have its own adapter instance.
e.g.
new
ImageListAdapter(this, recentImageList, null, whichView);
and
new
ImageListAdapter(this, null , interestingImageList,whichView);
For two different RecyclerView you will need two instance of the adapter. To go to the details activity, it will be better to get the selected or clicked item position in activity and start the details activity from present activity. To get the selected position from the adapter you can use interface. Here is an open source RecyclerView-Listener library to get the selected item position. You can use it or understand the concept and implement yourself.
Thank you everyone for your help! I've fixed it!
I realised that my original code was correct, however the line in my Details Activity:
photo = NetworkMgr.getInstance(this).recentImageList.get(photoPosition);
was the problem, as this line retrieved only the position for the recent images and didn't account for the interesting images. Therefore, in my OnClickListener I used an if statement for the two whichActivitys and putExtra a number to determine which arraylist needs to have its position retrieved in Details activity. See below:
ImageListAdapter -
View.OnClickListener onClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (whichActivity == 0) {
Intent intent = new Intent(context, DetailsActivity.class);
int position = ImageViewHolder.this.getLayoutPosition();
int imageListCode = 1;
intent.putExtra("PHOTO_POSITION", position);
intent.putExtra("IMAGELIST_CODE", imageListCode);
context.startActivity(intent);
}
else if (whichActivity == 1) {
Intent intent = new Intent(context, DetailsActivity.class);
int position = ImageViewHolder.this.getLayoutPosition();
int imageListCode = 2;
intent.putExtra("PHOTO_POSITION", position);
intent.putExtra("IMAGELIST_CODE", imageListCode);
context.startActivity(intent);
}
}
};
DetailsActivity -
Intent intent = getIntent();
photoPosition = intent.getIntExtra("PHOTO_POSITION", 0);
code = intent.getIntExtra("IMAGELIST_CODE", 0);
if (code == 1) {
photo = NetworkMgr.getInstance(this).recentImageList.get(photoPosition);
}
else if (code == 2) {
photo = NetworkMgr.getInstance(this).interestingImageList.get(photoPosition);
}

Finish android activity from another with Kotlin

I'm trying to finish an activity from another (android) with kotlin. I know the wat to do it with java is with the following code (https://stackoverflow.com/a/10379275/7280257)
at the first activity:
BroadcastReceiver broadcast_reciever = new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent intent) {
String action = intent.getAction();
if (action.equals("finish_activity")) {
finish();
// DO WHATEVER YOU WANT.
}
}
};
registerReceiver(broadcast_reciever, new IntentFilter("finish_activity"));
On the other activity:
Intent intent = new Intent("finish_activity");
sendBroadcast(intent);
For some reason converting the java activity to kotlin doesn't give a valid output, if someone could give me the correct syntax to do it properly with kotlin I will appreciate it
kotlin output (first activity) [OK]:
val broadcast_reciever = object : BroadcastReceiver() {
override fun onReceive(arg0: Context, intent: Intent) {
val action = intent.action
if (action == "finish_activity") {
finish()
// DO WHATEVER YOU WANT.
}
}
}
registerReceiver(broadcast_reciever, IntentFilter("finish_activity"))
kotlin output (2nd activity) [OK]
val intent = Intent("finish_activity")
sendBroadcast(intent)
ERROR: http://i.imgur.com/qaQ2YHv.png
FIX: THE CODE SHOWN IS RIGHT, YOU JUST NEED TO PLACE IT INSIDE THE onCreate FUNCTION
Simple code to finish a particular activity from another:
class SplashActivity : AppCompatActivity(), NavigationListner {
class MyClass{
companion object{
var activity: Activity? = null
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MyClass.activity = this#SplashActivity
}
override fun navigateFromScreen() {
val intent = Intent(this,LoginActivity::class.java)
startActivity(intent)
}
}
Now call SplashActivity.MyClass.activity?.finish() from another activity to finish above activity.
The error Expecting member declaration is there because you wrote a statement (the function call) inside a class. In that scope, declarations (functions, inner classes) are expected.
You have to place your statements inside functions (and then call those from somewhere) in order for them to be executed.

pass integer from listview to another activity

I want to pass the integer (id) from listview (when it is clicked) to TheEndActivity.java . But my code cant retrieve the integer properly.
UsernameList.java (it is the class where the integer comes from)
public void onListItemClick(ListView parent, View view, int position, long id) {
Intent i = new Intent( this, TheEndActivity.class );
i.putExtra( "int", (int)id);
Log.e(LOG_TAG, "id value "+id);
startActivity( i );
Intent intent = new Intent(UsernameList.this, QuizAppActivity.class);
startActivity(intent);
}
TheEndActivity.java (Its the class where i need to pass the integer here. it is created inside onCreate method)
Intent i = getIntent();
final int number = i.getIntExtra("int", -1);
and i need to use the integer (id) which i save it into database (the operation is done in TheEndActivity.java)
finishBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
helper.insertMark(currentGame.getRight(), number );
Log.e(LOG_TAG, "id value: " + number );
Intent i = new Intent(TheEndActivity.this, MainMenu.class);
startActivity(i);
}
});
Solved
The problem is i called 2 activities. Thank you guys
problem seems to be in calling 2 startActivities.
Are TheEndActivity and QuizAppActivity both activities?
You have to change TheEndActivity into an IntentService or AsyncTask if its something doing background activity like saving things into database.
If it's not, then launch only TheEndActivity and allow user to navigate to QuizAppActivity from there.
Also try changing
Intent i = new Intent( this, TheEndActivity.class ); to
Intent i = new Intent( UsernameList.this, TheEndActivity.class );
You are calling startActivity with an intent that doesn't have the int as an extra.

Categories