0

Hello i have problem using autocomplete with data from json/web server, i already success get data and display to autocomplete but when i try to type again i hava multiple result. Can anyone tell me how to fix this. Thanks in advance...

Here my code ===================Activity=================

package com.ctg.base.textoaster;

import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctg.base.BaseActivity;
import com.ctg.base.R;
import com.ctg.base.adapter.AddWebsiteAdapter;
import com.ctg.base.model.response.ToasterWebsiteModel;
import com.ctg.base.navigation.Navigator;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class ForthCreateAccountActivity extends BaseActivity {

    @BindView(R.id.tvTitle) TextView title;
    @BindView(R.id.edtKeyword) EditText edtKeyword;
    @BindView(R.id.edtWebsite) AutoCompleteTextView edtWebsite;
    @BindView(R.id.checklist) ImageView checlist;

    private ArrayList<ToasterWebsiteModel> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initialize(this, R.layout.activity_forth_create_account);

        initControl();
    }

    private void initControl(){
        list = new ArrayList<>();
        title.setTypeface(null, Typeface.BOLD);

        list.clear();
        AddWebsiteAdapter adapter = new AddWebsiteAdapter(context, android.R.layout.simple_dropdown_item_1line, list);
        edtWebsite.setAdapter(adapter);

    }

    @OnClick(R.id.btnPrev) void goToThirdCreateAccount() {
        onBackPressed();
//        navigator.navigate(Navigator.THIRDCREATEACCOUNT);
    }

    @OnClick(R.id.btnNext) void goToFinishCreateAccount() {
        navigator.navigate(Navigator.FORTHCREATEACCOUNT);
    }
}

=======================ADAPTER===================

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.ctg.base.R;
import com.ctg.base.model.response.ToasterWebsiteModel;
import com.ctg.base.service.BaseService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by GIGABYTE on 11/01/2017.
 */
public class AddWebsiteAdapter extends ArrayAdapter implements Filterable {

    private List<ToasterWebsiteModel> websiteList;
    private BaseService svc;

    public AddWebsiteAdapter(Context context, int resource, List<ToasterWebsiteModel> list) {
        super(context, resource);
        websiteList = list;
        svc = new BaseService(context);
    }

    @Override
    public int getCount() {
        return websiteList.size();
    }

    @Override
    public ToasterWebsiteModel getItem(int position) {
        return websiteList.get(position);
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null){
                    try {
                        String websiteKeyword = constraint.toString();

                        Call<List<ToasterWebsiteModel>> call = svc.addWebsite().addWebsite(websiteKeyword, 20);
                        call.enqueue(new Callback<List<ToasterWebsiteModel>>() {
                            @Override
                            public void onResponse(Call<List<ToasterWebsiteModel>> call, Response<List<ToasterWebsiteModel>> response) {
                                for (ToasterWebsiteModel toasterWebsiteModel : response.body()){
                                    toasterWebsiteModel.getWebUri();
                                    toasterWebsiteModel.getSubscriber();

                                    websiteList.add(toasterWebsiteModel);
                                }
                            }

                            @Override
                            public void onFailure(Call<List<ToasterWebsiteModel>> call, Throwable t) {

                            }
                        });
                    } catch (Exception e){
                        Log.d("HUS","EXCEPTION "+e);
                    }
                    filterResults.values = websiteList;
                    filterResults.count = websiteList.size();
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null && results.count > 0){
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };

        return filter;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.website_autocomplate, parent, false);

        ToasterWebsiteModel model = websiteList.get(position);

        TextView website = (TextView) view.findViewById(R.id.txtWebsite);
        TextView subscription = (TextView) view.findViewById(R.id.txtSubcription);

        String websiteText = model.getWebUri();
        String websiteSubstring = websiteText.substring(7);

        website.setText(websiteSubstring + " ");
        subscription.setText(model.getSubscriber() + " subscribers");

        return view;
    }

}
Ahmad Dudayef
  • 35
  • 2
  • 9
  • you keep appending results in onResponse , that gives duplicates, you need to recreate a list (or clear old one) before putting new results – Oleg Bogdanov Jan 12 '17 at 07:30
  • sorry can you give me example how to do that? thanks for response – Ahmad Dudayef Jan 12 '17 at 07:37
  • 1
    just try `websiteList.clear()` as a first line of onResponse – Oleg Bogdanov Jan 12 '17 at 07:40
  • thanks you bro... it's work,,, you made ma day :)) – Ahmad Dudayef Jan 12 '17 at 07:42
  • make it simple, like [this](http://stackoverflow.com/a/19860624/2252830), there is no need for any custom adapters with custom filter – pskink Jan 12 '17 at 11:03
  • @pskink yes it's nice too but how to do that with POJO object ? are u have example about that ? – Ahmad Dudayef Jan 12 '17 at 11:48
  • what do you need POJO for? where is it better than a `Cursor` based data model? did you notice how simple it is when you use a well defined generic `Cursor` interface? you just provide the data, you dont need to "fight" with lists, inflating views, custom filters etc – pskink Jan 12 '17 at 11:49
  • @pskink i already try to use it but in example you using json from wikipedia and its different with my json, i have problem implement with my json, may you give me how to do it with my json. Thanks in advance.. btw this my json http://pastebin.com/xEBrNzFg – Ahmad Dudayef Jan 13 '17 at 03:17
  • so you have simple JSONArray of JSONObjects... whats the problem with it? – pskink Jan 13 '17 at 09:30

0 Answers0