无法解析构造函数 ArrayAdapter (saveourcar.soc.Insurance,int,int, java.lang.String[],java.lang.Integer[])
Cannot resolve constructor ArrayAdapter (saveourcar.soc.Insurance,int,int, java.lang.String[],java.lang.Integer[])
我已经使用 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
您应该为此使用 CustomListAdapter
,因为您需要在每一行中同时显示名称和图像。为了使这更容易,您应该做的一件事是将图像和名称组合到一个对象中,并更新 CustomListAdapter
以使用该新对象的列表而不是两个单独的数组。否则你最终将不得不编写一个自定义过滤器并自己过滤两个数组。
此外,在 returns 名称的新对象上创建一个 toString()
方法。默认情况下,ArrayAdapter
过滤器会将您输入的字符串与每个对象的 toString()
进行比较,以了解要保留哪些记录以及要过滤掉哪些记录。这将修复 CustomListAdapter
.
上的过滤
我已经使用 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
您应该为此使用 CustomListAdapter
,因为您需要在每一行中同时显示名称和图像。为了使这更容易,您应该做的一件事是将图像和名称组合到一个对象中,并更新 CustomListAdapter
以使用该新对象的列表而不是两个单独的数组。否则你最终将不得不编写一个自定义过滤器并自己过滤两个数组。
此外,在 returns 名称的新对象上创建一个 toString()
方法。默认情况下,ArrayAdapter
过滤器会将您输入的字符串与每个对象的 toString()
进行比较,以了解要保留哪些记录以及要过滤掉哪些记录。这将修复 CustomListAdapter
.