从json多次结果自动完成?

时间:2017-01-12 07:20:50

标签: android json retrofit retrofit2 autocompletetextview

你好我使用自动完成与json / web服务器的数据有问题,我已经成功获取数据并显示为自动完成但是当我尝试再次输入时我有多种结果。任何人都可以告诉我如何解决这个问题。提前谢谢......

这是我的代码 ===================活动=================

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;
    }

}

0 个答案:

没有答案