Lenovo Officially Owns Motorola, Following ~$3 Billion Deal

Google is now officially no longer the owner American phone manufacturer … more

Microsoft Band Fitness Tracker Announced, Available

The wearable market has been around for a few years, with Pebble and Samsung smartwatches … more

Forums Added for the Oppo R5, Oppo N3, and Xiaomi Redmi Note

Just yesterday, Oppo unveiled a pair of rather unique smartphones, the Oppo … more

How to Play Music From Android Wear – XDA TV

Yesterday, we showed you how to do some advanced things with your Moto 360 quite easily … more
Post Reply

ArrayList<Integer> 's remove method confusing?

OP arpitkh96

22nd June 2014, 03:53 PM   |  #1  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 384
 
1,098 posts
Join Date:Joined: Feb 2012
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, 05:16 PM   |  #2  
SimplicityApks's Avatar
Senior Member
Flag Aachen
Thanks Meter: 335
 
339 posts
Join Date:Joined: May 2013
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, 07:08 PM   |  #3  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 384
 
1,098 posts
Join Date:Joined: Feb 2012
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, 09:19 PM   |  #4  
SimplicityApks's Avatar
Senior Member
Flag Aachen
Thanks Meter: 335
 
339 posts
Join Date:Joined: May 2013
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, 02:56 AM   |  #5  
Junior Member
Thanks Meter: 2
 
26 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, 08:16 AM   |  #6  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 384
 
1,098 posts
Join Date:Joined: Feb 2012
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


Top Threads in Java for Android App Development by ThreadRank