【问题标题】:get Data, on item selected item from spinner. and send through onClick in Button从微调器中获取项目选定项目的数据。并通过 onClick in Button 发送
【发布时间】:2017-01-17 11:17:36
【问题描述】:

我已将数据传递给 Spinner。这是我的 JSON 数据,

我有一个微调器,我在其中将它传递到 JSON 下方。这是来自 post 方法的数据。

我的计划是在 Spinner 中获取以下数据。选择批次后,我将单击下面的按钮,它将“Batch_Id”发送到下一个活动,它将从微调器中选择“Batch”中获取“Batch_Id”。因为下一个活动希望 Batch_Id 从 URL 获取数据。

我已经完成了“批处理”的解析。但我被困在第二部分,我想根据选择的批次发送 Batch_Id。

[
{
  "Batch_Id": "1",
  "Batch": "2016-21"
},
{
  "Batch_Id": "2",
  "Batch": "2015-20"
},
{
  "Batch_Id": "3",
  "Batch": "2014-19"
},
{
  "Batch_Id": "4",
  "Batch": "2013-18"
},
{
  "Batch_Id": "5",
  "Batch": "2012-17"
},
{
  "Batch_Id": "6",
  "Batch": "2014-17"
},
{
  "Batch_Id": "7",
  "Batch": "2015-18"
},
{
  "Batch_Id": "8",
  "Batch": "2016-19"
}
]

我已经在 Spinner 中解析了“Batch”,现在我想明智地获取 Batch_Id 位置,比如如果我选择 2016-19,那么我将得到“batch_id”等于 8。

我试过了,

          final JSONArray jsonArraybatch = jsonArray.getJSONArray(1);
            JSONArray jsonArraysection = jsonArray.getJSONArray(2);
            JSONArray jsonArraysubject = jsonArray.getJSONArray(3);
            JSONObject jsonResponse = jsonArrayTeacherName.getJSONObject(0);
            teachername = jsonResponse.optString("teacher_name");
            teacherid = jsonResponse.optString("teacher_id");

            for (int i = 0; i < jsonArraybatch.length(); i++)

            {
                final JSONObject jsonObject = jsonArraybatch.getJSONObject(i);
                final Spinner mySpinner = (Spinner) findViewById(R.id.batch_spinner);

                String batch = jsonObject.optString("Batch");


                batchlist.add(batch);
                mySpinner
                        .setAdapter(new ArrayAdapter<String>(PutCredentials.this,
                                android.R.layout.simple_spinner_dropdown_item,
                                batchlist));

                mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        selectedBatch = mySpinner.getItemAtPosition(position).toString();
                        batchid = jsonObject.optString("Batch_Id");


                    }

【问题讨论】:

  • 到目前为止尝试了什么。显示相同的代码
  • 其实我只展示了JSON的一部分,因为我已经在Spinner中解析了Batch。但我的问题是我想将 Batch_Id 发送到 Button Click 上的下一个活动。
  • OnItemSelectedListener#onItemSelected,最后一个参数是id,你的适配器只需覆盖getItemId方法
  • here 你有完整的工作代码,只需将其添加到Activity#onCreate 即可查看

标签: android json post android-spinner


【解决方案1】:

你有两个选择

  1. 用两个成员变量创建一个骨架BatchbatchNamebatchId 并为传递 Array&lt;Batch&gt; 的微调器编写自定义适配器
  2. 使用地图作为参考。

为微调器使用自定义适配器

适配器

   public class SchoolSpinnerAdapter extends BaseAdapter {
   private ArrayList<School> mSchools;
   private Context mContext;

   public SchoolSpinnerAdapter(Context ctx, ArrayList<School> schools) {
       mContext = ctx;
       mSchools = schools;
   }

   @Override
   public int getCount() {
       return mSchools.size();
   }

   @Override
   public Object getItem(int position) {
       return mSchools.get(position);
   }

   @Override
   public long getItemId(int position) {
       return 0;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       if (convertView == null) {
           LayoutInflater inf = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           convertView = inf.inflate(R.layout.spinner_text, null);
       }
       ((TextView) convertView).setText(mSchools.get(position).getName());
       return convertView;
   }
}

布局膨胀

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
style="?android:attr/dropDownItemStyle"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:background="#cccccc"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ellipsize="marquee" />

将此适配器设置为您的微调器

spinner.setAdapter(new SchoolAdapter(this,schoollist));

使用地图作为参考

为微调器准备值

String[] spinnerArray = new String[Province_ID.size()];
HashMap<String,String> spinnerMap = new HashMap<String, String>();
for (int i = 0; i < Province_ID.size(); i++)
{
   spinnerMap.put(Province_NAME.get(i),Province_ID.get(i));
   spinnerArray[i] = Province_NAME.get(i);
}

设置微调器的值

ArrayAdapter<String> adapter =new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

为微调器创造价值

String name = spinner.getSelectedItem().toString();
String id = spinnerMap.get(name);

【讨论】:

    【解决方案2】:

    创建模型类:

    class BatchModel{
    
     String batchId;
    
     String batchName;
    
    }
    

    batchlist 变量必须是 BatchModel 类型

     ex: List<BatchModel> list= new ArrayList<>();
    
    
     String batch = jsonObject.optString("Batch");
     String batchid = jsonObject.optString("Batch_Id");
    
     BatchModel bm= new BatchModel();
     bm.batchId=batchid;
     bm.batch=batch;
     list.add(bm);
    

    SpinnerAdapter 类

    public class SpinnerAdapter extends ArrayAdapter<BatchModel> {
    
    public SpinnerAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }
    
    public ListAdapter(Context context, int resource, ,List<BatchModel> listItems) {
        super(context, resource, listItems);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        View v = convertView;
    
        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.itemlistrow, null);
        }
    
        BatchModel bm = getItem(position);
    
        if (v != null) {
            TextView tt1 = (TextView) v.findViewById(R.id.batch);
             tt1.setText(bm.batch);
    
        }
    
        return v;
    }
    
    }
    

    【讨论】:

    • 你需要使用自定义的Arrayadapter或者base adapter来设置值。
    • if (p != null) { TextView tt1 = (TextView) v.findViewById(R.id.batch); tt1.setText(bm.batch); } 这里的 p 是什么?
    • 这是视图。我在此页面本身中输入了代码。通过手机。我更新它。
    【解决方案3】:

    首先将此方法保留在外面:

    mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            selectedBatch = mySpinner.getItemAtPosition(position).toString();
                            batchid = jsonObject.optString("Batch_Id");
    
    
                        }
    

    现在你可以使用这个来找到你的 batchid:

    @Override
    
    
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            selectedBatch = mySpinner.getItemAtPosition(position).toString();
                            for(int i = 0;jsonArraybatch.length(); i++){
            JSONObject jsonObject = jsonArraybatch.getJSONObject(i);
           if (selectedBatch.equals(jsonObject.optString("Batch"))){
               batchid = jsonObject.optString("Batch_Id");
       }
    }
    
    
                        }
    

    【讨论】:

    • 它给了我 batch_id = 8,对于任何选择.. bcoz 有一个 for 循环,因为它一直给 8。顺便说一句。
    • 您可以在创建适配器时显式创建 hashmap,其中-batch 将是键,batch_id 将是其对应的值,然后只需在 hashmap 中的值对中找到 batch_id
    猜你喜欢
    • 2021-12-04
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    相关资源
    最近更新 更多