我的删除方法不起作用(Android)

时间:2015-10-06 09:39:47

标签: php android json

我使用php创建了一个数据库并连接到android,现在我删除表中一行的方法无效。我在这里使用JSON填充我的列表。这是我的代码,如果有人可以帮助我:

包含所有项目列表的活动:

var str= "likeThis"

删除方法的活动:

public class QuestsFragment extends Fragment {

// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> questsList;

// url to get all products list
private static String url_all_quests = "http://192.168.0.17:8081/geomondo/get_all_quests.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_QUESTS = "quests";
private static final String TAG_QID = "qid";
private static final String TAG_TITLE = "title";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_DISTRICT = "district";
private static final String TAG_CODE = "code";

private ListView listView;

// quests JSONArray
JSONArray quests = null;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.quests, container, false);

    // Hashmap for ListView
    questsList = new ArrayList<HashMap<String, String>>();
    listView = (ListView) rootView.findViewById(R.id.list);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // getting values from selected ListItem
            String send_qid = ((TextView) view.findViewById(R.id.qid)).getText()
                    .toString();
            String send_title = ((TextView) view.findViewById(R.id.title))
                    .getText().toString();
            String send_description = ((TextView) view.findViewById(R.id.desc))
                    .getText().toString();
            String send_district = ((TextView) view.findViewById(R.id.district))
                    .getText().toString();
            String send_code = ((TextView) view.findViewById(R.id.code))
                    .getText().toString();
            Intent send_values = new Intent(getActivity(), QuestsSingleActivity.class);
            send_values.putExtra(TAG_QID, send_qid);
            send_values.putExtra(TAG_TITLE, send_title);
            send_values.putExtra(TAG_DESCRIPTION, send_description);
            send_values.putExtra(TAG_DISTRICT, send_district);
            send_values.putExtra(TAG_CODE, send_code);
            startActivity(send_values);
            getActivity().overridePendingTransition(R.anim.slide_in, R.anim.slide_in_out);

            // starting new activity and expecting some response back
            startActivityForResult(send_values, 100);
        }
    });

    // Loading quests in Background Thread
    new LoadAllQuests().execute();

    return rootView;
}

// Response from Edit Quest Activity
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // if result code 100
    if (resultCode == 100) {
        // if result code 100 is received
        // means user edited/deleted product
        // reload this screen again
        Intent intent = getActivity().getIntent();
        getActivity().startActivity(intent);
    }

}
/**
 * Background Async Task to Load all quest by making HTTP Request
 * */
class LoadAllQuests extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading quests. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All quests from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_quests, "GET", params);

        // Check your log cat for JSON reponse
        Log.d("All Quests: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Quests
                quests = json.getJSONArray(TAG_QUESTS);

                // looping through All Quests
                for (int i = 0; i < quests.length(); i++) {
                    JSONObject c = null;
                    try {
                        c = quests.getJSONObject(i);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    // Storing each json item in variable
                    String id = c.getString(TAG_QID);
                    String title = c.getString(TAG_TITLE);
                    String description = c.getString(TAG_DESCRIPTION);
                    String district = c.getString(TAG_DISTRICT);
                    String code = c.getString(TAG_CODE);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_QID, id);
                    map.put(TAG_TITLE, title);
                    map.put(TAG_DESCRIPTION, description);
                    map.put(TAG_DISTRICT, district);
                    map.put(TAG_CODE, code);

                    // adding HashList to ArrayList
                    questsList.add(map);
                }
            } else {
                // no products found
                // Launch Quest Single Activity
                Intent i = new Intent(getActivity(),
                        QuestsSingleActivity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // here you check the value of getActivity() and break up if needed
        if(getActivity() == null)
            return;
        // updating UI from Background Thread
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        getActivity(), questsList,
                        R.layout.quests_row, new String[]{TAG_QID,
                        TAG_TITLE, TAG_DESCRIPTION, TAG_DISTRICT, TAG_CODE},
                        new int[]{R.id.qid, R.id.title, R.id.desc, R.id.district, R.id.code});
                // updating listview
                listView.setAdapter(adapter);
            }
        });

    }

}
}

如果我需要为php文件添加更多代码,请告诉我。我会在这里给你一些帮助。

0 个答案:

没有答案