Android Listview隐藏重复条目

时间:2015-09-22 07:53:38

标签: android listview duplicates hide

我有一个具有重复条目的字符串数组,可以考虑A,B,C,D,E,F,F,G,G,G,G,H,H,H,I,I。其他String-array需要重复项才能具有相同的id。但是,我不希望重复项出现在ListView中。我已经尝试过使用hashsets,但我无法完全集成它。你能不能给我一些关于成功的提示?

    public void onCreate(Bundle icicle)
{
    super.onCreate(icicle);
    setContentView(R.layout.activity_search);
    lv1=(ListView)findViewById(R.id.ListView01);
    ed=(EditText)findViewById(R.id.EditText01);
    textSearch = new TextView(this);
    textSearch = (TextView) findViewById(R.id.textSearch);
    textSearch.setText(SearchHeadText);

    lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , searchItems));
    ed.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {

            textlength = ed.getText().length();
            arr_sort.clear();
            for (String searchItem : searchItems) {
                if (textlength <= searchItem.length()) {
                    if (ed.getText().toString().equalsIgnoreCase((String) searchItem.subSequence(0, textlength))) {
                        arr_sort.add(searchItem);
                    }
                }
            }

            lv1.setAdapter(new ArrayAdapter<String>(Rest_Search_Monotherapie.this, android.R.layout.simple_list_item_1, arr_sort));
        }
    });
    lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View v,
                                int position, long id) {
//                String a = parent.getItemAtPosition(position);
//                menuID = (int) id;

            a = (String) parent.getAdapter().getItem(position);
            menuID = CheckID(a);

            Toast.makeText(Rest_Search_Monotherapie.this,  a+" "+menuID, Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(context, Pre_Antistolling_Monotherapie_info.class);
                startActivity(intent);
        }
    });
}

2 个答案:

答案 0 :(得分:2)

我认为您可以使用HashMap容器​​创建自定义适配器。像这样的东西: HashMap<String, List<String>>密钥是单一条目,List<Strings>中包含重复实体。你可以使用公式(排序键数组中的键位置)+ List<String>

中的索引来获取它

答案 1 :(得分:0)

您可以将replaceAll()与正则表达式一起使用!

相关问题