如何仅在arrylist中设置搜索值

时间:2018-05-04 07:42:36

标签: android arraylist

当搜索一个值时,获取相同的值设置全部显示,如何做到这一点.. 示例..搜索值为1,然后单击搜索按钮,显示所有值,一,二,三。

UI Design

    cancel = (Button) findViewById(R.id.btncancel);
    search = (Button) findViewById(R.id.btnsearch);
    textView = (TextView) findViewById(R.id.adapterPhone);


    final String[] startplacesearchArrayList = { "one ","two","three" };
    final String[] startplacesearchArrayList1 = { "Cat ","Dog","Cow" };
    final String[] startplacesearchArrayList2 = { "Carrot ","Pottato","cake" };

    final ArrayList<String>f=new ArrayList<String>();
    f.addAll( Arrays.asList(startplacesearchArrayList) );
    f.addAll( Arrays.asList(startplacesearchArrayList1) );
    f.addAll( Arrays.asList(startplacesearchArrayList2) );

    final AutoCompleteDogsAdapter endadapter = new AutoCompleteDogsAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, f);
    start.setAdapter(endadapter);

    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String startval = start.getText().toString();

            if (TextUtils.isEmpty(startval)) {
                Toasty.warning(getApplicationContext(), "Enter The Places").show();
            } else {
                if (f.contains(startval)) {

                    for(int i=0;i<f.size();i++)
                    Toast.makeText(getApplicationContext(),"values is :- "+f.get(i).toString(),Toast.LENGTH_LONG).show();
                } else {

                    Toasty.warning(getApplicationContext(), "Account not founds").show();
                }


            }

        }
    });

1 个答案:

答案 0 :(得分:0)

你不应该在ArrayList.contains("")中继,因为它会检查确切的字符串匹配,所以它不会返回true,当使用手动输入时可能不是这种情况。

应用此代码,

cancel = (Button) findViewById(R.id.btncancel);
search = (Button) findViewById(R.id.btnsearch);
textView = (TextView) findViewById(R.id.adapterPhone);


final String[] startplacesearchArrayList = { "one ","two","three" };
final String[] startplacesearchArrayList1 = { "Cat ","Dog","Cow" };
final String[] startplacesearchArrayList2 = { "Carrot ","Pottato","cake" };

final ArrayList<String>f=new ArrayList<String>();
f.addAll( Arrays.asList(startplacesearchArrayList) );
f.addAll( Arrays.asList(startplacesearchArrayList1) );
f.addAll( Arrays.asList(startplacesearchArrayList2) );

final AutoCompleteDogsAdapter endadapter = new AutoCompleteDogsAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, f);
start.setAdapter(endadapter);

search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String startval = search.getText().toString();

            if (TextUtils.isEmpty(startval)) {
                Toasty.warning(getApplicationContext(), "Enter The Places").show();
            } else {

                boolean isExist = false;
                for (int i = 0; i < f.size(); i++) {

                    if (f.get(i).toLowerCase().equals(startval.toLowerCase())) {
                        isExist = true;
                        String toastMessage = "";

                        switch (i) {
                            case 0:
                            case 1:
                            case 2:
                                toastMessage = getToastMessage(startplacesearchArrayList);
                                break;
                            case 3:
                            case 4:
                            case 5:
                                toastMessage = getToastMessage(startplacesearchArrayList1);
                                break;
                            case 6:
                            case 7:
                            case 8:
                                toastMessage = getToastMessage(startplacesearchArrayList2);
                                break;
                        }
                        Toast.makeText(getApplicationContext(), "values is :- " + toastMessage, Toast.LENGTH_LONG).show();
                        break;
                    }
                }
                if (isExist)
                    Toasty.warning(getApplicationContext(), "Account not founds").show();
            }

        }
    });

getToastMessage()就是这样,

private String getToastMessage(String[] arrayList) {
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < arrayList.length; i++) {
        stringBuilder.append(arrayList[i]);
        if (i != arrayList.length - 1)
            stringBuilder.append(",");
    }
    return stringBuilder.toString();
}