4

This is the tutorial I have used: http://www.tutorialsbuzz.com/2014/08/filter-custom-listviewbaseadapter.html

and I implemented to it an onItemClickListener

    OnItemClickListener myListViewClicked = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String member_name = countrylist.get(position).getName();
            // get Internet status
            isInternetPresent = cd.isConnectingToInternet();

            if (isInternetPresent) {
                if (member_name.equals("aa")) {
                    Intent i = new Intent(Listview.this, Start.class);
                    startActivity(i);
                    displayInterstitial();
            } else {
                prName.show();
            }
        }
    };
    lv.setOnItemClickListener(myListViewClicked);


}

so, let us suppose my original list is

Aa
BB
Ca
DD

and I type 'a' in the filter, then the list becomes

Aa
Ca

But when I click Ca, I get redirected into BB using further action that depends on .equal for each list item.

My code for my list and custom adapter is the same as in the tutrial, but the onclickitem listener, i have implemeneted it, so I think im missing something in it. I tried searching and found couple of same quesiton, but none answered, each with self-answer that was different than mine and i couldn't apply it to my cusotm adapter.

user3549071
  • 117
  • 5

2 Answers2

3

it is mostly because your are accessing the dataset directly.

 String member_name = countrylist.get(position).getName();

During the filtering you are probably instantiating and new Collection overriding the the reference you are using in your Activity.

Use

String member_name = ((Country)parent.getItemAtPosition(position)).getName();

and it will work.

parent.getItemAtPosition(position) accesses the underlying dataset in your Adapter, if your getItem is correctly implemented

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Sorry but that doesn't work unfortunetly, because .getName doesn't apply to it. I tried this as I have seen it in another answer, but it doesn't work :"(. "Can't resolve method 'getName'" – user3549071 Dec 16 '15 at 19:23
  • `((Country)parent.getItemAtPosition(position)).getName()` – Blackbelt Dec 16 '15 at 19:26
  • Wow, it works perfectly .. Can you tell me what have u done? – user3549071 Dec 16 '15 at 19:29
  • `getItemAtPosition`, returns a generic `Object`. Using `((Country)parent.getItemAtPosition(position))` you are casting the generic object to `Country` – Blackbelt Dec 16 '15 at 20:08
1

You can use

String memberName=(YourObject)parent.getAdapter().get(postion).getName;

its easy.

Numan Turkeri
  • 526
  • 4
  • 18