我已经做了一个应用程序,其中有一个使用listView的保险公司列表。listView是使用数组填充的。我目前可以使用筛选器搜索此列表。然而,除了listView中的每一项之外,我想要一个图像,我可以使用CustomListAdapter adapter=new CustomListAdapter(this, 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发布于 2016-04-16 23:02:24
您应该为此使用您的CustomListAdapter,因为您需要在每一行中同时显示名称和图像。为了使这更容易,您应该做的一件事是将图像和名称组合到一个对象中,并更新CustomListAdapter以处理这个新对象的列表,而不是两个单独的数组。否则,您将不得不自己编写自定义筛选器并对两个数组进行筛选。
另外,在返回名称的新对象上创建一个toString()方法。默认情况下,ArrayAdapter筛选器将输入的字符串与每个对象的toString()进行比较,以了解要保存哪些记录和筛选哪些记录。这将修复CustomListAdapter上的筛选。
https://stackoverflow.com/questions/36669590
复制相似问题