Android:单击微调器项时刷新Listview项

时间:2015-12-26 13:07:10

标签: android listview

大家好我有这个问题,我希望我的应用在点击特定的微调项目时刷新或更新listview项目。这是截图

SS

而且我不知道我要做什么,到目前为止这是我的代码:(

public class ViewGrade extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private ListView mylistView;

    private String subj_code = "";
    private String subj_code_lab = "";

    private Spinner spinTerm;

    private List<GradeList> gradeList = new ArrayList<GradeList>();
    private GradeAdapter arrayAdapter;
    private ProgressDialog pDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_grade);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        spinTerm = (Spinner) findViewById(R.id.spinTerm);

        String[] Term = new String[]{"Prelim", "Midterm", "Tentative Final"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Term);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinTerm.setAdapter(adapter);

        SharedPreferences preferences = getSharedPreferences("MyApp", MODE_PRIVATE);
        subj_code = preferences.getString("code", "UNKNOWN");
        subj_code_lab = preferences.getString("code_lab", "UNKNOWN");

        mylistView = (ListView) findViewById(R.id.list);
        arrayAdapter = new GradeAdapter(this, gradeList);
        mylistView.setAdapter(arrayAdapter);
        new loadGrades().execute();


    }

    public class loadGrades extends AsyncTask<Void, Void, Void>{
        String term = spinTerm.getSelectedItem().toString();
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ViewGrade.this);
            // Showing progress dialog before making http request
            pDialog.setMessage("Loading...");
            pDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            String url = null;
            try {
                url = "http://192.168.22.3/MobileClassRecord/getGrade.php?subj_code="+ URLEncoder.encode(subj_code, "UTF-8")+"&term="+URLEncoder.encode(term, "UTF-8");
            }catch (UnsupportedEncodingException e){
                e.printStackTrace();
            }

            ServiceHandler jsonParser = new ServiceHandler();
            String json = jsonParser.makeServiceCall(url, ServiceHandler.GET);
            Log.e("Response; ", "> " + json);

            if (json != null){
                try {
                    JSONObject object = new JSONObject(json);
                    if (object != null){
                        JSONArray grade_array = object.getJSONArray("grade");
                        for (int i = 0; i < grade_array.length(); i++){
                            JSONObject grade = (JSONObject) grade_array.get(i);
                            GradeList grade_list = new GradeList();
                            grade_list.setName(grade.getString("stud_name"));
                            grade_list.setGrade(grade.getString("grade"));
                            grade_list.setRemark(grade.getString("remark"));

                            gradeList.add(grade_list);
                        }
                    }
                    arrayAdapter.notifyDataSetChanged();

                }catch (JSONException e){
                    e.printStackTrace();
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            pDialog.dismiss();
        }
    }

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

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_view_grade, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                Intent intent = new Intent(ViewGrade.this, SpecificClassRecord.class);
                startActivity(intent);
                return true;
            case R.id.action_add_grade:
                Intent intent1 = new Intent(ViewGrade.this, Grade.class);
                startActivity(intent1);
        }

        return super.onOptionsItemSelected(item);
    }
}

3 个答案:

答案 0 :(得分:2)

adapter.notifydatasetChanged()或者如果您知道插入/删除了特定项目,只需使用adapter.notifyitemInserted / Removed等。

在onClickListener中为spinner添加此代码

答案 1 :(得分:1)

覆盖微调器的onItemClick()侦听器方法,然后在方法内部调用listview的adapter.notifydatasetChanged()

答案 2 :(得分:0)

spinTerm.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        // change the adapter values if your list is being changedin anyway
        arrayAdapter.notifydatasetChanged();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }

});