Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Refresh Android List Adapter, so it shows new-added items
    primarykey
    data
    text
    <p>I was working on a project. It's just showing a list of tasks and Adding new tasks to it. I have 3 CLASSES. One for adding, One for view and One to hold all information (or so I think).</p> <p>I have 2 tasks in my List already, and they are shown properly. </p> <p>The problem is, when I <strong>add</strong> a new task it won't show them in view. I have tried many possible solutions:</p> <ul> <li><p>simply adding item to list</p></li> <li><p>creating a new list that consists items from old one and rebuilding adapter;</p></li> <li><p>using <code>notifyDataSetChanged();</code> alongside with add() command;</p></li> <li><p>etc.</p></li> </ul> <p>Here is my code, it's a bit messy, but I hope you will figure it out.</p> <p><strong>AndroidListAdapterActivity class:</strong></p> <pre><code>public class AndroidListAdapterActivity extends ListActivity { /** Called when the activity is first created. */ Button b1; Lista o; ArrayAdapter aa; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); b1=(Button)findViewById(R.id.add); Log.w("POC", "PA OVO SE ZOVE SVAKI PUT"); o=new Lista(); o.lis.add("S1"); o.lis.add("S2"); aa = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, o.lis); setListAdapter(aa); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(AndroidListAdapterActivity.this, Dodavanje.class); startActivity(i); } }); } @Override public void onResume(){ super.onResume(); aa.notifyDataSetChanged(); if(o.broj&gt;=2){ aa=new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_spinner_item, o.lis2); setListAdapter(aa); Log.w("myApp", "CALLED TOO"); } String yt=String.valueOf(o.ses); Log.w("teras", yt); aa.notifyDataSetChanged(); Log.w("myApp", "CaLLED!!!!!!!!!!!!!"); String fx= String.valueOf(o.broj); Log.w("myAPPe", fx); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); } } </code></pre> <p><strong>Dodavanje (Adding):</strong></p> <pre><code>public class Dodavanje extends Activity { Button but; Button but2; EditText et; Lista o; AndroidListAdapterActivity www; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.dodavanje); but= (Button)findViewById(R.id.bb); but2= (Button)findViewById(R.id.bc); et=(EditText)findViewById(R.id.tt); www=new AndroidListAdapterActivity(); o = new Lista(); but.setOnClickListener(new View.OnClickListener() { @SuppressWarnings("unchecked") @Override public void onClick(View arg0) { String t1= et.getText().toString(); //o.lis.add(t1); o.lis2.addAll(o.lis); o.lis2.add(t1); o.lis.add(t1); o.ses=true; Log.w("IZVJESTAJ: ", String.valueOf(o.ses)); o.broj++; String fx=String.valueOf(o.broj); Log.w("Izbacaj",fx); et.setText(""); } }); but2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { finish(); } }); } } </code></pre> <p><strong>Lista (list):</strong></p> <pre><code>public class Lista extends Application { ArrayList&lt;String&gt; lis=new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt; lis2=new ArrayList&lt;String&gt;(); int broj =1; boolean ses= false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload