2014-11-04 37 views
1

我寫了一個程序來顯示一個ListView。該活動包含一個帶有搜索按鈕和下面的ListView的EditText。我已經定義裏面string.xml項目ListView控件裏面顯示:如何顯示一個基於edittext文本內的listview的可點擊圖片按鈕來搜索listviews數據?

<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具有長度> 0的文本時,我的ListView可見。這是我想改變我提到的事情的地方。

我希望這個要求很明確。 歡迎任何建議。

回答

1

我希望我的程序要做的是,當我在我的edittext中編寫A或Ar並單擊搜索按鈕時,我希望列表視圖中的結果僅從A或Ar開始,當我編寫R或羅只有那些結果應我的列表視圖中顯示

爲你實現這一點,並使其有效的方法是使用一個Trie數據結構,你應該考慮的是,因爲解釋這是我的SO過於寬泛的一個好辦法回答。

另一種方式來達到一個簡單的解決辦法是使用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 ...

+0

感謝這麼多先生,它的工作 – 2014-11-05 06:25:09

+0

@arjunnarahari歡迎:) – nem035 2014-11-05 14:23:59

相關問題