2017-09-15 75 views
0

您好,我有一個搜索視圖來過濾短列表中的結果。我目前使用整數傳遞到下一個活動來加載需要顯示的內容。但是,如果您搜索,則您搜索的項目現在位於第0個位置,即第一個項目所在的位置。那麼,如何爲此設置一個唯一的ID系統,或者我可以爲此調用一個方法嗎?帶有ListView的SearchView更改物品位置,需要其他活動的確切物品ID

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_search_clinics); 
    listView = (ListView) findViewById(R.id.listView); 
    final ArrayList<String> clinicInfoArrayList = new ArrayList<String>(); 
    clinicInfoArrayList.add("Phoenix Urgent Care \n 3931 E Camelback Rd, Phoenix, AZ 85018"); 
    clinicInfoArrayList.add("Pain Stop North Phoenix Clinic \n 3329 East Bell Road, Suite A2-A5, Phoenix, AZ 85032"); 
    clinicInfoArrayList.add("The Little Clinic \n 2727 W Bell Rd, Phoenix, AZ 85053"); 
    clinicInfoArrayList.add("Native Health \n Building C, 4041 N Central Ave, Phoenix, AZ 85012"); 
    clinicInfoArrayList.add("Allied Clinic, PLLC \n 1147 E Glendale Ave, Phoenix, AZ 85020"); 
    clinicInfoArrayList.add("Neighborhood Clinic \n 1929 W Fillmore St, Phoenix, AZ 85009"); 
    clinicInfoArrayList.add("MinuteClinic \n 4742 E Indian School Rd, Phoenix, AZ 85018"); 


    mSearchView = (SearchView) findViewById(R.id.menu_search); 


    listView.setTextFilterEnabled(true); 

    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
      this, 
      android.R.layout.simple_list_item_1, 
      clinicInfoArrayList); 

    listView.setAdapter(arrayAdapter); 


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapter, View v, int position, 
           long arg3) { 



      Intent intent = new Intent(SearchClinics.this,ClinicInfoView.class); 


      int something = (int)arrayAdapter.getItemId(position); 



     intent.putExtra("Another", something); 
     startActivity(intent); 
     } 
    }); 


    } 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.search_menu, menu); 

    SearchManager searchManager = (SearchManager) 
      getSystemService(Context.SEARCH_SERVICE); 
    MenuItem searchMenuItem = menu.findItem(R.id.menu_search); 
    mSearchView = (SearchView) searchMenuItem.getActionView(); 

    mSearchView.setSearchableInfo(searchManager. 
      getSearchableInfo(getComponentName())); 


    mSearchView.setIconifiedByDefault(false); 
    mSearchView.setOnQueryTextListener(this); 
    mSearchView.setSubmitButtonEnabled(true); 
    mSearchView.setQueryHint("Search Here"); 
    return true; 

} 


@Override 
public boolean onQueryTextChange(String newText) { 
    if (TextUtils.isEmpty(newText)) { 
     listView.clearTextFilter(); 
    } else { 
     listView.setFilterText(newText.toString()); 
    } 
    return true; 
} 

@Override 
public boolean onQueryTextSubmit(String query) { 
    return true; 
} 
+0

傳遞你的對象,而不是itemId,我想getItemId()只是返回項目的位置。您可以從列表中獲取對象,'onItemClick'內的'clinicInfoArrayList.get(position)'' –

+0

問題是,position是一個數字,當文本從SearchView中過濾時會發生變化。因此,例如在列表視圖項目編號5中,如果現在搜索的是基於位置值的項目編號0,因爲列表視圖僅顯示搜索到的一個。所以這個位置對這些都沒有幫助,因爲它總是會給出錯誤的。 –

+0

如果診所的名稱是唯一的,循環,檢查名稱匹配,然後你得到的對象。 –

回答

0

我有同樣的問題,我這樣固定的話,請注意我想我的科特林邏輯轉換成Java,糾正我,如果有錯,首先我們需要實現我們的過濾邏輯,檢查onQueryTextChange在執行邏輯

