OnePlus Indian Ban Lifted, Android One Expanding – XDA TV

Android 5.0.1 Lollipop is available for the Nvidia SHIELD Tablet! That and … more

Nvidia and Robotics w/ Barrett Williams – XDA:DevCon 2014

Robotics continues to dominate our videos from this years XDA:DevCon 2014 in … more

Speed Up Deodexing with JoelDroid Batch Deodexer

Android 5.0 Lollipop brings many under-the-hood changes like changing the default compiler … more

Top 5 Apps for 2014 + Top Xposed Mod of 2014 – XDA TV

Its that time of year where you look back and remember the things that happened … more

Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

ArrayList<Integer> 's remove method confusing?

OP arpitkh96

22nd June 2014, 04:53 PM   |  #1  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,127 posts
Join Date:Joined: Feb 2012
Donate to Me
More
Suppose I added a digit '1' to the arraylist<Integer> which is at position 0.So when I call remove method like this.
array.remove(1);
It gives an exception.although the method remove(object) exists.so I how can I remove the object(integer) by not using the position

Sent from my GT-S5570 using XDA Premium 4 mobile app
22nd June 2014, 06:16 PM   |  #2  
SimplicityApks's Avatar
Senior Member
Flag Aachen
Thanks Meter: 337
 
347 posts
Join Date:Joined: May 2013
More
Quote:
Originally Posted by arpitkh96

Suppose I added a digit '1' to the arraylist<Integer> which is at position 0.So when I call remove method like this.
array.remove(1);
It gives an exception.although the method remove(object) exists.so I how can I remove the object(integer) by not using the position

Sent from my GT-S5570 using XDA Premium 4 mobile app

array.remove(new Integer(1));
22nd June 2014, 08:08 PM   |  #3  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,127 posts
Join Date:Joined: Feb 2012
Donate to Me
More
Quote:
Originally Posted by SimplicityApks

array.remove(new Integer(1));

That didn't worked.I am using it like this
Code:
 
//CopyIds is the arraylist
private BroadcastReceiver Copy_Receiver = new BroadcastReceiver(){

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Bundle b=arg1.getExtras();
if(b!=null){
int id=b.getInt("id");
Integer id1=new Integer(id);
if(CopyIds.contains(id)){

boolean completed=b.getBoolean("COPY_COMPLETED",false);
View process=rootView.findViewWithTag("copy"+id);
  if(completed){ rootView.removeViewInLayout(process);CopyIds.remove(id1);}
else{
String name=b.getString("name");
int p1=b.getInt("p1");
int p2=b.getInt("p2");
long total=b.getLong("total");
long done=b.getLong("done");
  ((TextView)process.findViewById(R.id.progressText)).setText("Copying \n"+name+"\n"+utils.readableFileSize(done)+"/"+utils.readableFileSize(total)+"\n"+p1+"%");
ProgressBar p=(ProgressBar)process.findViewById(R.id.progressBar1);
p.setProgress(p1);
p.setSecondaryProgress(p2);}
}else{
View root=getActivity().getLayoutInflater().inflate(R.layout.processrow, null);
root.setPaddingRelative(10,10,10,10);
String name=b.getString("name");
int p1=b.getInt("p1");
int p2=b.getInt("p2");
root.setTag("copy"+id);
((TextView)root.findViewById(R.id.progressText)).setText("Copying \n"+name);
ProgressBar p=(ProgressBar)root.findViewById(R.id.progressBar1);
p.setProgress(p1);
p.setSecondaryProgress(p2);
CopyIds.add(id);
rootView.addView(root);
}
}
}};
Log says error receiving broadcast.arryindexoutofbounds exception ,size 1 index 1

Sent from my GT-S5570 using XDA Premium 4 mobile app
22nd June 2014, 10:19 PM   |  #4  
SimplicityApks's Avatar
Senior Member
Flag Aachen
Thanks Meter: 337
 
347 posts
Join Date:Joined: May 2013
More
Quote:
Originally Posted by arpitkh96

That didn't worked.I am using it like this

Code:
 
//CopyIds is the arraylist
private BroadcastReceiver Copy_Receiver = new BroadcastReceiver(){

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Bundle b=arg1.getExtras();
if(b!=null){
int id=b.getInt("id");
Integer id1=new Integer(id);
if(CopyIds.contains(id)){

boolean completed=b.getBoolean("COPY_COMPLETED",false);
View process=rootView.findViewWithTag("copy"+id);
  if(completed){ rootView.removeViewInLayout(process);CopyIds.remove(id1);}
else{
String name=b.getString("name");
int p1=b.getInt("p1");
int p2=b.getInt("p2");
long total=b.getLong("total");
long done=b.getLong("done");
  ((TextView)process.findViewById(R.id.progressText)).setText("Copying \n"+name+"\n"+utils.readableFileSize(done)+"/"+utils.readableFileSize(total)+"\n"+p1+"%");
ProgressBar p=(ProgressBar)process.findViewById(R.id.progressBar1);
p.setProgress(p1);
p.setSecondaryProgress(p2);}
}else{
View root=getActivity().getLayoutInflater().inflate(R.layout.processrow, null);
root.setPaddingRelative(10,10,10,10);
String name=b.getString("name");
int p1=b.getInt("p1");
int p2=b.getInt("p2");
root.setTag("copy"+id);
((TextView)root.findViewById(R.id.progressText)).setText("Copying \n"+name);
ProgressBar p=(ProgressBar)root.findViewById(R.id.progressBar1);
p.setProgress(p1);
p.setSecondaryProgress(p2);
CopyIds.add(id);
rootView.addView(root);
}
}
}};
Log says error receiving broadcast.arryindexoutofbounds exception ,size 1 index 1

Strange since I don't get any compile time errors (sorry don't have time to test it right now) and that should be the way to do it...

Anyway, if it still doesn't work for you, just manually search for the right index using get().equals in a for loop and remove the element at the right index then... (that's what the remove(Object) method does anyway).
23rd June 2014, 03:56 AM   |  #5  
Member
Thanks Meter: 3
 
36 posts
Join Date:Joined: Feb 2012
Quote:
Originally Posted by SimplicityApks

array.remove(new Integer(1));

That works. But why ArrayList is not generified. I would like to use it like:
Code:
arrayList.<Integer>remove(new Integer(1));
It would throw Compiler time Error in case if you pass object of wrong type, which would safe time people like OP.
23rd June 2014, 09:16 AM   |  #6  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,127 posts
Join Date:Joined: Feb 2012
Donate to Me
More
Quote:
Originally Posted by SimplicityApks

Strange since I don't get any compile time errors (sorry don't have time to test it right now) and that should be the way to do it...

Anyway, if it still doesn't work for you, just manually search for the right index using get().equals in a for loop and remove the element at the right index then... (that's what the remove(Object) method does anyway).

I solved it

Sent from my GT-S5570 using XDA Premium 4 mobile app
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes