java - 如何隐藏微调器中项目的 id?

标签 java android arraylist

在微调器中,我在 ArrayList 中添加“target Id”和“target name”,在微调器中,“target Id”和“target name”都显示,但我只想显示目标名称在微调器中

私有(private)无效displaypinnertarget() {

    final ArrayList<String> target = new ArrayList<String>();

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    JsonArrayRequest jsonArrayRequest= new JsonArrayRequest("http://54.146.132.94/webservices/target_allocated?parent_id="+ get_id +"&target_status=5",
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {



                        try {

                            for (int i = 0; i < response.length(); i++) {
                                JSONObject jsonObject = response.getJSONObject(i);
                                target.add(jsonObject.getString("target_id")+" . "+jsonObject.getString("target_name"));


                            }

                            /*ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(Behavior_data_create.this,
                                    android.R.layout.simple_spinner_item,target);

                            arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);*/
                            CustomAdapter_spinner customAdapter_spinner=new CustomAdapter_spinner(getApplicationContext(),target);


                            a4.setAdapter(customAdapter_spinner);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }



                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            Toast.makeText(Behavior_data_create.this, ""+error.toString(), Toast.LENGTH_SHORT).show();

        }
    });
    requestQueue.add(jsonArrayRequest);

    a4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            item= parent.getItemAtPosition(position).toString();
            t_id = item.substring(0,2).trim();
            Toast.makeText(Behavior_data_create.this, "" + item, Toast.LENGTH_SHORT).show();

        }

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

        }
    });

}

in this image, 2 is target id and abc is target name, so I only want o display name here and when I click on the item, I will need the id (means that number 2)

我的customadapter_spinner.class在这里:

公共(public)类 CustomAdapter_spinner 扩展 BaseAdapter{

Context context;
ArrayList<String> countryNames;
LayoutInflater inflter;

public CustomAdapter_spinner(Context applicationContext, ArrayList<String> countryNames) {
    this.context = applicationContext;
    this.countryNames = countryNames;
    inflter = (LayoutInflater.from(applicationContext));
}

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

@Override
public Object getItem(int position) {
    return null;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    convertView = inflter.inflate(R.layout.custom_spinner_items, null);
    TextView names = (TextView) convertView.findViewById(R.id.textView);
    names.setText(countryNames.get(position));
    return convertView;
}

}

最佳答案

首先让你的列表接受一个像这样的对象

final ArrayList<Target> target = new ArrayList<Target>();

然后为您的目标数据创建一个 POJO 类

public class Target {

      private String targetId;
      private String targetName;

      public void setTargetId(String targetId){
         this.targetId = targetId;      
      }

      public String getTargetId(){
         return targetId;
      }

      public void setTargetName(String targetName){
         this.targetName= targetName;      
      }

      public String getTargetName(){
         return targetName;
      }

}

然后在你的循环中

for (int i = 0; i < response.length(); i++) {
    JSONObject jsonObject = response.getJSONObject(i);

    Target targetObject = new Target();
    targetObject.setTargetId(jsonObject.getString("target_id"));
    targetObject.setTargetName(jsonObject.getString("target_name"));        

    target.add(targetObject);
}

然后在 CustomAdapter_Spinner 类的 getView() 方法中仅获取目标名称,例如

@Override
public View getView(int position, View convertView, ViewGroup parent) {

convertView = inflter.inflate(R.layout.custom_spinner_items, null);
TextView names = (TextView) convertView.findViewById(R.id.textView);
names.setText(countryNames.get(position).getTargetName());
return convertView;
}

你就完成了。希望这会有所帮助。

关于java - 如何隐藏微调器中项目的 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52363181/

相关文章:

android - 如何获取数据库路径和名称

javascript - 如何在 javascript 中将项目转换为键/值对对象?

java - 防止程序员获取类实例的锁

Java MVP - 不持有 Presenter 对象的引用?

android - Ubuntu 上的 Appium 更新

android - 在 Android 中将 & 转换为 &?

android - 如何将位置类型 ArrayList 从 android 中的 MainActivity 传递给 MapsActivity?

java - 我正在填充 ArrayList<Integer> 类型的 ArrayList。插入完成两次,但结果仅反射(reflect)第二次更新。解释

Java如何在作业完成时调用或通知调用类?

java - 正如我可以在一个类中关联两个不同的文件与 jaxb