import android.app.SearchManager; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.text.TextUtils; 
    import android.view.Menu; 
    import android.view.MenuInflater; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.widget.AdapterView; 
    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 
    import android.widget.SearchView; 

    import java.util.ArrayList; 

    /** 
    * Created by dr_success on 9/16/2017. 
    */ 

    public class test extends AppCompatActivity{ 

     //filtered list here 
     final ArrayList<String> filteredList = new ArrayList<String>(); 
     final ArrayList<String> clinicInfoArrayList = new ArrayList<String>(); 

     ListView listView; 

     SearchView mSearchView; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_search_clinics); 
      listView = (ListView) findViewById(R.id.listView); 

      clinicInfoArrayList.add("Phoenix Urgent Care \n 3931 E Camelback Rd, Phoenix, AZ 85018"); 
      clinicInfoArrayList.add("Pain Stop North Phoenix Clinic \n 3329 East Bell Road, Suite A2-A5, Phoenix, AZ 85032"); 
      clinicInfoArrayList.add("The Little Clinic \n 2727 W Bell Rd, Phoenix, AZ 85053"); 
      clinicInfoArrayList.add("Native Health \n Building C, 4041 N Central Ave, Phoenix, AZ 85012"); 
      clinicInfoArrayList.add("Allied Clinic, PLLC \n 1147 E Glendale Ave, Phoenix, AZ 85020"); 
      clinicInfoArrayList.add("Neighborhood Clinic \n 1929 W Fillmore St, Phoenix, AZ 85009"); 
      clinicInfoArrayList.add("MinuteClinic \n 4742 E Indian School Rd, Phoenix, AZ 85018"); 


      mSearchView = (SearchView) findViewById(R.id.menu_search); 


      listView.setTextFilterEnabled(true); 

      final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
        this, 
        android.R.layout.simple_list_item_1, 
        clinicInfoArrayList); 

      listView.setAdapter(arrayAdapter); 


      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> adapter, View v, int position, 
             long arg3) { 



        Intent intent = new Intent(SearchClinics.this,ClinicInfoView.class); 


        int itemId = (int)arrayAdapter.getItemId(position); 

        String somethingText; 

        //if the filtered list is not empty, get the item from there 
        if(!filteredList.isEmpty()){ 
         somethingText = filteredList.get(itemId); 
        }else{ 
         somethingText = clinicInfoArrayList.get(itemId); 
        } 

        intent.putExtra("Another", somethingText); 
        startActivity(intent); 
       } 
      }); 


     } 


     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.search_menu, menu); 

      SearchManager searchManager = (SearchManager) 
        getSystemService(Context.SEARCH_SERVICE); 
      MenuItem searchMenuItem = menu.findItem(R.id.menu_search); 
      mSearchView = (SearchView) searchMenuItem.getActionView(); 

      mSearchView.setSearchableInfo(searchManager. 
        getSearchableInfo(getComponentName())); 


      mSearchView.setIconifiedByDefault(false); 
      mSearchView.setOnQueryTextListener(this); 
      mSearchView.setSubmitButtonEnabled(true); 
      mSearchView.setQueryHint("Search Here"); 
      return true; 

     } 


     @Override 
     public boolean onQueryTextChange(String newText) { 

       //clear filtered list, so to avoid problems 
       filteredList.clear() 

      if (TextUtils.isEmpty(newText)) { 
    ; 

       return false; 
      } 

      for(int i=0;i <= clinicInfoArrayList.size();i++){ 

       String clinicInfoText = clinicInfoArrayList.get(i); 

       if(clinicInfoText.contains(clinicInfoText)){ 
        filteredList.add(clinicInfoText); 
       } 


       final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
         this, 
         android.R.layout.simple_list_item_1, 
         clinicInfoArrayList); 

       //reset the adapter 
       listView.setAdapter(arrayAdapter); 
       listView.notify(); 
      } 



      return true 
     } 

     @Override 
     public boolean onQueryTextSubmit(String query) { 
      return true; 
     } 
    } 
相關問題