使用ListView的SearchView更改项目位置,需要其他活动的确切项目ID

时间:2017-09-15 23:47:13

标签: android listview searchview

您好我有一个searchview来过滤短列表中的结果。我目前使用整数传递给下一个活动来加载需要显示的内容。但是,如果搜索,您搜索的项目现在位于第0项,即第一项。那么如何为此设置一个唯一的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;
 }

1 个答案:

答案 0 :(得分:0)

我有同样的问题,我用这种方式修复它,注意我试图将我的kotlin逻辑转换成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;
        }
    }