Android搜索过滤器在gridview中无法正常运行?

时间:2014-01-05 07:38:36

标签: android

public class ExploreScreenActivity extends Activity implements OnClickListener,         OnItemClickListener,IServerResponse, TextWatcher{
private ImageButton _refreshBtn=null;
private ImageButton _searchBtn=null;
private Button _cancelBtn=null;
private EditText _searchText=null;
//  private ExploreGridAdapter mAdapter=null;
private TextView _exploreTxt=null;
private LinearLayout _search_all_layout=null;
private ImageButton _allSearchBtn=null;
private ImageButton _brandsSearchBtn=null;
private ImageButton _itemsSearchBtn=null;
private ImageButton _peopleSearchBtn=null;
private SharedPreferences _sPrefs=null;
public ArrayList<Object> _itemList=null;
private ArrayList<Object>_brandlist=null;
private ArrayList<Object>_alluserlist=null;
private ArrayList<Object>_peoplelist=null;

private String type=null;;
private String searchString=null;;
private GridView _explorGridView=null;

//private GridSearchNewAdapter adapter=null;
private GridBrandadapter brandadapter=null;
private GridAllUserAdapter alladapter=null;
private GridItemAdapter itemadapter=null;
private GridUserAdapter useradapter=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_explorscreen);
    _sPrefs=getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
    _itemList = new ArrayList<Object>();
    _brandlist=new ArrayList<Object>();
    _alluserlist=new ArrayList<Object>();
    _peoplelist=new ArrayList<Object>();
    _refreshBtn=(ImageButton)findViewById(R.id.refresh_btn);
    _refreshBtn.setOnClickListener(this);
    _search_all_layout=(LinearLayout)findViewById(R.id.search_all_layout);
    _searchBtn=(ImageButton)findViewById(R.id.search_btn);
    _searchBtn.setOnClickListener(this);
    _searchText=(EditText)findViewById(R.id.search_edtTxt);
    _searchText.setOnClickListener(this);
    _cancelBtn=(Button)findViewById(R.id.cancel_btn);
    _cancelBtn.setOnClickListener(this);
    _explorGridView=(GridView)findViewById(R.id.explor_gridView);
    _exploreTxt=(TextView)findViewById(R.id.explor_textView);
    _allSearchBtn=(ImageButton)findViewById(R.id.all_search_btn);
    _allSearchBtn.setOnClickListener(this);
    _brandsSearchBtn=(ImageButton)findViewById(R.id.brands_search_btn);
    _brandsSearchBtn.setOnClickListener(this);
    _itemsSearchBtn=(ImageButton)findViewById(R.id.items_search_btn);
    _itemsSearchBtn.setOnClickListener(this);
    _peopleSearchBtn=(ImageButton)findViewById(R.id.people_search_btn);
    _peopleSearchBtn.setOnClickListener(this);

    type= _searchText.getText().toString();
    searchString=_searchText.getText().toString();
    getSearch(type,searchString);
    //_explorGridView.setAdapter(mAdapter);
    _explorGridView.setOnItemClickListener(this);
    _searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                Intent intent=new Intent(ExploreScreenActivity.this, FeedSelectedActivity.class);
                TabGroupActivity parentActivity = (TabGroupActivity)getParent();
                parentActivity.startChildActivity("feedselected", intent);

                return true;
            }
            return false;
        }
    });
    _searchText.addTextChangedListener(this);
}



@Override
public void onClick(View v) {
    TabGroupActivity parentActivity=(TabGroupActivity)getParent();
    Intent intent=null;
    switch (v.getId()) {
    case R.id.refresh_btn:
        break;
    case R.id.search_btn:

        _searchText.setText("");
        intent=new Intent(this, FeedSelectedActivity.class);
        parentActivity.startChildActivity("feedselect", intent);
        break;
    case R.id.cancel_btn:
        _exploreTxt.setVisibility(View.VISIBLE);
        _searchBtn.setVisibility(View.VISIBLE);
        _search_all_layout.setVisibility(View.GONE);
        _cancelBtn.setVisibility(View.GONE);
        _searchText.setText("");
        //  _explorGridView.setAdapter(mAdapter);

        break;
    case R.id.search_edtTxt:
        _exploreTxt.setVisibility(View.GONE);
        _searchBtn.setVisibility(View.GONE);
        _cancelBtn.setVisibility(View.VISIBLE);
        _search_all_layout.setVisibility(View.VISIBLE);
        break;
    case R.id.all_search_btn:
        _brandsSearchBtn.setBackgroundResource(R.drawable.brand_deselected);
        _itemsSearchBtn.setBackgroundResource(R.drawable.items_deselected);
        _peopleSearchBtn.setBackgroundResource(R.drawable.people_deselected);
        _allSearchBtn.setBackgroundResource(R.drawable.all_selected);
        type="all";
        getSearch( type, searchString);

        break;
    case R.id.brands_search_btn:
        _brandsSearchBtn.setBackgroundResource(R.drawable.brand_selected);
        _itemsSearchBtn.setBackgroundResource(R.drawable.items_deselected);
        _peopleSearchBtn.setBackgroundResource(R.drawable.people_deselected);
        _allSearchBtn.setBackgroundResource(R.drawable.all_deselected);
        type="brand";
        getSearch(type,searchString);

        break;
    case R.id.items_search_btn:
        _brandsSearchBtn.setBackgroundResource(R.drawable.brand_deselected);
        _itemsSearchBtn.setBackgroundResource(R.drawable.items_selected);
        _peopleSearchBtn.setBackgroundResource(R.drawable.people_deselected);
        _allSearchBtn.setBackgroundResource(R.drawable.all_deselected);
        type="item";
        getSearch(type,searchString);

        break;
    case R.id.people_search_btn:
        _brandsSearchBtn.setBackgroundResource(R.drawable.brand_deselected);
        _itemsSearchBtn.setBackgroundResource(R.drawable.items_deselected);
        _peopleSearchBtn.setBackgroundResource(R.drawable.people_selected);
        _allSearchBtn.setBackgroundResource(R.drawable.all_deselected);
        type="user";
        getSearch(type,searchString);

        break;
    default:
        break;
    }       
}
private void getSearch(String type,String searchString) {
    if (NetworkAvailablity.checkNetworkStatus(ExploreScreenActivity.this)) {
        ArrayList<NameValuePair> requestParaList = new ArrayList<NameValuePair>();
        requestParaList.add(new BasicNameValuePair("type", type));
        requestParaList.add(new BasicNameValuePair("searchstring",searchString));
        /*requestParaList.add(new BasicNameValuePair("accesstoken",Constant.ACCESS_TOCKEN));
        requestParaList.add(new BasicNameValuePair("userid",Constant.USER_ID));
         */
        // CALL WEBSERVICE
        WebServiceCommunicator.getInstance().registerForServerResponse((IServerResponse) ExploreScreenActivity.this);
        WebServiceCommunicator.getInstance().callGetAppWebService(false,
                this,
                WSConstant.getUrl(WSConstant.METHOD_SEARCH),
                ExploreScreenActivity.this, WSConstant.PID_SEARCH, false,
                requestParaList);
    }
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    Intent intent=new Intent(this, FeedSelectedActivity.class);
    TabGroupActivity parentActivity = (TabGroupActivity)getParent();
    parentActivity.startChildActivity("feedselected", intent);
    //startActivity(intent);
}

