Android Stundio使用列表视图向Fragment添加搜索功能

时间:2017-06-12 03:52:01

标签: android listview android-fragments search

我在标签式活动中的片段中有一个列表视图。我的列表视图有图片和文字。不幸的是,我不知道如何将搜索功能添加到片段中。搜索功能必须显示图片。我希望有人可以帮助我。如果有人不理解我的问题,请索取母马信息

public class Fragment_1 extends Fragment {

    String text ;
    int zähler = 0;
    String teile[];
    String inTitel ="" ,inBeschreibung ="",inTitel2 ="" ,inBeschreibung2 ="",in = "", in2 = "",  in3 = "",in4 = "" ,in5 = "", in6 = "", in7 = "", in8 = "",in9 = "", in10 = "", in11 = "", in12 = "", in13 = "",in14 = "", in15 = "", in16 = "", in17= "", in18 = "",in19 = "", in20 = "", in21 = "", in22 = "", in23 = "", in24= "";
    ListView listView;
    String [] liste;
    String value = "MIT";
    int check= 0;
    String titles [];
   // String in2 [];
    String description [];



    String[] kontrolle;
    int imgss[], imgss2[];
    int[] imgs ={};



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {



        View view = inflater.inflate(R.layout.fragment_1, container, false);

        listView = (ListView) view.findViewById(R.id.list1);
       // final Button but = (Button) view.findViewById(R.id.but1) ;





        new doit().execute();





        return view;
    }



    public class doit extends AsyncTask<Object, Object, String> {
        String words = "";
        @Override
        protected String doInBackground(Object... params) {

            try {
                Document doc = Jsoup.connect("http://vertretung.esy.es/gust.txt").get();
                words = doc.text();
                text = words;

                teile = words.split("\\s");
                //Log.d("MainActivity" ,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+text);






            } catch (IOException e) {
                e.printStackTrace();
            }

            return words;
        }

