Reputation: 171
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).
I have 2 tasks in my List already, and they are shown properly.
The problem is, when I add a new task it won't show them in view. I have tried many possible solutions:
simply adding item to list
creating a new list that consists items from old one and rebuilding adapter;
using notifyDataSetChanged();
alongside with add() command;
etc.
Here is my code, it's a bit messy, but I hope you will figure it out.
AndroidListAdapterActivity class:
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<String>(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>=2){
aa=new ArrayAdapter<String>(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();
}
}
Dodavanje (Adding):
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();
}
});
}
}
Lista (list):
public class Lista extends Application {
ArrayList<String> lis=new ArrayList<String>();
ArrayList<String> lis2=new ArrayList<String>();
int broj =1;
boolean ses= false;
}
Upvotes: 11
Views: 38396
Reputation: 353
The solutions proposed thus far work, but are dependent on the Android version of your device. For example, to use the AddAll method you have to put android:minSdkVersion="10" in your android device.
To solve this questions for all devices I have created my on own method in my adapter and use inside the add and remove method inherits from ArrayAdapter that update you data without problems.
My Code: Using my own data class RaceResult, you use your own data model.
ResultGpRowAdapter.java
public class ResultGpRowAdapter extends ArrayAdapter<RaceResult> {
Context context;
int resource;
List<RaceResult> data=null;
public ResultGpRowAdapter(Context context, int resource, List<RaceResult> objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
this.data = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
........
}
//my own method to populate data
public void myAddAll(List<RaceResult> items) {
for (RaceResult item:items){
super.add(item);
}
}
ResultsGp.java
public class ResultsGp extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...........
...........
ListView list = (ListView)findViewById(R.id.resultsGpList);
ResultGpRowAdapter adapter = new ResultGpRowAdapter(this, R.layout.activity_result_gp_row, new ArrayList<RaceResult>()); //Empty data
list.setAdapter(adapter);
....
....
....
//LOAD a ArrayList<RaceResult> with data
ArrayList<RaceResult> data = new ArrayList<RaceResult>();
data.add(new RaceResult(....));
data.add(new RaceResult(....));
.......
adapter.myAddAll(data); //Your list will be udpdated!!!
Upvotes: 3
Reputation: 10482
extends BaseAdapter
class ,Override notifyDataSetChanged();
method.
Please see below snip code:
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
list.addAll();//--insert object to list
}
call notifyDataSetChanged();
method in Activity onResume()
method ,like this:
@Override
protected void onResume() {
super.onResume();
((BaseAdapter)getListAdapter()).notifyDataSetChanged();
}
Upvotes: 2
Reputation: 2534
You can use notifyDataSetChanged() method to update your listview.In your case you can use.
aa.notifyDataSetChanged();
Please Try this way....
public class MainActivity extends Activity {
ListView lView;
Button btnAdd;
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainListView = (ListView) findViewById( R.id.listView1 );
btnAdd = (Button)findViewById(R.id.button1);
// Create and populate a List of alphabet names.
String[] alphabets = new String[] { "A", "B", "C", "D",
"E", "F", "G", "H"};
ArrayList<String> alphabetsList= new ArrayList<String>();
alphabetsList.addAll( Arrays.asList(alphabets) );
// Create ArrayAdapter using the for the list.
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, alphabetsList);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
btnAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// Here I am adding more alphabets in the list on the listener of add button.
listAdapter.add( "I" );
listAdapter.add( "J" );
listAdapter.add( "K" );
listAdapter.add( "L" );
listAdapter.add( "M" );
}
});
}
}
Upvotes: 4
Reputation: 266
You must use every time when data changed.
aa.notifyDataSetChanged();
Upvotes: 8