Java – The wrong location of the project after using customAdapter

The wrong location of the project after using customAdapter… here is a solution to the problem.

The wrong location of the project after using customAdapter

Here’s the tutorial I’ve used: http://www.tutorialsbuzz.com/2014/08/filter-custom-listviewbaseadapter.html

I implemented an onItemClickListener for it

    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’s assume my original list was

Aa
BB
Ca
DD

Then I enter “a” in the filter and the list becomes

Aa
Ca

However, when I click on the ca, I redirect to BB with further actions that depend on .equal for each list item.

My list and custom adapter code is the same as in the tutorial, but I’ve implemented the onclickitem listener, so I guess I’m missing something in it. I tried searching and found several identical questions but no one answered, the self answer to each question was different from mine, and I couldn’t apply it to my cusotm adapter.

Solution

This is mainly because you are accessing the dataset directly.

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

During filtering, you may be instantiating a new Collection to overwrite the references you used in the activity.

Use

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

It will work.

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

Related Problems and Solutions