Listview如何检查是否存在1个或多个字符串

时间:2015-09-04 02:12:00

标签: android listview

这是我正在测试的代码,但没有成功。我正在尝试搜索字符串demooo。在此测试中,demooo可以位于第一个索引或任何位置,因此我尝试遍历listview以找到它。我不确定为什么它不起作用。

addButtonn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //check listview
                Context context = getApplicationContext();
                String strName = "demooo";
                listView = (ListView) findViewById(R.id.listViewfind);
                for (int i = 0; i < listView.getAdapter().getCount(); i++) {
                    if (strName.equals((String) listView.getAdapter().getItem(i).toString())) {
                        CharSequence text = "fffffffffffffffound";
                        int duration = Toast.LENGTH_SHORT;
                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                        return;
                    }else{
                        CharSequence text = "nooooooooooot found";
                        int duration = Toast.LENGTH_SHORT;
                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                        return;
                }
            }
        }
    });

将项目添加到listview:

addButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String strUserName = editText.getText().toString();
                strUserName = editText.getText().toString();
                if (TextUtils.isEmpty(strUserName)) {
                    editText.setError("cannot be emty");
                    return;
                }
                listItems.add(editText.getText().toString());
                adapter.notifyDataSetChanged();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position,
                                    long id) {
                Toast.makeText(threee.this, "Clicked " + position, Toast.LENGTH_LONG)
                        .show();
            }
        });

3 个答案:

答案 0 :(得分:0)

... Ungh

我猜您的工作流程包括从空列表开始,每次用户按下“添加”按钮时添加新项目。无论如何,不​​是在ListView中搜索正确的字符串,而是应该搜索“listItems”对象以查看它是否包含正确的值。

请阅读此处 http://www.tutorialspoint.com/java/util/arraylist_indexof.htm

答案 1 :(得分:0)

我假设你的listItems是一个ArrayList

<script src="a.js"></script>
<script src="b.js"></script>
...

答案 2 :(得分:0)

ListView listView = (ListView) findViewById(R.id.listViewfind);
List<CreateDao> list = new ArrayAdapter<CreateDao>();
MyAdapter adapter = new MyAdapter(this, list);
EditText editText  = (EditText) findViewById(R.id.editText);
final String strName = editText.getText().toString;
listView.setAdapter(adapter);
Button addButtonn = (Button) findViewById(R.id.button);

addButtonn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               CreateDao createdao = new CreateDao();
               if(strName.equals(""){
                   Toast.makeText(sample.this, "data empty", Toast.LENGTH_SHORT).show();
                   createDao.setName(strName);
                   list.addAll(createDao);
                   adapter.notifyDataSetChange();
            }
        }
    });