如何在微调器中隐藏项目的ID?

时间:2018-09-17 07:57:29

标签: java android arraylist

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

private void 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在这里:

公共类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;
}

}

2 个答案:

答案 0 :(得分:0)

目标必须是具有ID和名称的对象,而不是String。然后,在适配器内部,只需显示名称

答案 1 :(得分:0)

首先使您的列表中包含一个类似

的对象
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;
}

您已完成。希望这会有所帮助。