0

I would like to use a non-final variable in an On-click listener and converting the variable to final is not an option because the variable is an ArrayList<String> which i will be altering and editing throughout the coarse of the program.

This is an overview of the code

I have an EditText which i will be getting the value of to add to the ArrayList.

I am then setting a ListView based on this ArrayList using a custom adapter which all works fine.

ArrayList<String> nameList = new ArrayList<String>();

		Button newName = (Button) findViewById(R.id.AddItemButton);
		newName.setOnClickListener(new View.OnClickListener(){
			
			@Override
			public void onClick(View v){
				
				if(name.getText().toString().equals("")){
					
					Toast error2 = Toast.makeText(getBaseContext(), "The Product Name cannot be empty", Toast.LENGTH_LONG); 
					error2.setGravity(Gravity.CENTER, error2.getXOffset() / 2, error2.getYOffset() / 2);
					error2.show();
					
				}
				else{
	                                // The variable i need to access which cannot be non-final vvv
					nameList.add(name.getText().toString());
					
					setListAdapter(adapter);
					
				};
				
			}

I realise i could make a junk variable to make a copy of the arraylist and make that final but i dont believe this variable will update itself when my ArrayList chances.

I have found this solution which interests me however i get error on the Super(); line saying that a constructor must be the first statement.

newName.setOnClickListener(new OnClickListener() {
        private ArrayList<String> nameList;
        public void OnClickListener(ArrayList<String> nameList){
            super(); // error line
            this.nameList = nameList;
        }
        // do rest of code

Any thoughts?

Edited by Archenemie: n/a

1
Contributor
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Archenemie
0

Ive knocked up this which as far as i can see is right apart from the same error in the super(); line.

Can anybody tell me whats wrong?

newName.setOnClickListener(new OnClickListener(){
			
			ArrayList<String> refList;
			ArrayList<String> nameList;
			ArrayList<String> locList;
			ArrayList<String> qtyList;
			
			
			public void OnClickListener(ArrayList<String> nameList, ArrayList<String> locList, ArrayList<String> qtyList, ArrayList<String> refList){
	           
				super();
				this.refList = refList;
	            this.nameList = nameList;
	            this.locList = locList;
	            this.qtyList = qtyList;
	        }

@Override
			public void onClick(View v){
                            // do stuff
}
}
0

anybody have any advice? im new to Java so im not too familiar with super classes. In python i can access variables that can change :(.

0

Ignore that stupid question, anybody who has studied the scopes of variables in java for more than 5 minutes can asnwer that, and i am now one of them.

Instead of decraring the variables as final initialize them before the onCreate Method.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.