I have made an application that has a list of insurance companies using a listView
. The listView
is populated using an array. I can currently search through this list using a filter. However beside each item in the listView
I want an image, I can do this using CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
however this stops my search from working so I'm trying to implement it like this adapter = new ArrayAdapter<String>(this, R.layout.mylist, R.id.textView1, itemname, imgid);
which is then throwing a Cannot resolve constructor ArrayAdapter (saveourcar.soc.Insurance,int,int, java.lang.String[],java.lang.Integer[])
error. Is there anyway I can have my imgid
included in my ArrayAdapter
.
My Code is as fallows
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" />
If I take away imgid from my ArrayAdapter i does not filter my listView so I tried to add R.layout.mylist
to my CustomListAdapter however when I try and add R.layout.myList
to my CustomListAdapter
I get the fallowing error
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
You should use your CustomListAdapter
for this since you need to show both a name and image in each row. One thing that you should do to make this easier is combine the image and name into a single object and update the CustomListAdapter
to work with a list of that new object instead of two separate arrays. Otherwise you will end up having to write a custom filter and filter both arrays yourself.
Also, create a toString()
method on that new object that returns the name. The ArrayAdapter
filter by default compares the String you enter to the toString()
of each object to know which records to keep and which to filter out. This will fix filtering on the CustomListAdapter
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments