Php JSON Android应用程序,数据检索问题

Php JSON Android应用程序,数据检索问题,php,android,json,Php,Android,Json,我的问题是,我有一个包含所有记录的列表视图,当只剩下一条记录时,我尝试删除它,该记录在PHP文件中被删除,但在列表视图中,它仍然存在,有人可以检查我的代码吗 下面是填充ListView的代码 public class MainActivity extends AppCompatActivity { String id,name,course; JSONArray students = null; @Override protected void onCreate

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

下面是填充ListView的代码

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();
            }
        }
    }
public类MainActivity扩展了AppCompatActivity{
字符串id、名称、课程;
JSONArray students=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
新建JSONParse().execute();
}
@凌驾
受保护的void onResume(){
super.onResume();
新建JSONParse().execute();
}
私有类JSONParse扩展了异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(MainActivity.this);
setMessage(“获取数据…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的JSONObject doInBackground(字符串…参数){
JSONParser jParser=新的JSONParser();
JSONObject json=null;
试一试{
json=jParser.getJSONFromUrl(“http://192.168.8.102/mymobile/getstudentlist.php");
}
捕获(例外e){
e、 printStackTrace();
}
返回json;
}
@凌驾
受保护的void onPostExecute(JSONObject json){
onPostExecute(json);
pDialog.disclose();
最终ArrayList ID=新ArrayList();
最终ArrayList名称=新ArrayList();
最终ArrayList课程=新ArrayList();
试一试{
if(!(json.getJSONArray(“学生”).isNull(0))){
students=json.getJSONArray(“学生”);
for(int i=0;i
这是我用来检索数据并将其解析为json对象的php文件

<?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);
?>

我在试图获取jsonarray的代码块上添加了一个try-and-catch,因为当jsonobject为空时,应用程序崩溃。 [编辑]

编辑和删除活动:

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();
    }
}
公共类活动EditDel扩展活动{
文本视图tvId;
编辑文本etName,etCourse;
字符串strId、strName、strCourse;
布尔逻辑;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.UpdateDeleteView);
tvId=(TextView)findViewById(R.id.tvId);
etName=(EditText)findViewById(R.id.etName);
etCourse=(EditText)findViewById(R.id.etCourse);
Intent i=getIntent();
strId=i.getStringExtra(“螺柱id”);
strName=i.getStringExtra(“螺柱名称”);
strCourse=i.getStringExtra(“stud_课程”);
tvId.setText(strId);
etName.setText(strName);
etCourse.setText(strCourse);
isDeleteAction=false;
}
公共无效数据更新(视图v){
isDeleteAction=false;
新建JSONParse().execute();
}
公共空间(视图五){
isDeleteAction=true;
新建JSONParse().execute();
}
私有类JSONParse扩展了异步任务{
私人对话;
私人对话日志;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(ActivityEditDel.this);
如果(isDeleteAction){
setMessage(“删除记录…”);
}
否则{
setMessage(“更新记录…”);
}
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的JSONObject doInBackground(字符串…参数){
JSONParser jParser=新的JSONParser();
ArrayList nameValuePairs=新的ArrayList();
nameValuePairs.add(新的基本名称
<?php
$database_name = 'dbstudrec';
$con = mysqli_connect('localhost', 'root', '',$database_name);
$id = $_POST['id'];
$query = "DELETE FROM tblstudent WHERE id = $id";
mysqli_query($con,$query);
mysqli_close($con);
?>
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();