更新列表项后如何刷新列表视图项 - 在onResume中使用

时间:2016-04-03 17:58:10

标签: android listview android-fragments

我有一个更新片段,我将用它更新数据库中的数据。更新完成后,我将返回“查看数据库中的数据”片段,我需要更新列表。我想我必须在onResume()内部编写代码,但我不知道该怎么做。 “查看数据库中的数据”片段的代码:

public class Testing extends Fragment {
String uname="921312104053";
private static final String TAG_RESULTS="result";
private static final String TAG_Sname = "Subject_name";
private static final String TAG_Scode = "Subject_code";
private static final String TAG_Sgrade ="Grade";
ListView lv;
JSONArray subjects = null;
String myJSON;
ListAdapter adapter;
ArrayList<HashMap<String, String>> subList,subList1;
public Testing() {

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_testing,
            container, false);

    disp(view);



    return view;
}
public void disp(View view)
{
    lv = (ListView)view.findViewById(R.id.list);
    subList = new ArrayList<HashMap<String,String>>();
    getData(view);

}
public void getData(View view){
    class GetDataJSON extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://10.0.2.2/markolepsy/android_connect/testing.php?usrname="+uname);


            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();

                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (Exception e) {

            }
            finally {
                try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result){
            myJSON=result;
            showList();
        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();
}
protected void showList(){
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        subjects = jsonObj.getJSONArray(TAG_RESULTS);

        for(int i=0;i<subjects.length();i++){
            JSONObject c = subjects.getJSONObject(i);
            String sname = c.getString(TAG_Sname);
            String scode = c.getString(TAG_Scode);
            String sgrade = c.getString(TAG_Sgrade);

            HashMap<String,String> records = new HashMap<String,String>();

            records.put(TAG_Sname,sname);
            records.put(TAG_Scode,scode);
            records.put(TAG_Sgrade,sgrade);

            subList.add(records);
        }

        adapter = new SimpleAdapter(
                getActivity(), subList, R.layout.list_layout,
                new String[]{TAG_Sname,TAG_Scode,TAG_Sgrade},
                new int[]{R.id.subject_name, R.id.subject_code, R.id.grade}
        );

        lv.setAdapter(adapter);

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

}
}

1 个答案:

答案 0 :(得分:0)

使用notifyDataSetChanged()可以满足您的需求。

请参阅:How to refresh Android listview?

编辑:

       adapter = new SimpleAdapter(
            getActivity(), subList, R.layout.list_layout,
            new String[]{TAG_Sname,TAG_Scode,TAG_Sgrade},
            new int[]{R.id.subject_name, R.id.subject_code, R.id.grade}
    );

    lv.setAdapter(adapter);
adapter.notifyDataSetChanged();

以上代码应该有所帮助。