使用android中的微调数据更新listview

时间:2015-12-12 05:28:47

标签: android listview android-listview android-spinner

我试图在listview的每个选项上更新spinner。但它不起作用。 <{1}}不是获取新数据,而是重复相同的值。

我无法找出我的错误。

这是我的活动代码:

listview

选择 @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(); } } 数据后,将其发送到服务器,并从服务器获取其相关数据并将其发送回列表视图。现在,当我第一次选择微调器时,它会相应地显示数据。但在第二次选择时,它将包含先前的数据而不更新spinner

适配器代码:

listview

这是我获取和设置数据的性​​能列表

PerformanceListItems代码:

    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;
    }
}

调试整个代码后,我发现我的JSONObject没有使用新值进行更新

任何帮助都会很明显。

2 个答案:

答案 0 :(得分:1)

执行此操作时更新适配器的数据 new setAsyncTask_performance()。execute();

如果您只想显示新数据,只需删除所有列表视图项,然后更新数据并再次设置适配器。

答案 1 :(得分:0)

不要在oncreate中设置适配器。在Asynctask Post执行中设置适配器。并在doinbackground中设置你的数组以及获取数据任务。