private Handler _handler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.arg2) {
        case WSConstant.PID_SEARCH:
            getJsonResponse(msg.obj.toString(), msg.arg2);
            break;

        default:
            break;
        }
    }
};
private void getJsonResponse(String response, int arg2) {
    JSONObject jObject=null;
    try {
        jObject = new JSONObject(response);
        JSONArray list = jObject.getJSONArray("posts");

        for (int i = 0; i < list.length(); i++) {
            JSONObject element = list.getJSONObject(i);
            if ( type.equalsIgnoreCase("all")) {
                AllBean bean= new AllBean();
                bean.setid(element.getString("id"));
                bean.setfullname(element.getString("fullname"));
                bean.setusername(element.getString("username"));
                bean.setprofile_photo(element.getString("profile_photo"));
                bean.setprofile_type(element.getString("profile_type"));
                bean.setprofile(element.getString("profile"));
                _alluserlist.add(bean);
            }
            if (_alluserlist.size()!=0) {

                alladapter=new GridAllUserAdapter(ExploreScreenActivity.this, _alluserlist);
                _explorGridView.setAdapter(alladapter);
            }

            else if (type.equalsIgnoreCase("brand")) {
                BrandBean bean= new BrandBean();
                bean.setbrand_id(element.getString("brand_id"));
                bean.setbrand_name(element.getString("brand_name"));
                bean.setbrand_photo(element.getString("brand_photo"));

                _brandlist.add(bean);
            }
            if (_brandlist.size()!=0) {

                brandadapter=new GridBrandadapter(ExploreScreenActivity.this, _brandlist);
                _explorGridView.setAdapter(brandadapter);
            }

            else if ( type.equalsIgnoreCase("item")) {

                ItemBean bean= new ItemBean();
                bean.setitem_id(element.getString("item_id"));
                bean.setitem_name(element.getString("item_name"));
                bean.setitem_photo(element.getString("item_photo"));

                _itemList.add(bean);
            }
            if (_itemList.size()!=0) {
                itemadapter=new GridItemAdapter(ExploreScreenActivity.this, _itemList);
                _explorGridView.setAdapter(itemadapter);
            }
            else if (type.equalsIgnoreCase("user")) {

                UserBean bean= new UserBean();
                bean.setid(element.getString("id"));
                bean.setfullname(element.getString("fullname"));
                bean.setusername(element.getString("username"));
                bean.setprofile_photo(element.getString("profile_photo"));

                _peoplelist.add(bean);
            }
            if (_peoplelist.size()!=0) {

                useradapter=new GridUserAdapter(ExploreScreenActivity.this, _peoplelist);
                _explorGridView.setAdapter(useradapter);
            }

        }
    }
    catch (Exception e) {
        // TODO: handle exception
    }
}

@Override
public void serverResponse(String response, int processid) {
    // TODO Auto-generated method stub
    Message msg = new Message();
    msg.obj = response;
    msg.arg2 = processid;
    _handler.sendMessage(msg);  
}



@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
    searchString=s.toString();

    getSearch(type,searchString);
}



@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub

}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

}

 }

我正在使用searchfilter来搜索gridview中的项目..,我在serchfilter上调用webservice。我正在使用多个适配器用于diffrent2服务。问题是当我搜索任何值时,我得到特定值,但是当我搜索另一个值时,我的gridview不清楚,新值没有显示。我做什么..,请帮助我。,谢谢...

1 个答案:

答案 0 :(得分:1)

// Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        _alluserlist.clear();
        if (charText.length() == 0) {
            _alluserlist.addAll(_alluserlist);
        } else {
            for (Object bean : _alluserlist) {
                if (((AllBean) bean).getusername().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    _alluserlist.add(bean);
                }
            }
        }
        notifyDataSetChanged();

actully我没有添加setfilter方法。通过在我的适配器中使用setfilter方法。代码将正常工作。并在您的活动中调用set filter。