2

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" />

Edit

If I take away imgid from my ArrayAdapter i does not filter my listView so I tried to add R.layout.mylistto 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
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52
  • The base implementation of `ArrayAdapter` only supports very limited layouts with a `TextView`. You were correct to implement a custom adapter and filtering should still work on it. You can add the filtering code to see if we can spot any issues. – George Mulligan Apr 16 '16 at 21:00
  • I have tries filtering with my Custom Adapter and it doesn't seem to work unless I'm doing it wrong but I can't see how. Any ideas on how I would do this with a Custom Adapter. – Craig Gallagher Apr 16 '16 at 21:02
  • What do you mean doesn't seem to work? What is it currently doing? You can search online for an example or look at another Q/A regarding [filtering](http://stackoverflow.com/questions/6492214/custom-filtering-arrayadapter-in-listview) – George Mulligan Apr 16 '16 at 21:07
  • I have tries filtering using my Custom Adapter and it doesn't filter my list I tried to add in `R.id.layout.mylist` and I then get an error not sure what I'm doing wrong – Craig Gallagher Apr 16 '16 at 21:13
  • As I mentioned you can add to your post the code for the `CustomAdapter` so we can try and find what you did wrong. – George Mulligan Apr 16 '16 at 21:15
  • I added an update to my question it's under the "Edit" heading. Am I going about this the correct way? – Craig Gallagher Apr 16 '16 at 21:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109348/discussion-between-craig-gallagher-and-george-mulligan). – Craig Gallagher Apr 16 '16 at 21:58

1 Answers1

1

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.

George Mulligan
  • 11,813
  • 6
  • 37
  • 50
  • 1
    By chance I went back to the chat we had and saw your last comment. Just so you know users are not notified when new messages are posted there. It is better to leave a comment here so that I get notified. I answered your latest question in the chat if you open it again. Let me know if you have further issues. – George Mulligan Apr 18 '16 at 01:26