Sagar Pawar
Sagar Pawar

Reputation: 475

update listview using spinner data in android

Am trying to update my listview on every selection of the spinner. but its not working. Instead of getting new data, listview is repeating the same values.

I am unable to find out what is my mistake.

here is my avtivity code:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        setContentView(R.layout.activity_performance_details);

        PerfList = new ArrayList<PerformanceListItem>();
        months = (Spinner) findViewById(R.id.load_month);
        listview_performance = (ListView)     findViewById(R.id.performance_details_list);
        sadapter = new PerformanceAdapter(PerformanceDetails.this, PerfList);


        months.setOnItemSelectedListener(this);

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

         Spinner a=(Spinner)parent;
        if(a.getId() == R.id.load_month) {
            monthid =1+(int)months.getSelectedItemPosition();
       Toast.makeText(getApplicationContext(),""+monthid,Toast.LENGTH_LONG).show();
            new setAsyncTask_performance().execute();

        }
    }

after selecting spinner data it is sent to server and from server its relevant data is fetched and sent back to the list view. now when i first time select the spinner it show the data accordingly. But on second selection it will include the previous data without updating the listview

Adapter Code:

    public class PerformanceAdapter extends BaseAdapter {

    private Activity activity;
    private LayoutInflater inflater;
    private Context context;
    private List<PerformanceListItem> performanceList;

    public PerformanceAdapter(Activity activity, List<PerformanceListItem> PList) {

        this.activity = activity;
        this.performanceList = PList;

    }




    @Override
    public int getCount() {
        return performanceList.size();
    }

    @Override
    public Object getItem(int position) {
        return performanceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null) {
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.performance_itemlist, null);
        }
        Animation slideUp = AnimationUtils.loadAnimation(activity, R.anim.slide_up);

        TextView staffName = (TextView) convertView.findViewById(R.id.perf_staffName);
        TextView staffDesignation = (TextView) convertView.findViewById(R.id.perf_Design);
        TextView staffPerformance = (TextView) convertView.findViewById(R.id.perf_performance);


        PerformanceListItem plist = performanceList.get(position);

        staffName.setText(plist.getpStaffName());
        staffDesignation.setText(plist.getpDesignation());
        staffPerformance.setText(plist.getpPerformance());

        slideUp.setDuration(500);
        convertView.startAnimation(slideUp);
        slideUp = null;
        return convertView;
    }
}

and this is my performance list to get and set data

PerformanceListItems code:

 public class PerformanceListItem {
    private String pSid;
    private String pStaffName;
    private String pDesignation;
    private String pPerformance;
    private String pList;

    public PerformanceListItem(){

    }

    public PerformanceListItem(String pList){
        this.pList = pList;

    }

    public String getpSid(){
        return pSid;
    }
    public void setpSid(String pSid){
        this.pSid = pSid;
    }
    public String getpStaffName(){
        return pStaffName;

    }

    public void setpStaffName(String pStaffName){
        this.pStaffName = pStaffName;
    }

    public String getpDesignation(){
        return pDesignation;
    }

    public void setpDesignation(String pDesignation){
        this.pDesignation = pDesignation;
    }

    public String getpPerformance(){
        return pPerformance;
    }

    public void setpPerformance(String pPerformance){
        this.pPerformance = pPerformance;
    }
}

After debugging the entire code i found that my JSONObject is not updating with new value

any help would be appreciable.

Upvotes: 0

Views: 134

Answers (2)

kiturk3
kiturk3

Reputation: 558

dont set adapter in oncreate. Set your adapter in Asynctask Post execute. and set your array inside doinbackground along with getting data task.

Upvotes: 0

mariozawa
mariozawa

Reputation: 1524

Update the data of your adapter when you execute this new setAsyncTask_performance().execute();

If you want to show only the new data just remove all your listview items then update the data and set the adapter again.

Upvotes: 1

Related Questions