【问题标题】:I Want To Itemonclicklister in Fragment on Spinner我想在 Spinner 的 Fragment 中添加 Itemclicklister
【发布时间】:2021-07-03 15:59:41
【问题描述】:

这是主要片段 片段:


  private void getStock() {
          dialog.show();
          Retrofit retrofit = RetrofitClient.getRetrofitInstance();
          apiInterface api = retrofit.create(apiInterface.class);
          Call<List<Blocks>>call = api.getVaccineBlocks();
          call.enqueue(new Callback<List<Blocks>>() {
              @Override
              public void onResponse(Call<List<Blocks>>call, Response<List<Blocks>> response) {
                  if (response.code() == 200) {
                      block = response.body();
                     spinnerada();
                      dialog.cancel();
  
                  }else{
                      dialog.cancel();
  
                  }
              }
  
              @Override
              public void onFailure(Call<List<Blocks>> call, Throwable t) {
                  dialog.cancel();
              }
          });
      }
  
      private void spinnerada() {
  
  
          String[] s = new String[block.size()];
          for (int i = 0; i < block.size(); i++) {
              s[i] = block.get(i).getBlockName();
              final ArrayAdapter a = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, s);
              a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
              //Setting the ArrayAdapter data on the Spinner
              spinner.setAdapter(a);
            
          }
  
  
      }

这是块模型 型号:

    package com.smmtn.book.models;
    
    import java.io.Serializable;
    
    public class Blocks implements Serializable {
        public String id;
        public String blockName;
        public String blockSlug;
    
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getBlockName() {
            return blockName;
        }
    
        public void setBlockName(String blockName) {
            this.blockName = blockName;
        }
    
        public String getBlockSlug() {
            return blockSlug;
        }
    
        public void setBlockSlug(String blockSlug) {
            this.blockSlug = blockSlug;
        }
    }

这里我需要带有 blocklug 的 onitemclick,请任何人都可以提供帮助,我是 android 新手,所以我需要一些示例。点击时我想要使用 blocklug 并使用该 blocklug 加载另一种方法,就像会从你 "http:/" 获取数据/example.com/block/"+blockslug 我想从选定的块中获取块块 我希望伙计们我能得到帮助 对不起我的英语不好,

【问题讨论】:

    标签: android-studio android-spinner


    【解决方案1】:

    首先,你需要实现setOnItemSelectedListener。参考这个https://stackoverflow.com/a/20151596/9346054

    选择项目后,您可以通过创建新方法来调用它们。如下示例

    public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        Toast.makeText(parent.getContext(), 
            "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
            Toast.LENGTH_SHORT).show();
        final String itemSelected = parent.getItemAtPosition(pos).toString();
        showBlockSlug(itemSelected);
    }
    

    然后,在方法 showBlockSlug() 中,您可以调用 Retrofit。

    private void showBlockSlug(final String blockslug){
       final String url = "http://example.com/block/"+ blockslug;
       //Do your stuff...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多