我通过扩展ArrayAdapter<>类创建了一个自定义微调器:
public class SpinnerAdapter extends ArrayAdapter<Category> {
Activity activity ;
List<Category> mCategoriesList ;
int itemResourceId ;
public SpinnerAdapter(Activity activity , List<Category> CategoriesList,int itemResourceId){
super(activity,itemResourceId,CategoriesList);
this.activity=activity;
this.mCategoriesList=CategoriesList;
this.itemResourceId=itemResourceId;
}
public View getView(int position , View convertView , ViewGroup parent){
View MyCustomLayout = convertView ;
if(MyCustomLayout== null){
LayoutInflater Inflator = activity.getLayoutInflater();
MyCustomLayout = Inflator.inflate(R.layout.custom_spinner_layout,parent,false);
}
TextView FullN = MyCustomLayout.findViewById(R.id.CatSubCatName);
ImageView ProfP = MyCustomLayout.findViewById(R.id.BloodyIcon);
FullN.setText(mCategoriesList.get(position).getName());
return MyCustomLayout ;
}
}提供的mCategoriesList是通过解析jsonArray获得的:
try {
JSONArray liste = new JSONArray(response);
for (int i=0;i<liste.length();i++){
JSONObject obj = liste.getJSONObject(i);
Category cat= new Category();
cat.setName(obj.getString("name"));
cat.setID(obj.getInt("ID_categorie"));
mCategoriesList.add(cat);}}
catch(Throwable t){
t.printStackTrace();
}下面是与微调器对应的自定义布局的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/BloodyIcon"
android:layout_marginStart="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/CatSubCatName"
android:textSize="21sp"
android:textColor="@color/Purple"
android:fontFamily="sans-serif-condensed"
android:layout_marginStart="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>这是Category类:
public class Category {
private int ID ;
private String Name ;
public Category(int ID ,String Name){
this.ID=ID ;
this.Name=Name ;
}
public Category(){
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}问题是,当我单击微调器时,我得到的不是类别名称的列表,而是类似于:packagename.Category@(随机字符),然而所选项目正确地显示了名称,我是否遗漏了什么?
发布于 2018-04-24 04:19:25
通过在适当的位置放置断点来确保正确解析名称
https://stackoverflow.com/questions/49988849
复制相似问题