处理自动完成文本视图时未调用android baseadapter getview

时间:2014-11-28 12:17:11

标签: android

public class AutoSearchListAdapter extends BaseAdapter implements Filterable {
    private Context mContext;
    private List<SuggetionEntity> resultList = new ArrayList<SuggetionEntity>();

    public AutoSearchListAdapter(Context context,
            List<SuggetionEntity> resultList) {
        mContext = context;
        this.resultList = resultList;
    }

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

    @Override
    public SuggetionEntity getItem(int index) {
        return resultList.get(index);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.simple_dropdown_item_2line,
                    parent, false);
        }
        ((TextView) convertView.findViewById(R.id.tvSuggetionHeader))
                .setText(getItem(position).getSuggetion());
        ((TextView) convertView.findViewById(R.id.tvSuggetionFooter))
                .setText(getItem(position).getSuggetionType());
        return convertView;
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    List<SuggetionEntity> suggetions = setSuggetions(constraint
                            .toString());

                    filterResults.values = suggetions;
                    filterResults.count = suggetions.size();
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {
                if (results != null && results.count > 0) {
                    resultList = (List<SuggetionEntity>) results.values;
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };
        return filter;
    }

    DatabaseManager databaseManager = MyApplication
            .getDatabaseManagerInstance();

    private List<SuggetionEntity> setSuggetions(String searchString) {
        List<String> modules = databaseManager.getTransMenuInfo();
        List<SuggetionEntity> suggetions = new ArrayList<SuggetionEntity>();
        for (int i = 0; i < modules.size(); i++) {
            if (isPresentInDatabse(getTabelNameFromModuleId(modules.get(i)),
                    searchString)) {
                SuggetionEntity entity = new SuggetionEntity();
                entity.setSuggetion(searchString
                        + " in "
                        + databaseManager.getTransMenuModuleName(modules.get(i)));
                entity.setSuggetionId(modules.get(i));
                suggetions.add(entity);

            }
        }
        return suggetions;

    }

    private String getTabelNameFromModuleId(String moduleId) {
        String tableName = "";
        if (moduleId.equals(QueueConstant.TOP_UP_PRODUCTS)) {
            tableName = mContext.getResources().getString(
                    R.string.tbl_TopUpProducts);
        } else if (moduleId.equals(QueueConstant.BILL_PAY_PRODUCTS)) {
            tableName = mContext.getResources().getString(
                    R.string.tbl_BillPayProducts);
        } else if (moduleId.equals(QueueConstant.VOUCHER_PRODUCTS)) {
            tableName = mContext.getResources().getString(
                    R.string.tbl_VoucherProducts);
        } else if (moduleId.equals(QueueConstant.WALLET_PRODUCTS)) {
            tableName = mContext.getResources().getString(
                    R.string.tbl_WalletProducts);
        } else if (moduleId.equals(QueueConstant.IR_PRODUCTS)) {
            tableName = mContext.getResources().getString(
                    R.string.tbl_IRProducts);
        }
        return tableName;
    }

    private boolean isPresentInDatabse(String tableName, String searchString) {

        return databaseManager.filterTabelInfo(tableName, searchString);
    }
}
                                           
     // Here is BEAN CLASS
                                       

public class SuggetionEntity {

    private String suggetion = "", suggetionId = "", suggetionType = "";

    public String getSuggetion() {
        return suggetion;
    }

    public void setSuggetion(String suggetion) {
        this.suggetion = suggetion;
    }

    public String getSuggetionId() {
        return suggetionId;
    }

    public void setSuggetionId(String suggetionId) {
        this.suggetionId = suggetionId;
    }

    @Override
    public String toString() {
        return suggetion;
    }

    public String getSuggetionType() {
        return suggetionType;
    }

    public void setSuggetionType(String suggetionType) {
        this.suggetionType = suggetionType;
    }

}

                                           //Here is Activity
                                           
 public class SearchActivity extends ActionBarActivity implements
        OnQueryTextListener {
    private AutoCompleteTextView autoCompleteTextView;
    List<SuggetionEntity> suggetionEntities = new ArrayList<SuggetionEntity>();
    private DatabaseManager databaseManager = MyApplication
            .getDatabaseManagerInstance();

    AutoSearchListAdapter adapter;
    private ExpandableListView lvSearchOperatorList;
    private TextView tvNoSearchProduct;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        setActionBar();
        init();
    }

    private void init() {

        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        adapter = new AutoSearchListAdapter(this, suggetionEntities);

        autoCompleteTextView.setThreshold(1);
        autoCompleteTextView.setAdapter(adapter);
        lvSearchOperatorList = (ExpandableListView) findViewById(R.id.lvSearchOperatorList);
        tvNoSearchProduct = (TextView) findViewById(R.id.tvNoSearchProduct);
        lvSearchOperatorList.setEmptyView(tvNoSearchProduct);
        lvSearchOperatorList.setTextFilterEnabled(true);
        

        autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {

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

                SuggetionEntity suggetionEntity = (SuggetionEntity) parent
                        .getItemAtPosition(position);
                if (QueueConstant.TOP_UP_PRODUCTS.equals(suggetionEntity
                        .getSuggetionId())) {
                    MyApplication.showMsg("TopUp", SearchActivity.this);
                } else if (QueueConstant.BILL_PAY_PRODUCTS
                        .equals(suggetionEntity.getSuggetionId())) {
                    MyApplication.showMsg("Billpay", SearchActivity.this);
                } else if (QueueConstant.VOUCHER_PRODUCTS
                        .equals(suggetionEntity.getSuggetionId())) {
                    MyApplication.showMsg("Voucher", SearchActivity.this);
                }

            }
        });
    }

   

    private void setActionBar() {
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setCustomView(
                R.layout.search_action_bar_auto_complete);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onQueryTextChange(String searchString) {
        adapter.getFilter().filter(searchString);
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String arg0) {
        return false;
    }
}

0 个答案:

没有答案