2016-05-31 23 views
0

我希望條形碼掃描的結果能夠自動從MySQL獲取數據,而無需再次單擊按鈕。如何在不點擊按鈕的情況下從MySQL自動獲取數據到Android?

這是我點擊按鈕來獲取MySQL數據的代碼。如何在不再次點擊按鈕的情況下獲得相同的數據?

public class EditPerangkat extends AppCompatActivity implements View.OnClickListener { 

private EditText editTextNoIT; 
private Button buttonGet; 
private TextView textViewResult; 

private ProgressDialog loading; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.editperangkat); 

    Scan = (Button) findViewById(R.id.buttonScan); 
    buttonGet = (Button) findViewById(R.id.buttonGet); 
    textViewResult = (TextView) findViewById(R.id.textViewResult); 

    Scan.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      IntentIntegrator scanIntegrator = new IntentIntegrator(EditPerangkat.this); 
      scanIntegrator.initiateScan(); 
     } 
    }); 

    buttonGet.setOnClickListener(this); 
} 

private void getData() { 
    String it = editTextNoIT.getText().toString().trim(); 
    if (it.equals("")) { 
     Toast.makeText(this, "Please input data", Toast.LENGTH_LONG).show(); 
     return; 
    } 
    loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false); 

    String url = Config.URL_GET+editTextNoIT.getText().toString().trim(); 

    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      loading.dismiss(); 
      showJSON(response); 
     } 
    }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(EditPerangkat.this,error.getMessage().toString(),Toast.LENGTH_LONG).show(); 
       } 
      }); 

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

public void showJSON(String response){ 
    String jenisperangkat=""; 
    String namaperangkat=""; 
    String serialnumber= ""; 
    try { 
     JSONObject jsonObject = new JSONObject(response); 
     JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY); 
     JSONObject collegeData = result.getJSONObject(0); 
     jenisperangkat = collegeData.getString(Config.KEY_PER_JENISPERANGKAT); 
     namaperangkat = collegeData.getString(Config.KEY_PER_NAMAPERANGKAT); 
     serialnumber = collegeData.getString(Config.KEY_PER_SERIALNUMBER); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    textViewResult.setText(jenisperangkat + "\n" + namaperangkat + "\n" + serialnumber); 
} 

@Override 
public void onClick(View v) { 
    getData(); 
} 

回答

0

您的IntentIntegrator類應該提供一些回調/偵聽器事件。 掃描完成後,回調/事件將被觸發,並且您可以調用您的getdata()。

相關問題