2015-08-20 42 views
0

如何通過btnup提交微調selecteditem文本,爲什麼選擇報告btnup的情況下聲明沒有使用?我有足夠的搜索來解決這個問題,但我仍然blankon我需要做的順序在微調器上獲取文本以適應編碼。如何發送空字符串使用微調在Android

UserLocalStore userLocalStore; 
EditText etName, etAge, etUsername, uploadImageName; 
Button bLogout; 
ImageView viewImage; 
Button b,btnup; 
private String selectedReport = null; 



} private void createSpinnerDropDown() { 

    //get reference to the spinner from the XML layout 
    Spinner spinner = (Spinner) findViewById(R.id.spinner); 

    //Array list of report to display in the spinner 
    List<String> list = new ArrayList<String>(); 
    list.add("Crime"); 
    list.add("Bribery"); 
    list.add("Schools problem"); 
    list.add("Homeless"); 
    list.add("Rural Problems"); 
    list.add("Public Transport"); 

    //create an ArrayAdaptar from the String Array 
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, list); 

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    spinner.setAdapter(dataAdapter); 

    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); 

} 

public class MyOnItemSelectedListener implements OnItemSelectedListener { 

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 

     String selectedItem = parent.getItemAtPosition(pos).toString(); 


     switch (parent.getId()) { 

      case R.id.spinner: 

       if (selectedReport != null) { 
        Toast.makeText(parent.getContext(), "Report you select is " + selectedItem, 
          Toast.LENGTH_LONG).show(); 
       } 
       selectedReport = selectedItem; 
       break; 
     } 


    } 

    public void onNothingSelected(AdapterView<?> parent) { 
     // Do nothing. 
    } 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.bLogout: 
      userLocalStore.clearUserData(); 
      userLocalStore.setUserLoggedIn(false); 
      Intent loginIntent = new Intent(this, Login.class); 
      startActivity(loginIntent); 
      break; 
     case R.id.btnup: 
      Bitmap image = ((BitmapDrawable) viewImage.getDrawable()).getBitmap(); 
      new UploadImage(image, uploadImageName.getText().toString()).execute(); 
      Spinner spinner = (Spinner)findViewById(R.id.spinner); 
      String selectedReport = spinner.getSelectedItem().toString(); 
      break; 
    } 
} 

回答

0

聲明的onCreate之前,你的意見,你的類:

UserLocalStore userLocalStore; 
EditText etName, etAge, etUsername, uploadImageName; 
ImageView viewImage; 
Button b, bLogout, btnup; 
Spinner spinner; 
String selectedReport; 

在你的onCreate,添加以下內容:

String[] list = {"Crime", "Bribery", "Schools Problem", "Homeless", "Rural Problems", "Public Transport"} 
spinner = (Spinner) findViewById(R.id.spinner); 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); 
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
spinner.setAdapter(adapter); 
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 
     Toast.makeText(this, "Report selected: " + list[position], Toast.LENGTH_LONG).show(); 
     //You can remove this switch statement, if you don't need it. 
     switch (position) { 
      case 0: 
       //Do something 
       break; 
      case 1: 
       //Do something 
       break; 
      case 2: 
       //Do something 
       break; 
      case 3: 
       //Do something 
       break; 
      case 4: 
       //Do something 
       break; 
      case 5: 
       //Do something 
       break; 
     } 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parentView) { 

    } 
}); 

我這裏假設你已經設置onClickListener到onCreate中的按鈕。

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.bLogout: 
      userLocalStore.clearUserData(); 
      userLocalStore.setUserLoggedIn(false); 
      Intent loginIntent = new Intent(this, Login.class); 
      startActivity(loginIntent); 
      break; 
     case R.id.btnup: 
      Bitmap image = ((BitmapDrawable) viewImage.getDrawable()).getBitmap(); 
      new UploadImage(image, uploadImageName.getText().toString()).execute(); 
      selectedReport = spinner.getSelectedItem().toString(); 
      break; 
    } 
} 
+0

什麼是解釋? @Hussein El Feky –

+0

我看到你已經添加onClick方法,但是你沒有添加一個監聽器到你的按鈕。所以我解決了。 –

+0

我有聽衆在我的編碼,只是我抹去了他們所以說我沒什麼好說的,因爲它的所有編碼 –

相關問題