我通过扩展ArrayAdapter<>来制作自定义微调器。 class:
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 @(randomcharacters),但是所选项目正确显示名称,我在这里遗漏了一些东西?
答案 0 :(得分:0)
这是java对象默认打印的{{1}}方法。
确保通过在适当的位置放置断点来正确解析名称