【问题标题】:Cannot resolve constructor ArrayAdapter (saveourcar.soc.Insurance,int,int, java.lang.String[],java.lang.Integer[])无法解析构造函数 ArrayAdapter (saveourcar.soc.Insurance,int,int, java.lang.String[],java.lang.Integer[])
【发布时间】:2016-08-08 18:01:57
【问题描述】:

我创建了一个应用程序,其中包含使用 listView 的保险公司列表。 listView 使用数组填充。我目前可以使用过滤器搜索此列表。然而,除了listView 中的每个项目之外,我想要一个图像,我可以使用CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid); 来执行此操作,但这会阻止我的搜索工作,所以我试图像这样实现它adapter = new ArrayAdapter<String>(this, R.layout.mylist, R.id.textView1, itemname, imgid);,然后抛出一个Cannot resolve constructor ArrayAdapter (saveourcar.soc.Insurance,int,int, java.lang.String[],java.lang.Integer[])错误。无论如何我可以将我的imgid 包含在我的ArrayAdapter 中。

我的代码是休闲的

Insurance.java

    public class Insurance extends AppCompatActivity {
        ListView list;
        ArrayList<String> listItems;
        ArrayAdapter<String> adapter;
        EditText inputSearch;
        String[] itemname ={
                "123.ie",
                "AA",
                "Acorn",
                "Admiral",
                "AIG",
                "Allianz",
                "Aviva",
                "Auto Direct",
                "AXA",
                "Chill",
                "Churchill",
                "CoverBox",
                "DirectChoice",
                "FBD",




        };

        Integer[] imgid= {
                R.drawable.onetwothree,
                R.drawable.aa,
                R.drawable.acorn,
                R.drawable.admiral,
                R.drawable.aig,
                R.drawable.allianz,
                R.drawable.aviva,
                R.drawable.autodirect,
                R.drawable.axa,
                R.drawable.chill,
                R.drawable.churchill,
                R.drawable.coverbox,
                R.drawable.directchoice,
                R.drawable.fbd,


        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_insurance);
            adapter = new ArrayAdapter<String>(this, R.layout.mylist, R.id.textView1, itemname, imgid);
            CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
            list=(ListView)findViewById(R.id.list);
            inputSearch = (EditText) findViewById(R.id.itemtext);
            list.setAdapter(adapter);


            inputSearch.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                    Insurance.this.adapter.getFilter().filter(cs);


                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                              int arg3) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                }
            });

Insurance.xml

    <EditText
        android:layout_width="400dp"
        android:layout_height="60dp"
        android:id="@+id/itemtext"
        android:layout_marginBottom="50dp"

/>
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="107dp">
    </ListView>

myList.xml

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="60dp"
    android:layout_weight="2"
    >
    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="6"
    android:orientation="vertical"  >

    <TextView
        android:id="@+id/item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:padding="2dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF"
        />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="TextView"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    >
<ImageView
    android:id="@+id/imageView2"
    android:layout_width="20dp"
    android:layout_height="50dp"
    android:layout_alignParentRight="true"
    android:src="@drawable/nextarrow"
    android:layout_marginTop="6dp" />

编辑

如果我从我的 ArrayAdapter 中删除 imgid,我不会过滤我的 listView,所以我尝试将 R.layout.mylist 添加到我的 CustomListAdapter 但是当我尝试将 add R.layout.myList 添加到我的 CustomListAdapter 时,我得到了休闲错误

Error:(67, 35) error: constructor CustomListAdapter in class CustomListAdapter cannot be applied to given types;
required: Activity,String[],Integer[]
found: Insurance,String[],Integer[],int
reason: actual and formal argument lists differ in length

【问题讨论】:

  • ArrayAdapter 的基本实现仅支持带有TextView 的非常有限的布局。您实现自定义适配器是正确的,过滤应该仍然可以使用。您可以添加过滤代码,看看我们是否可以发现任何问题。
  • 我尝试使用我的自定义适配器进行过滤,但它似乎不起作用,除非我做错了,但我不知道如何做。关于如何使用自定义适配器执行此操作的任何想法。
  • 你的意思是似乎不起作用?它目前在做什么?您可以在线搜索示例或查看关于 filtering 的另一个 Q/A
  • 我尝试使用我的自定义适配器进行过滤,但它没有过滤我尝试添加到 R.id.layout.mylist 的列表,然后我收到一个错误,不确定我做错了什么
  • 正如我所提到的,您可以将CustomAdapter 的代码添加到您的帖子中,这样我们就可以尝试找出您做错了什么。

标签: java android android-studio android-arrayadapter android-search


【解决方案1】:

您应该为此使用CustomListAdapter,因为您需要在每一行中显示名称和图像。为了使这更容易,您应该做的一件事是将图像和名称合并到一个对象中并更新CustomListAdapter 以使用该新对象的列表而不是两个单独的数组。否则,您最终将不得不自己编写自定义过滤器并过滤两个数组。

另外,在返回名称的新对象上创建一个toString() 方法。默认情况下,ArrayAdapter 过滤器会将您输入的字符串与每个对象的toString() 进行比较,以了解要保留哪些记录以及要过滤掉哪些记录。这将修复对CustomListAdapter 的过滤。

【讨论】:

  • 偶然我回到了我们的聊天室,看到了你最后的评论。只是为了让您知道当新消息在那里发布时不会通知用户。最好在这里发表评论,以便我得到通知。如果您再次打开它,我在聊天中回答了您的最新问题。如果您还有其他问题,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 1970-01-01
相关资源
最近更新 更多