在微调器中,我在 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) {
}
});
}
我的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/