无法过滤android中列表视图中的文本

时间:2016-06-09 07:16:50

标签: android

适配器类:

  public class ListViewAdapter extends ArrayAdapter<Datamodel> {
        // Declare Variables
        Context context;
        LayoutInflater inflater;
        List<Datamodel> datamodel;
        ArrayList<Datamodel> productlist;


        public ListViewAdapter(Context context, int resourceId,
                List<Datamodel> worldpopulationlist) {
            super(context, resourceId, worldpopulationlist);

            this.context = context;

            this.datamodel = new ArrayList<Datamodel>();

            this.datamodel.addAll(worldpopulationlist);
            inflater = LayoutInflater.from(context);

            productlist = new ArrayList<Datamodel>();

            productlist.addAll(worldpopulationlist);

        }

        private class ViewHolder {
            TextView name;

            ImageView flowerimage;
            ImageView share;
            ImageView bookmark;

        }

        @Override
        public int getCount() {
            return super.getCount();
        }

        @Override
        public Datamodel getItem(int position) {
            // TODO Auto-generated method stub
            return productlist.get(getCount() - position - 1);
        }

        public View getView(final int position, View view, ViewGroup parent) {
            final ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = inflater.inflate(R.layout.listitem, null);
                //

                holder.name = (TextView) view.findViewById(R.id.name);
                holder.flowerimage = (ImageView) view
                        .findViewById(R.id.flowerimage);

                holder.share = (ImageView) view.findViewById(R.id.share);

                holder.bookmark = (ImageView) view.findViewById(R.id.bookmark);

                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.name.setText(datamodel.get(position).getName());
            Picasso.with(context).load(datamodel.get(position).getImagepath())
                    .into(holder.flowerimage);
            holder.flowerimage.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    imagealert(position);
                }
            });

            // holder.itemnumber.setText("5");
            holder.share.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    sharedata(position);
                }

            });
            holder.bookmark.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                }
            });
            return view;
        }

        public void imagealert(int position) {
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.imagepopulayout);
            // set the custom dialog components - text, image and button
            ImageView image = (ImageView) dialog.findViewById(R.id.imagepopup);

            String imageUrl = datamodel.get(position).getImagepath();
            Picasso.with(context).load(imageUrl).into(image);
            // ImageView image = (ImageView) dialog.findViewById(R.id.image);
            // image.setImageResource(R.drawable.splash_screen_bg);

            dialog.show();
        }

        public void sharedata(int position) {

            String name = datamodel.get(position).getName();
            String image = datamodel.get(position).getImagepath();
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Flower Name:" + name
                    + "Flower Image Path:" + image);
            sendIntent.setType("text/plain");
            context.startActivity(sendIntent);
        }


        public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
            datamodel.clear();
            if (charText.length() == 0) {
                datamodel.addAll(productlist);
            } else {
                for (Datamodel wp : productlist) {
                    if (wp.getName().toLowerCase(Locale.getDefault())
                            .contains(charText)) {
                        datamodel.add(wp);
                    }
                }
            }

Actvity Class:

