JSON Android App,检索数据问题

时间:2016-05-18 11:18:13

标签: php android json

我的问题是,我有一个包含所有记录的列表视图,当只剩下1条记录时,我尝试删除它,记录在PHP文件中删除,但在列表视图中,它还在那里,有人可以检查我的代码吗?

这是我填充ListView

的代码
Table1

这是我用来检索数据并将其解析为json对象的php文件。

public class MainActivity extends AppCompatActivity {

    String id,name,course;
    JSONArray students = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new JSONParse().execute();
    }

    @Override
    protected void onResume() {
        super.onResume();
        new JSONParse().execute();
    }

    private class JSONParse extends AsyncTask<String,String,JSONObject>{
        private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Getting Data...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected JSONObject doInBackground(String... params) {
            JSONParser jParser = new JSONParser();
            JSONObject json = null;
            try {
                json = jParser.getJSONFromUrl("http://192.168.8.102/mymobile/getstudentlist.php");
            }
            catch(Exception e){
                e.printStackTrace();
            }
            return json;
        }
        @Override
        protected void onPostExecute(JSONObject json) {
            super.onPostExecute(json);
            pDialog.dismiss();
            final ArrayList<String> ids = new ArrayList<String>();
            final ArrayList<String> names = new ArrayList<String>();
            final ArrayList<String> courses = new ArrayList<String>();
            try{
                if(!(json.getJSONArray("students").isNull(0))) {
                    students = json.getJSONArray("students");
                    for (int i = 0; i < students.length(); i++) {
                        JSONObject student = students.getJSONObject(i);
                        ids.add(student.getString("id"));
                        names.add(student.getString("name"));
                        courses.add(student.getString("course"));
                    }
                    final ListView listView = (ListView) findViewById(R.id.list);
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>
                            (MainActivity.this, android.R.layout.simple_list_item_1,
                                    android.R.id.text1, names);

                    listView.setAdapter(adapter);
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            String stud_id = ids.get(position);
                            String stud_name = names.get(position);
                            String stud_course = courses.get(position);

                            Intent i = new Intent(MainActivity.this, ActivityEditDel.class);
                            i.putExtra("stud_id", stud_id);
                            i.putExtra("stud_name", stud_name);
                            i.putExtra("stud_course", stud_course);
                            i.putExtra("stud_course", stud_course);
                            startActivity(i);
                        }
                    });
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (NullPointerException e){
                e.printStackTrace();
            }
        }
    }

我添加了一个尝试并抓住我试图获取jsonarray的代码块,因为当jsonobject为空时,应用程序崩溃。 [编辑]

编辑和删除活动:

<?php

$database_name = 'dbstudrec';
$con = mysqli_connect('localhost', 'root', '',$database_name);
$query = "SELECT * FROM tblstudent";
$result = mysqli_query($con,$query);
$rec = array();
while($row = mysqli_fetch_assoc($result)){
    $rec['students'][] = $row;
}
echo json_encode($rec);
mysqli_close($con);
?>

用于删除的php文件:

public class ActivityEditDel extends Activity{
    TextView tvId;
    EditText etName, etCourse;
    String strId, strName, strCourse;
    boolean isDeleteAction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.updatendeleteview);
        tvId = (TextView) findViewById(R.id.tvId);
        etName = (EditText) findViewById(R.id.etName);
        etCourse = (EditText) findViewById(R.id.etCourse);

        Intent i = getIntent();

        strId = i.getStringExtra("stud_id");
        strName = i.getStringExtra("stud_name");
        strCourse = i.getStringExtra("stud_course");

        tvId.setText(strId);
        etName.setText(strName);
        etCourse.setText(strCourse);

        isDeleteAction = false;
    }
    public void doUpdate(View v){
        isDeleteAction=false;
        new JSONParse().execute();
    }
    public void doDelete(View v){
        isDeleteAction = true;
        new JSONParse().execute();
    }
    private class JSONParse  extends AsyncTask<String,String,JSONObject>{
        private ProgressDialog pDialog;
        private Dialog aDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ActivityEditDel.this);
            if(isDeleteAction){
                pDialog.setMessage("Deleting Record...");
            }
            else{
                pDialog.setMessage("Updating Record...");
            }
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected JSONObject doInBackground(String... params) {
            JSONParser jParser = new JSONParser();
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("id",strId));
            nameValuePairs.add(new BasicNameValuePair("name", etName.getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("course", etCourse.getText().toString()));
            if(isDeleteAction){
                jParser.runQuery("http://192.168.1.101/mymobile/deleterec.php", nameValuePairs);
            }
            else{
                jParser.runQuery("http://192.168.1.101/mymobile/updaterec.php", nameValuePairs);
            }
            return null;
        }

        @Override
        protected void onPostExecute(JSONObject jsonObject) {
            pDialog.dismiss();
            if (isDeleteAction){
                aDialog = new Dialog(ActivityEditDel.this);
                aDialog.setTitle("Message");
                TextView tv = new TextView(ActivityEditDel.this);
                tv.setText("Delete Successful!");
                tv.setPadding(30, 30, 30, 30);
                aDialog.setContentView(tv);
                aDialog.show();
                aDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        finish();
                    }
                });

            }
            else{
                aDialog = new Dialog(ActivityEditDel.this);
                aDialog.setTitle("Message");
                TextView tv = new TextView(ActivityEditDel.this);
                tv.setText("Update Successful!");
                tv.setPadding(30,30,30,30);
                aDialog.setContentView(tv);
                aDialog.show();
                aDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        finish();
                    }
                });

            }
        }
    }
    public void doBack(View v){
        finish();
    }
}

2 个答案:

答案 0 :(得分:1)

拨打适配器上的notifyDataSetChanged()或列表视图中的invalidateViews()

进一步阅读:How to refresh Android listview?

答案 1 :(得分:1)

尝试使用

final ListView listView = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,android.R.id.text1, names);
listView.setAdapter(adapter);
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();

adapter.notifyDataSetChanged();