Add item to android array list

I want to add a value derived from the android text view to an existing list of arrays. my current list of arrays contains Cricket, Football and text view values. I want to add hockey to the list of arrays in the last position. Then my list of arrays will become cricket, football, hockey. My list of cricket and football arrays comes from previous activity. But now he adds only cricket and football, but does not add hockey. How can I do this?

resultArrGame+=resultArrGame.add(txtGame.getText().toString()); 
+6
source share
4 answers

This will definitely work for you ...

 ArrayList<String> list = new ArrayList<String>(); list.add(textview.getText().toString()); list.add("B"); list.add("C"); 
+19
source

You are trying to assign the result of the add operation resultArrGame, and add can either return true or false, depending on whether the operation was successful or not. What you want is probably simple:

 resultArrGame.add(txt.Game.getText().toString()); 
+2
source

you can use this add line to display on the button

 final String a[]={"hello","world"}; final ArrayAdapter<String> at=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,a); final ListView sp=(ListView)findViewById(R.id.listView1); sp.setAdapter(at); final EditText et=(EditText)findViewById(R.id.editText1); Button b=(Button)findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int k=sp.getCount(); String a1[]=new String[k+1]; for(int i=0;i<k;i++) a1[i]=sp.getItemAtPosition(i).toString(); a1[k]=et.getText().toString(); ArrayAdapter<String> ats=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,a1); sp.setAdapter(ats); } }); 

So, when you click the button, it will get the line from edittext and save it in the list. You can change it to your needs.

+1
source
 item=sp.getItemAtPosition(i).toString(); list.add(item); adapter.notifyDataSetChanged () ; 

look for ArrayAdapter.notifyDataSetChanged ()

+1
source

All Articles