簡體   English   中英

獲取微調器在數據庫上選擇的項目並刪除

[英]Get spinner selected item on database and delete

這是我的微調程序代碼,該代碼從聯機數據庫中獲取data 代碼很好。 我找不到任何從在線數據庫獲取數據並delete 微調器 有一些但對我沒有幫助,因為它僅顯示1個data並且很難理解。 有人可以幫助我如何創建delete語句嗎?

PS *我正在使用活動而不是對話框

PS **我是Java的新手,很難理解

該行從數據庫獲取數據,並將獲取數據設置為字符串。

 private void getData() {
    StringRequest stringRequest = new StringRequest(Config.DATA_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            JSONObject j = null;
            try {
                j = new JSONObject(response);
                result = j.getJSONArray(Config.JSON_ARRAY);
                getDate(result);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void getDate(JSONArray j) {
    for (int i = 0; i < j.length(); i++) {
        try {
            JSONObject json = j.getJSONObject(i);
            albay.add(json.getString(Config.TAG_DATE));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    spinner.setAdapter(new ArrayAdapter<String>(UpdateData.this, android.R.layout.simple_spinner_dropdown_item, albay));
}

private String getlocation1(int position) {
    String location1 = "";
    try {
        //Getting object of given index
        JSONObject json = result.getJSONObject(position);
        //Fetching name from that object
        location1 = json.getString(Config.TAG_LOCATION1);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return location1;
}

private String getlocation2(int position) {
    String location2 = "";
    try {
        JSONObject json = result.getJSONObject(position);
        location2 = json.getString(Config.TAG_LOCATION2);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return location2;
}

這是獲取數據到textview的地方

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    //Setting the values to textviews for a selected item
    textViewLocation1.setText(getlocation1(position));
    textViewLocation2.setText(getlocation2(position));

這是我在其他java類中的刪除代碼。 我不知道如何從onclicklistenter調用它。 此代碼有效嗎?

 public boolean delete(int position) {
    try {
        albay.remove(position);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

只需在按鈕單擊列表器上

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            albay.remove(spinner1.getSelectedItemPosition());
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM