0

我从 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);
                    }
                });

    }
4

1 回答 1

0

我最终只创建了第二个数组,然后将其拆分,将其与所选项目进行比较,然后将 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);
        }
    });
    }
于 2017-02-26T15:52:10.010 回答