当前位置: 首页 > 知识库问答 >
问题:

android可搜索旋转适配器中的问题

刘棋
2023-03-14

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

    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每个项目旋转器输出的垃圾值

    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;
    }

}
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;
}
}

共有1个答案

邴俊友
2023-03-14

您可以重写模型(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;
    }
}
 类似资料:
  • 当我开始搜索/筛选recyclerview列表结果时,我就会从搜索适配器中的onBind方法获取IndexOutofBoundsException。这里有个例外: 我在网上试了几篇文章,但没有成功。我尝试的一些文章是:RecolyerView适配器onBind方法和RecolyerView:Inconsistency Detected。无效的物料位置 此问题的适配器代码位于:https://pas

  • 问题内容: 我正在尝试创建一个Rotatable ImageView,我将向其指定特定角度和枢轴点,并查看其围绕该枢轴点旋转。我尝试过这样的事情: 但是postRotate方法的参数(第二个和第三个-枢轴点)完全没有变化。即使它们是0、0,也一样。 所以我想创建一个ImageView,在初始化时将旋转一定角度。在此示例中为45度。我试图设置范围和人员..没有帮助。 我怎么做?:/ 问题答案: 您可

  • 请帮助我得到NullPointerException错误。我是新的碎片,我尝试了没有碎片,它是工作的,我不明白为什么它不工作,当我实现它在碎片上,我已经检查了数据库,数据库没有问题,谢谢。 公共类DatabaseHandler扩展SQLiteOpenHelper{ }

  • 我正在使用pagerAdapter显示带有片段支持的内容。我得到了错误 当我在Android2.2或更低版本上运行应用程序时。但在安装Android4.0的设备上,这种方法运行得很好。请检查有错误的日志详细信息: java.lang.NoSuchMethodError:android.webkit.webview.SetLayerType 02-28 11:19:08.205:错误/Android

  • 我正在尝试在ndroid ListView上应用搜索,但它对我不起作用,这里是我尝试的代码。 下面是Users类

  • 需要一个解决方案来转换PDF文件,其中每个页面都是图像,页面可以包含文本,表格或两者的组合到可搜索的pdf。 我使用了ABBY FineReader Online,这项工作做得非常好,但我正在寻找一个可以通过Windows Python实现的解决方案 我已经做了详细的分析,下面的链接接近我想要的,但并不准确: 扫描图像/PDF到可搜索图像/PDF 它告诉我们首先要使用Ghost脚本将其转换为图像,