【问题标题】:Making ListView clickable after being filtered by TextWatcher使 ListView 被 TextWatcher 过滤后可点击
【发布时间】:2017-02-24 19:03:54
【问题描述】:

我从 SQLite 数据库中提取数据并将其存储在 arrayList 中,以通过 listView 显示有关联系人的信息。然后,用户可以输入 EditText,TextWatcher 会过滤 listView。但是,当发生这种情况时,列表视图项的位置不会在我的 onClickListener 中更新。

我发送给我的 CRUD 活动的 ID 始终是它们被过滤后的位置,而不是它们最初列出时的位置。我怎样才能让我的 ListView 项目在被过滤后保持原来的位置?或者是一种更好的方法来为我的 CRUD 活动提供正确的 ID,而不是通过位置。

    public class Search extends AppCompatActivity {

    // List view
    private ListView lv;


 // Listview Adapter
    ArrayAdapter<String> adapter;

    // Search EditText
    EditText inputSearch;


    // ArrayList for Listview
    ArrayList<String> arrayList = new ArrayList<>();

    DBHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        db = new DBHelper(this);

        arrayList = db.getAllContacts();

        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);

        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
        lv.setAdapter(adapter);


        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                Search.this.adapter.getFilter().filter(cs);

        }


            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                // TODO Auto-generated method stub



            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                adapter.notifyDataSetChanged();
            }
        });

     lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {


                        int id_To_Search = pos + 1;

                        Bundle dataBundle = new Bundle();
                        dataBundle.putInt(EXTRA_FLAG, id_To_Search);
                        // The flag value > 0 is used to signal that the upcoming operation
                        // is not adding, but updating or deleting

                        Intent intent = new Intent(getApplicationContext(), CRUDActivity.class);
                        intent.putExtras(dataBundle);  //Intent.putExtras(Bundle extras
                        startActivity(intent);
                    }
                });

    }

【问题讨论】:

  • 在 onTextChange 中设置 onItemClickListener 没有意义。位置不是id。您只能使用常用方法从 onItemClick 中的 ArrayList 获取文本来获取“点击”元素(不,它不是 arrayList.get(pos) 因为 pos 指向新的过滤列表,常用方法是使用 onItemClick 的父参数 -通过互联网搜索很容易找到)
  • @Slevin 谢谢!
  • 为了什么?这只是一些提示......
  • @Selvin 总比没有好。我知道位置不是 ID 我正在寻找一种方法让未过滤的列表视图记住项目在被过滤之前的位置。然后让过滤后的项目在过滤后将其原始位置传递给我的 CRUD 活动
  • parent.getItemAtPosition(pos) 转换为字符串。在原始 ArrayList 中查找此字符串的索引...但是有更好的方法

标签: android sqlite listview android-adapter android-textwatcher


【解决方案1】:

我最终只创建了第二个数组,然后将其拆分,将其与所选项目进行比较,然后将 id 从 String 解析为 int。之后,我将 id 发送到我的 Crud 活动,以便它可以填充搜索到的联系人。

lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {

            String split = (String) parent.getItemAtPosition(pos);

            // Split this into sections then compare the name with arrayList2, then take the ID from arratList2
            // parse it to an int and set it as the ID to search
            int id_To_Search = 0;
            String array[] = split.split(" ");


            for ( int i = 0; i < arrayList2.size(); i++) {
                String split2[] =  arrayList2.get(i).split(" ");

                if (split2[1].equals(array[0])) {
                    //print item the same way as you did
                   id_To_Search = Integer.parseInt(split2[0]);
                }
            }


            Bundle dataBundle = new Bundle();
            dataBundle.putInt(EXTRA_FLAG, id_To_Search);
            // The flag value > 0 is used to signal that the upcoming operation
            // is not adding, but updating or deleting

            Intent intent = new Intent(getApplicationContext(), CRUDActivity.class);
            intent.putExtras(dataBundle);  //Intent.putExtras(Bundle extras
            startActivity(intent);
        }
    });
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多