为了账号安全,请及时绑定邮箱和手机立即绑定

如何将 PLEASE SELECT 错误添加到 JSON Spinner

如何将 PLEASE SELECT 错误添加到 JSON Spinner

繁星点点滴滴 2023-06-14 16:12:59
我有一个应用程序,您可以在其中将图像上传到我公司的服务器我有 2 个微调器填充了 json 数据,微调器中的选定项目被传递给上传 url 的 uri.builder,我已经看到很多关于设置请选择微调器上的选项但是我的微调器的请选择选项作为项目编码到我的 JSON 数据中。现在我想要的是,如果选择了 please select item 以提示用户在微调器中选择一个项目,应用程序将给出一个错误总而言之,我的微调器中填充了 json 数据,json 数据中的第一项是“请选择”,如果选择了“请选择”选项,我希望显示一条错误消息。   public class SecondActivity extends AppCompatActivity implements    View.OnClickListener {private final int PICK_IMAGE=12345;private final int REQUEST_CAMERA=6352;private static final int REQUEST_CAMERA_ACCESS_PERMISSION=5674;private Bitmap bitmap;private ImageView imageView; String myURL; Spinner spinner; Spinner spinner2; String URL; String URL2; ArrayList<String> CategoryName; ArrayList<String> ClientName; String Item; String Item2; String email; String clientId; String pwd;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);     setContentView(R.layout.activity_second);     imageView=findViewById(R.id.imageView);     Button fromCamera=findViewById(R.id.fromCamera);     Button fromGallery=findViewById(R.id.fromGallery);     Button upload=findViewById(R.id.upload);     CategoryName=new ArrayList<>();     ClientName=new ArrayList<>();     spinner=findViewById(R.id.spinner);     spinner2=findViewById(R.id.spinner2);     email = getSharedPreferences("MyPrefs", MODE_PRIVATE).getString("name", "");     clientId= getSharedPreferences("MyPrefs", MODE_PRIVATE).getString("id", "");     pwd= getSharedPreferences("MyPrefs", MODE_PRIVATE).getString("password", "");    CheckBox chk =findViewById(R.id.chk1);    if (chk.isChecked()) {         Uri.Builder builder=new Uri.Builder();         builder.scheme("https")
查看完整描述

2 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

将以下行添加到微调器方法的顶部onItemSelected。


  if(i==0){

    ///Here you need to show the error msg for the first item selected

    Log.v("ERROR","Please select an item callded");


    //return is used the break the flow of the app so the code below does not run in this case

    return;


    }


查看完整回答
反对 回复 2023-06-14
?
DIEA

TA贡献1820条经验 获得超2个赞

在第一个位置添加您的项目。在 Spinner 上添加一个 Item Selected Check Listner 并添加一个检查是否选择的值是第一个。如果先显示消息


spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override

    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

        // your code here

        if(position==0)

            Toast.makeText(getContext,"Please select a value",Toast.LENGTH_LONG).show()

    }


    @Override

    public void onNothingSelected(AdapterView<?> parentView) {

        // your code here

    }


});


查看完整回答
反对 回复 2023-06-14
  • 2 回答
  • 0 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信