如何根据edittext中的文本显示listview,并使用可点击的图像按钮来搜索listviews数据?

时间:2014-11-04 19:45:45

标签: android listview

我编写了一个显示ListView的程序。 Activity包含一个带有搜索按钮的EditText和下面的ListView。我已经在string.xml中定义了要在ListView中显示的项目:

<string-array name="Name2">
    <item>Name:Rohit Kumar</item>
    <item>Name:Rohit Kumar</item>
    <item>Name:Rohit Kumar</item>
    <item>Name:Rohit Kumar</item>
    <item>Name:Rohit Kumar</item>
    <item>Name:Rohit Kumar</item>
</string-array>
<string-array name="Name3">
    <item>Name:Arjun Narahari</item>
    <item>Name:Arjun Narahari</item>
    <item>Name:Arjun Narahari</item>
    <item>Name:Arjun Narahari</item>
    <item>Name:Arjun Narahari</item>
    <item>Name:Arjun Narahari</item>
</string-array>

我已经把它放了6次,因为在我的ListView中我想要显示相同的名字6次。

我的程序是做什么的,当我在EditText中写“ar”时,当我在EditText中写“ro”或任何时间字母并点击搜索按钮时,结果显示我已经通过里面的项目我的定制适配器意味着任何情况下只显示Arjun Narahari,如果我传递rohit数组,那么对于任何情况,rohit只显示在我的ListView中。

我希望我的程序要做的是,当我在EditText中编写A或Ar并单击搜索按钮时,我希望ListView中的结果仅从A或Ar开始,当我写R或Ro时只有那些结果应该显示在我的ListView中。 请,需要一些帮助。

将在此处发布我的代码:

Result_Name.java

public class Result_Name extends Activity {

 ImageView iv;
 EditText etSearch;
 private TextView txtNoResult;
 ImageView imgSearch;
 ResultsNameAdapter adapter;

 ListView lv;
 String[] name1, name2; // name3, name4, name5, name6;
 String[] mobile0, mobile1; // mobile2, mobile3, mobile4, mobile5;
 String[] age1, age2; // age3, age4, age5, age6;
 String[] gender;
 String[] diagname;
 String[] year;

 String PassedData;

 int[] images = {
  R.drawable.photo_bg,
  R.drawable.photo_bg,
  R.drawable.photo_bg,
  R.drawable.photo_bg,
  R.drawable.photo_bg,
  R.drawable.photo_bg,
  R.drawable.date,
  R.drawable.b_list,
  R.drawable.v_list,
  R.drawable.g_list
 };

 @Override
 protected void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);
   setContentView(R.layout.result_listr_name);
   iv = (ImageView) findViewById(R.id.imgBackReuslt);
   etSearch = (EditText) findViewById(R.id.edtSearchName);
   imgSearch = (ImageView) findViewById(R.id.imgSearchlist);

   Resources res = getResources();
   name1 = res.getStringArray(R.array.Name1);

   name2 = res.getStringArray(R.array.Name2);

   mobile0 = res.getStringArray(R.array.Mobile0);
   mobile1 = res.getStringArray(R.array.Mobile1);

   age1 = res.getStringArray(R.array.Age1);
   age2 = res.getStringArray(R.array.Age2);

   diagname = res.getStringArray(R.array.DiagnosisName);

   gender = res.getStringArray(R.array.Gender);
   year = res.getStringArray(R.array.year_array);

   etSearch = (EditText) findViewById(R.id.edtSearchName);

   etSearch = (EditText) findViewById(R.id.edtSearchName);

   imgSearch.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
     if (etSearch.getText().toString().trim().length() < 0) {
      lv.setVisibility(View.GONE);

     }
     if (etSearch.getText().toString().trim().length() > 0) {
      lv = (ListView) findViewById(R.id.lstResult);
      adapter = new ResultsNameAdapter(getBaseContext(), name1,
       name2, mobile0, mobile1, age1, age2, images, year,
       diagname, gender);
      lv.setAdapter(adapter);

     }

    }

   });

imgsearch是我用作ClickListener的图像,而etsearch是我的EditText。如果我的EditText为空,则我的ListView不可见,当我的EditText包含长度为&gt;的文本时0,我的ListView可见。这是我想改变我提到的东西的地方。

我希望这些要求是明确的。 欢迎任何建议。

1 个答案:

答案 0 :(得分:1)

  

我希望我的程序要做的是,当我在编辑文本中编写A或Ar并单击搜索按钮时,我希望listview中的结果仅从A或Ar开始,当我写R或Ro时只有那些结果应该显示在我的listview

实现这一目标并提高效率的一个好方法是使用Trie数据结构,你应该研究一下,因为解释它太宽泛了我的答案。

达到简单解决方案的另一种方法是使用String.startsWith方法:

String text = etSearch.getText().toString().trim();
// check for "Rohit Kumar" array
for(String name : name2) {
    String nameWithoutLabel = name.substring(5); // skip the "Name:" part
    if(nameWithoutLabel.startsWith(text)) {
        // you will reach this point only if current nameWithoutLabel starts with the text from your EditText
        // add nameWithoutLabel to your list view or where you need it
    }
}
// same check for name3 which is the "Arjun Narahari" array

您可以检查以某个序列开头的Strings的另一种方法是使用正则表达式:

// ...
if (nameWithoutLabel.matches("(" + text + ").*")) {
// ...

您希望如何使用上述代码取决于ResultsNameAdapter的使用方式以及您希望如何整理数据。

例如,您可以做的是从上面的代码中创建一个方法,该方法在以特定文本开头的数组中查找Strings。类似的东西:

ArrayList<String> getMatches(String text, String[] names) {
    ArrayList<String> list = new ArrayList<String>();
    for(String name : names) {
        String nameWithoutLabel = name.substring(5); // skip the "Name:" part
        if(nameWithoutLabel.startsWith(text)) {
            list.add(nameWithoutLabel);
        }
    }
    return list;
}

然后你可以调用这个方法从你需要的任何数组中找到匹配项并将匹配项存储在ArrayList中:

ArrayList<String> allMatches = new ArrayList<String>();

// ...

String text = etSearch.getText().toString().trim();
allMatches.addAll(getMatches(text, name2)); // add all matches from name2
allMatches.addAll(getMatches(text, name3)); // add all matches from name3
// now ArrayList allMatches contains all matches from arrays name2 and name3

现在,您可以使用ArrayList<String> allMatches填充ListView内的ResultsNameAdapter ...