public class MainActivity extends Activity implements
        android.view.View.OnClickListener {

    ArrayList<Datamodel> list = new ArrayList<Datamodel>();

    ListView listview;

    EditText editText1;

    ImageView search;
    ListViewAdapter adapter;
    public static final String mypreference = "mypref";
    DbHelper dbhelper;
    ImageView imageView2;
    SharedPreferences sharedpreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbhelper = new DbHelper(this);
        listview = (ListView) findViewById(R.id.listView);
        editText1 = (EditText) findViewById(R.id.editText1);
        search = (ImageView) findViewById(R.id.search);
        imageView2 = (ImageView) findViewById(R.id.imageView2);
        imageView2.setOnClickListener(this);
        search.setOnClickListener(this);
        sharedpreferences = getSharedPreferences(mypreference,
                Context.MODE_PRIVATE);
        // dbhelper.insertModfydate("1", "5");

        new a().execute();

         editText1.addTextChangedListener(new TextWatcher() {

         @Override
         public void onTextChanged(CharSequence cs, int arg1, int arg2, int
         arg3) {
         // When user changed the Text
         System.out.println("Text [" + cs + "]");


         }

         @Override
         public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
         int arg3) {
         // TODO Auto-generated method stub

         }



         @Override
         public void afterTextChanged(Editable arg0) {
         // TODO Auto-generated method stub
             String text = editText1.getText().toString().toLowerCase(Locale.getDefault());
                adapter.filter(text);
         }
         });

    }

    class a extends AsyncTask<Void, Void, Void> {

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

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            try {
                String response = JSONfunctions
                        .getJSONfromURL("http://development.easystartup.org/NO/Backend/flower.php");

                JSONObject _jfuncation = new JSONObject(response.toString());

                String data = _jfuncation.getString("data");

                JSONArray _JsonArray = new JSONArray(data);

                for (int i = 0; i < _JsonArray.length(); i++) {

                    Datamodel datamodel = new Datamodel();

                    JSONObject newjsonobject = _JsonArray.getJSONObject(i);
                    String flowername = newjsonobject.getString("name");
                    String flowerimage = newjsonobject.getString("url");

                    datamodel.setName(flowername);
                    datamodel.setImagepath(flowerimage);

                    list.add(datamodel);

                    Log.d("Response", flowername + "" + flowerimage);

                }

            }

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

            return null;
        }

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

            String name = sharedpreferences.getString("Falitervalue", "");

            Toast.makeText(MainActivity.this, name, 1000).show();
            if (name.equals("1")) {

                Collections.sort(list, new Comparator<Datamodel>() {
                    @Override
                    public int compare(Datamodel u1, Datamodel u2) {
                        return u2.name.compareToIgnoreCase(u1.name);
                    }
                });

                adapter = new ListViewAdapter(MainActivity.this,
                        R.layout.listitem, list);

                listview.setAdapter(adapter);
            } else {
                adapter = new ListViewAdapter(MainActivity.this,
                        R.layout.listitem, list);

                listview.setAdapter(adapter);
            }

        }
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if (arg0.getId() == R.id.search) {
//          adapter.getFilter().filter(editText1.getText().toString());




        } else if (arg0.getId() == R.id.imageView2) {

            String name = sharedpreferences.getString("Falitervalue", "");

            if (name.equals("1")) {
                Editor editor = sharedpreferences.edit();
                editor.putString("Falitervalue", "0");

                editor.commit();
                Collections.sort(list, new Comparator<Datamodel>() {
                    @Override
                    public int compare(Datamodel u1, Datamodel u2) {
                        return u1.name.compareToIgnoreCase(u2.name);
                    }
                });

                adapter = new ListViewAdapter(MainActivity.this,
                        R.layout.listitem, list);

                listview.setAdapter(adapter);

            }

            else {
                Editor editor = sharedpreferences.edit();
                editor.putString("Falitervalue", "1");

                editor.commit();

                Collections.sort(list, new Comparator<Datamodel>() {
                    @Override
                    public int compare(Datamodel u1, Datamodel u2) {
                        return u2.name.compareToIgnoreCase(u1.name);
                    }
                });

                adapter = new ListViewAdapter(MainActivity.this,
                        R.layout.listitem, list);

                listview.setAdapter(adapter);

            }

        }
    }

}

                notifyDataSetChanged();
            }

        }

我想在文本的基础上过滤listview我希望过滤列表视图或搜索列表视图名称明智我有写代码但是当我尝试搜索它时会在holder.name.setText(datamodel.get(position).getName());此行提供异常arraylistthrogh索引超出约束异常告诉我在哪里以及做错了什么我希望搜索名称只明智地过滤listview名称。

1 个答案:

答案 0 :(得分:0)

下面:

var requestWeather = {
    method: 'GET',
    url: WEATHER_URL,
    params: {
      id: 2988507, // Change to ID of Paris,
      mode: MODE,
      units: UNITS,
      appid: API_KEY,
      lang: LANG
    }
  };

如果 @Override public int getCount() { return super.getCount(); } 方法使用super.getCount(),则返回getView导致问题,返回用于获取数据的相同数据源计数:

position