        @Override
        protected void onPostExecute(String aVoid) {
            super.onPostExecute(aVoid);
            while (zähler < (5)) {   // Weiter machen
                if (zähler == 0) {
                    inTitel = teile[1] + "   " + teile[2] + " ";
                }
                inBeschreibung = teile[0] + " " + teile[3] + " " + teile[4];





               if (inBeschreibung.contains("5a")){
                   imgss2 = new int[]{R.drawable.klasse5a};
               }else if (inBeschreibung.contains("5b")) {
                   imgss2 = new int[]{R.drawable.klasse5b};
               }else if (inBeschreibung.contains("5c")) {
                   imgss2 = new int[]{R.drawable.klasse5c};
               }else if (inBeschreibung.contains("5d")) {
                   imgss2 = new int[]{R.drawable.klasse5d};
               }else if (inBeschreibung.contains("5e")) {
                   imgss2 = new int[]{R.drawable.klasse5e};
               }else if (inBeschreibung.contains("5f")) {
                   imgss2 = new int[]{R.drawable.klasse5f};
               }else if (inBeschreibung.contains("5g")) {
                   imgss2 = new int[]{R.drawable.klasse5g};
                   //5 zu ende
               }else if (inBeschreibung.contains("6a")) {
                   imgss2 = new int[]{R.drawable.klasse6a};
               }else if (inBeschreibung.contains("6b")) {
                   imgss2 = new int[]{R.drawable.klasse6b};
               }else if (inBeschreibung.contains("6c")) {
                   imgss2 = new int[]{R.drawable.klasse6c};
               }else if (inBeschreibung.contains("6d")) {
                   imgss2 = new int[]{R.drawable.klasse6d};
               }else if (inBeschreibung.contains("6e")) {
                   imgss2 = new int[]{R.drawable.klasse6e};
               }else if (inBeschreibung.contains("6f")) {
                   imgss2 = new int[]{R.drawable.klasse6f};
               }else if (inBeschreibung.contains("6g")) {
                   imgss2 = new int[]{R.drawable.klasse6g};
                   //6 zu ende
               }else if (inBeschreibung.contains("7a")) {
                   imgss2 = new int[]{R.drawable.klasse7a};
               }else if (inBeschreibung.contains("7b")) {
                   imgss2 = new int[]{R.drawable.klasse7b};
               }else if (inBeschreibung.contains("7c")) {
                   imgss2 = new int[]{R.drawable.klasse7c};
               }else if (inBeschreibung.contains("7d")) {
                   imgss2 = new int[]{R.drawable.klasse7d};
               }else if (inBeschreibung.contains("7e")) {
                   imgss2 = new int[]{R.drawable.klasse7e};
               }else if (inBeschreibung.contains("7f")) {
                   imgss2 = new int[]{R.drawable.klasse7f};
               }else if (inBeschreibung.contains("7g")) {
                   imgss2 = new int[]{R.drawable.klasse7g};
                   //7 zu ende
               }else if (inBeschreibung.contains("8a")) {
                   imgss2 = new int[]{R.drawable.klasse8a};
               }else if (inBeschreibung.contains("8b")) {
                   imgss2 = new int[]{R.drawable.klasse8b};
               }else if (inBeschreibung.contains("8c")) {
                   imgss2 = new int[]{R.drawable.klasse8c};
               }else if (inBeschreibung.contains("8d")) {
                   imgss2 = new int[]{R.drawable.klasse8d};
               }else if (inBeschreibung.contains("8e")) {
                   imgss2 = new int[]{R.drawable.klasse8e};
               }else if (inBeschreibung.contains("8f")) {
                   imgss2 = new int[]{R.drawable.klasse8f};
               }else if (inBeschreibung.contains("8g")) {
                   imgss2 = new int[]{R.drawable.klasse8g};
                   //8 zu ende
               }else if (inBeschreibung.contains("9a")) {
                   imgss2 = new int[]{R.drawable.klasse9a};
               }else if (inBeschreibung.contains("9b")) {
                   imgss2 = new int[]{R.drawable.klasse9b};
               }else if (inBeschreibung.contains("9c")) {
                   imgss2 = new int[]{R.drawable.klasse9c};
               }else if (inBeschreibung.contains("9d")) {
                   imgss2 = new int[]{R.drawable.klasse9d};
               }else if (inBeschreibung.contains("9e")) {
                   imgss2 = new int[]{R.drawable.klasse9e};
               }else if (inBeschreibung.contains("9f")) {
                   imgss2 = new int[]{R.drawable.klasse9f};
               }else if (inBeschreibung.contains("9g")) {
                   imgss2 = new int[]{R.drawable.klasse9g};
                   //9 zu ende
               }else if (inBeschreibung.contains("10a")) {
                   imgss2 = new int[]{R.drawable.klasse10a};
               }else if (inBeschreibung.contains("10b")) {
                   imgss2 = new int[]{R.drawable.klasse10b};
               }else if (inBeschreibung.contains("10c")) {
                   imgss2 = new int[]{R.drawable.klasse10c};
               }else if (inBeschreibung.contains("10d")) {
                   imgss2 = new int[]{R.drawable.klasse10d};
               }else if (inBeschreibung.contains("10e")) {
                   imgss2 = new int[]{R.drawable.klasse10e};
               }else if (inBeschreibung.contains("10f")) {
                   imgss2 = new int[]{R.drawable.klasse10f};
               }else if (inBeschreibung.contains("10g")) {
                   imgss2 = new int[]{R.drawable.klasse10g};
                   //10 zu ende

                   //kurse müssen noch eingetragen werden!!!


               }
                zähler++;
            }

            inBeschreibung = teile [3] + " " + teile[4];



            //1
            //textView.setText(in);

           if ((zähler - teile.length) < 0) {
               {
                   while (zähler < 10) {
                       in2 = in2 + " " + teile[zähler];


                       zähler++;
                   }


                   inBeschreibung2 = teile[5] + " " + teile[6] + " " + teile[9];

                   inTitel2 = teile[7] + " " + teile[8];


                   if (inBeschreibung2.contains("5a")) {
                       imgss = new int[]{R.drawable.klasse5a};
                   } else if (inBeschreibung2.contains("5b")) {
                       imgss = new int[]{R.drawable.klasse5b};
                   } else if (inBeschreibung2.contains("5c")) {
                       imgss = new int[]{R.drawable.klasse5c};
                   } else if (inBeschreibung2.contains("5d")) {
                       imgss = new int[]{R.drawable.klasse5d};
                   } else if (inBeschreibung2.contains("5e")) {
                       imgss = new int[]{R.drawable.klasse5e};
                   } else if (inBeschreibung2.contains("5f")) {
                       imgss = new int[]{R.drawable.klasse5f};
                   } else if (inBeschreibung2.contains("5g")) {
                       imgss = new int[]{R.drawable.klasse5g};
                       //5 zu ende
                   } else if (inBeschreibung2.contains("6a")) {
                       imgss = new int[]{R.drawable.klasse6a};
                   } else if (inBeschreibung2.contains("6b")) {
                       imgss = new int[]{R.drawable.klasse6b};
                   } else if (inBeschreibung2.contains("6c")) {
                       imgss = new int[]{R.drawable.klasse6c};
                   } else if (inBeschreibung2.contains("6d")) {
                       imgss = new int[]{R.drawable.klasse6d};
                   } else if (inBeschreibung2.contains("6e")) {
                       imgss = new int[]{R.drawable.klasse6e};
                   } else if (inBeschreibung2.contains("6f")) {
                       imgss = new int[]{R.drawable.klasse6f};
                   } else if (inBeschreibung2.contains("6g")) {
                       imgss = new int[]{R.drawable.klasse6g};
                       //6 zu ende
                   } else if (inBeschreibung2.contains("7a")) {
                       imgss = new int[]{R.drawable.klasse7a};
                   } else if (inBeschreibung2.contains("7b")) {
                       imgss = new int[]{R.drawable.klasse7b};
                   } else if (inBeschreibung2.contains("7c")) {
                       imgss = new int[]{R.drawable.klasse7c};
                   } else if (inBeschreibung2.contains("7d")) {
                       imgss = new int[]{R.drawable.klasse7d};
                   } else if (inBeschreibung2.contains("7e")) {
                       imgss = new int[]{R.drawable.klasse7e};
                   } else if (inBeschreibung2.contains("7f")) {
                       imgss = new int[]{R.drawable.klasse7f};
                   } else if (inBeschreibung2.contains("7g")) {
                       imgss = new int[]{R.drawable.klasse7g};
                       //7 zu ende
                   } else if (inBeschreibung2.contains("8a")) {
                       imgss = new int[]{R.drawable.klasse8a};
                   } else if (inBeschreibung2.contains("8b")) {
                       imgss = new int[]{R.drawable.klasse8b};
                   } else if (inBeschreibung2.contains("8c")) {
                       imgss = new int[]{R.drawable.klasse8c};
                   } else if (inBeschreibung2.contains("8d")) {
                       imgss = new int[]{R.drawable.klasse8d};
                   } else if (inBeschreibung2.contains("8e")) {
                       imgss = new int[]{R.drawable.klasse8e};
                   } else if (inBeschreibung2.contains("8f")) {
                       imgss = new int[]{R.drawable.klasse8f};
                   } else if (inBeschreibung2.contains("8g")) {
                       imgss = new int[]{R.drawable.klasse8g};
                       //8 zu ende
                   } else if (inBeschreibung2.contains("9a")) {
                       imgss = new int[]{R.drawable.klasse9a};
                   } else if (inBeschreibung2.contains("9b")) {
                       imgss = new int[]{R.drawable.klasse9b};
                   } else if (inBeschreibung2.contains("9c")) {
                       imgss = new int[]{R.drawable.klasse9c};
                   } else if (inBeschreibung2.contains("9d")) {
                       imgss = new int[]{R.drawable.klasse9d};
                   } else if (inBeschreibung2.contains("9e")) {
                       imgss = new int[]{R.drawable.klasse9e};
                   } else if (inBeschreibung2.contains("9f")) {
                       imgss = new int[]{R.drawable.klasse9f};
                   } else if (inBeschreibung2.contains("9g")) {
                       imgss = new int[]{R.drawable.klasse9g};
                       //9 zu ende
                   } else if (inBeschreibung2.contains("10a")) {
                       imgss = new int[]{R.drawable.klasse10a};
                   } else if (inBeschreibung2.contains("10b")) {
                       imgss = new int[]{R.drawable.klasse10b};
                   } else if (inBeschreibung2.contains("10c")) {
                       imgss = new int[]{R.drawable.klasse10c};
                   } else if (inBeschreibung2.contains("10d")) {
                       imgss = new int[]{R.drawable.klasse10d};
                   } else if (inBeschreibung2.contains("10e")) {
                       imgss = new int[]{R.drawable.klasse10e};
                   } else if (inBeschreibung2.contains("10f")) {
                       imgss = new int[]{R.drawable.klasse10f};
                   } else if (inBeschreibung2.contains("10g")) {
                       imgss = new int[]{R.drawable.klasse10g};
                       //10 zu ende
                   }
                   inBeschreibung2 = teile[6] + " " + teile[9];

                  MyAdapter adapter = new MyAdapter(getActivity(),titles,imgs,description);
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity().getApplicationContext(),Integer.toString(position),Toast.LENGTH_LONG).show();
            }
        });
               }
               class MyAdapter extends ArrayAdapter<String> {

        Context context;            //Haben wir nicht
        String myTitles[];          //Die Titel die wir geben
        String myDescription[];     //Beaschreibung
        int[] imgs;                // Array für Bilder


        MyAdapter(Context c, String[] titles, int[] img, String[] 
         description) {
            super(c,R.layout.row,R.id.text1,titles);
            this.context=c;
            this.imgs=img;
            this.myTitles=titles;
            this.myDescription=description;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater layoutInflater = (LayoutInflater)   getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);  //GetActivity für was anderes ausgetauscht
            View row = layoutInflater.inflate(R.layout.row, parent, false);
            ImageView images = (ImageView) row.findViewById(R.id.logo);
            TextView myTitle = (TextView) row.findViewById(R.id.text1);
            TextView myDescription = (TextView) row.findViewById(R.id.text2);
            //images.setImageResource(R.drawable.facebook);
            images.setImageResource(imgs[position]);        //Von Position auf 0 gesetzt
            myTitle.setText(titles[position]);
            myDescription.setText(description[position]);
            return row;
        }
    }





}
           }

1 个答案:

答案 0 :(得分:0)

XML中创建res/menu/menu.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:id="@+id/search_menu"
        android:title=""
        android:icon="@mipmap/ic_search"
        app:showAsAction="ifRoom|collapseActionView"/>
</menu>

然后将其添加到您的Fragment

的onCreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
} 

onCreateOptionsMenu

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu, menu);
    MenuItem item = menu.findItem(R.id.search_menu);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
    searchView.setQueryHint("hit text");
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            //define your adapter as global
            adapter.getFilter().filter(newText);

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

    super.onCreateOptionsMenu(menu, inflater);
}

将String数组转换为ArrayList,然后让Adapter实现Filterable

class MyAdapter extends BaseAdapter implements Filterable{}

如果您不知道如何转换,请查看此example

最后根据您的需要覆盖适配器中的getFilter

@Override
public Filter getFilter() {
Filter filter = new Filter() {
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
    }
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        // Implement your logic here
        return results;
    }

  };
   return filter;
}