gpt4 book ai didi

android - 可搜索微调器适配器 android 中的问题

转载 作者:太空狗 更新时间:2023-10-29 13:09:48 26 4
gpt4 key购买 nike

我正在我的应用程序中集成可搜索微调器。下面是我的代码

gradle 文件

    compile 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1'

XML文件

 <com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:hintText="Select Country"/>

Man.java

public class MainActivity extends AppCompatActivity {

SearchableSpinner mSearchableSpinner;
ArrayList<GetCountry> mGetCountries;
PriorityAdapter mPriorityAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSearchableSpinner = (SearchableSpinner) findViewById(R.id.spinner);

mGetCountries = new ArrayList<>();
GetCountry mGetCountry = new GetCountry();
mGetCountry.setId("1");
mGetCountry.setName("India");
mGetCountries.add(mGetCountry);

GetCountry mGetCountry2 = new GetCountry();
mGetCountry2.setId("2");
mGetCountry2.setName("USA");
mGetCountries.add(mGetCountry2);


GetCountry mGetCountry3 = new GetCountry();
mGetCountry3.setId("3");
mGetCountry3.setName("UK");
mGetCountries.add(mGetCountry3);

GetCountry mGetCountry4 = new GetCountry();
mGetCountry4.setId("4");
mGetCountry4.setName("CHINE");
mGetCountries.add(mGetCountry4);

GetCountry mGetCountry5 = new GetCountry();
mGetCountry5.setId("5");
mGetCountry5.setName("MALASIYA");
mGetCountries.add(mGetCountry5);

mPriorityAdapter=new PriorityAdapter(mGetCountries);
mSearchableSpinner.setAdapter(mPriorityAdapter);
}


public class PriorityAdapter extends ArrayAdapter<GetCountry> {

ArrayList<GetCountry> list;

public PriorityAdapter(ArrayList<GetCountry> list) {
super(MainActivity.this, R.layout.spin_layout, list);
this.list = list;
}

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


return initView(position, convertView, parent);
}

@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) { // This view starts when we click the
// spinner.
return initView(position, convertView, parent);
}

public View initView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflator = getLayoutInflater();
convertView = inflator.inflate(R.layout.spin_layout, null);
TextView mTextView = (TextView) convertView
.findViewById(android.R.id.text1);
mTextView.setText(list.get(position).getName());
return convertView;
}

}

}

当我运行上面的代码时,我得到如下图所示的输出。自定义 arraylist 数据不显示它是 java 每一项的打印垃圾值 Spinner Output

知道如何解决这个问题吗?

编辑

    public class PriorityAdapter extends ArrayAdapter<GetCountry> {

ArrayList<GetCountry> list;

public PriorityAdapter(ArrayList<GetCountry> list) {
super(MainActivity.this, R.layout.spin_layout, list);
this.list = list;
}

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


return initView(position, convertView, parent);
}

@Nullable
@Override
public GetCountry getItem(int position) {
return super.getItem(position);
}



@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) { // This view starts when we click the

View view = convertView;
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(R.layout.spin_layout_1, parent, false);

TextView tv= (TextView) view.findViewById(R.id.text1);
tv.setText(list.get(position).getName());
return view;

}

public View initView(int position, View convertView, ViewGroup parent) {


View view = convertView;
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(R.layout.spin_layout_1, parent, false);

TextView tv= (TextView) view.findViewById(R.id.text1);
tv.setText(list.get(position).getName());
return view;
}

}

GetCountry.java

public class GetCountry {
String name;
String id;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}

最佳答案

您可以像这样覆盖模型 (GetCountry) 的 toString() 函数:

GetCountry.java

public class GetCountry {
String name;
String id;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@Override
public String toString() {
return name;
}
}

关于android - 可搜索微调器适配器 android 中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42507097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com