Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Refresh Android List Adapter, so it shows new-added items

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; 
 }
like image 845
Igx33 Avatar asked Jul 27 '12 11:07

Igx33


2 Answers

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!!!
like image 84
AntuanSoft Avatar answered Oct 21 '22 15:10

AntuanSoft


You must use every time when data changed.

aa.notifyDataSetChanged();
like image 42
Andrew Avatar answered Oct 21 '22 15:10

Andrew