SearchView不执行其功能,如何过滤此自定义适配器?

时间:2015-09-15 22:15:11

标签: android-arrayadapter searchview custom-adapter

我创建了一个自定义适配器,它扩展了包含Listview的ArrayAdapter,并在一行中包含两个TextView。我在ActionBar.In onViewTextChange(String s)上有SearchView我已经尝试了一些我找到的方法,但是结果不正确,最后我只有ListView的第一行。如何使SearchView正常工作? 其他一切正常。这个应用程序适用于minSdkVersion =" 9"以上。任何建议都可以。问候。

import android.app.SearchManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ListView;
import android.widget.TextView;

    public class A extends ActionBarActivity{

    ListView list;
    String[] titl;
    String[] opis;
    SearchView searchView;
    VjuAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity);

        ActionBar aB = getSupportActionBar();
        aB.setDisplayHomeAsUpEnabled(true);


        Resources res=getResources();
        titl=res.getStringArray(R.array.naslov);
        opis=res.getStringArray(R.array.podnaslov);

        list=(ListView) findViewById(R.id.listView);
        VjuAdapter adapter=new VjuAdapter(this, titl, opis);
        adapter.getFilter().filter(null);
        list.setAdapter(adapter);
        list.setTextFilterEnabled(true);

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();            

    }


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        finish();
    }       


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub       
         MenuInflater men = getMenuInflater();
         men.inflate(R.menu.main, menu);    

         SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

         MenuItem searchItem = menu.findItem(R.id.trazi);
         ComponentName cn = new ComponentName(this, A.class);

         searchView = (SearchView) MenuItemCompat.getActionView(searchItem);     
         searchView.setSearchableInfo(searchManager.getSearchableInfo(cn)); 

         //searchView.setOnQueryTextListener(this);


         searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String arg0) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                // Here i have tried some methods which i have found
                // this works but result is not correct, in the end i have
                // only first row from ListView

                /*if (TextUtils.isEmpty(s)) {
                ((LayoutInflater) list.getAdapter()).getFilter().filter(s);
            } else {
                adapter.getFilter().filter(s.toString());
            }
            return true;

        }
    });*/
                //s.toLowerCase(Locale.getDefault());
                VjuAdapter vd = (VjuAdapter)list.getAdapter();
                Ll.m("POCETAK VD "+ vd);
                Filter filter = vd.getFilter();
                Ll.m("SRED VD.GET "+ vd.getFilter());
                filter.filter(s);
                Ll.m("KRAJ FILTER "+ filter);


                /*if (TextUtils.isEmpty(s)) {
                    list.clearTextFilter();
                } else {
                    //adapter.setSelectionAfterHeaderView();
                    adapter.getFilter().filter(s.toString());
                }*/



                return true;

            }

        });


         return super.onCreateOptionsMenu(menu);
    }



    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub      
        int id = item.getItemId();
        /*if (id == R.id.trazi){
            onSearchRequested();
        }*/
        if (id == android.R.id.home){
            onBackPressed();
        }
        if (id == R.id.about){
            Intent i = new Intent("com.kanna.sanjarica.ABOUT");
            startActivity(i);
            }
        if (id == R.id.oceni_apl){
            Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName());
            Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(goToMarket);
        }
        if (id == R.id.kontakt){
            String mailTo="kanjah77@gmail.com";
            Intent email_intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",mailTo, null)); 
            email_intent.putExtra(android.content.Intent.EXTRA_SUBJECT, ""); 
            email_intent.putExtra(android.content.Intent.EXTRA_TEXT,""); 

            startActivity(Intent.createChooser(email_intent, "Pošalji email..."));
        }
        return true;
    }
    }
    class VjuAdapter extends ArrayAdapter<String>{

    Context context;
    String [] titlArray;
    String [] opisArray;
    public VjuAdapter(Context c, String[] naslov, String[] podnaslov ) {
        super(c,R.layout.single_row,R.id.textView1,naslov);
        this.context=c;
        this.titlArray=naslov;
        this.opisArray=podnaslov;
    }
    class MyViewHolder{
        TextView textVel;
        TextView textMal;

        MyViewHolder(View v){
            textVel=(TextView) v.findViewById(R.id.textView1);
            textMal=(TextView) v.findViewById(R.id.textView2);
        }       
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row=convertView;
        MyViewHolder holder=null;
        if(row==null){      
        LayoutInflater inf=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=inf.inflate(R.layout.single_row, parent, false);
        holder=new MyViewHolder(row);
        row.setTag(holder);

        }else{
            holder=(MyViewHolder) row.getTag();
        }

        holder.textVel.setText(titlArray[position]);
        holder.textMal.setText(opisArray[position]);


        return row;
    }   


    }

1 个答案:

答案 0 :(得分:0)

这是http://developer.android.com/

  

由任意数组支持的具体BaseAdapter   对象。默认情况下,此类需要提供的资源 id   引用单个TextView 。如果你想使用更复杂的   layout,使用也带有字段id的构造函数。该字段ID   应该在较大的布局资源中引用TextView。

你有2个TextView,构造函数类是这样的:

  

ArrayAdapter(Context context,int resource,int textViewResourceId)