本文介绍了Android ListView适配器检查其是否包含字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我到目前为止对Android编程还是一种新事物,每天都在设法学习一些新东西,但有时我确实会卡住,今天我正在使用带有listview和一个按钮的简单项目来检查它是否包含列表视图中的特定字符串.

Hello i am kind of new to programming in android so far day by day am managing to learn something new but at times i do get stuck , today i am working on a simple project with listview and a button to check if it contains a specific string in the listview.

这是我的代码

private ListView listView1 ;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blockcontact);
        listView1 = (ListView) findViewById( R.id.listView1 );
        Button button6 = (Button) findViewById(R.id.button6);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
        list.add("07474715336");
        list.add("+61470405818");
        list.add("470105848");
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = getApplicationContext();
                String strName = "+61470405818";
                for (int i = 0; i < listView1.getAdapter().getCount(); i++) {
                    if (strName.equals(list)) {

                        CharSequence text = "found";
                        int duration = Toast.LENGTH_SHORT;

                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();

                    } else

                        context = getApplicationContext();
                    CharSequence text = "not found";
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                    {


                    }
                }
            }
        });

 listView1.setAdapter(adapter);

am正在尝试检查此号码是否存在字符串strName ="+61470405818";

am trying to check if this number exist String strName = "+61470405818";

该列表可能包含大量数字,因此我想先遍历该列表,然后使用索引检查该项目是否存在.

the list may contain large numbers so i want to first loop through the list and then check using index if the item exist or not.

推荐答案

您应该将列表中的项目与目标字符串进行比较.

You should compare the items inside the list with your target String.

所以代替:

strName.equals(list);

使用:

strName.equals((String)listView1.getAdapter().getItem(i));

这篇关于Android ListView适配器检查其是否包含字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:41