使用EditText进行ListView搜索

时间:2019-01-07 19:00:57

标签: android listview search android-edittext

如何通过edittext在列表视图中进行搜索? 重要的是我的json值,比如说,如果您搜索一个东西并且结果是第五个元素,但您的链接是零个元素,我该如何解决这个问题?通常我使用位置值来均衡我获得的标题和链接值从json开始,例如标题的位置为5,链接的位置为5,但是如果您使用edittext搜索,标题的位置将再次为5,但链接的位置为0。

 public class makale extends AppCompatActivity {

        ListView listviewMakale;
        ArrayList a22 = new ArrayList();
      private  ArrayAdapter adapter;
     private  EditText ara;

        String url = "https://";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.makale);

            ActionBar actionBar = getSupportActionBar();
            actionBar.setIcon(R.mipmap.ic_launcher);
            actionBar.setDisplayShowHomeEnabled(true);

            listviewMakale = (ListView)findViewById(R.id.listviewMakale);

            //Makale Adı için request
            StringRequest request = new StringRequest(url, new Response.Listener<String>() {
                @Override
                public void onResponse(String string) {
                    parseJsonData(string);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(getApplicationContext(), "Hata-Tekrar Deneyiniz!!", Toast.LENGTH_SHORT).show();

                }
            });



            //linkler için request
            StringRequest request2 = new StringRequest(url, new Response.Listener<String>() {
                @Override
                public void onResponse(String string) {
                    parseJsonDataLinks(string);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(getApplicationContext(), "Hata-Tekrar Deneyiniz!!", Toast.LENGTH_SHORT).show();

                }
            });

            //baslık için
            RequestQueue rQueue = Volley.newRequestQueue(makale.this);
            rQueue.add(request);

            //linkler için
            RequestQueue rQueue2 = Volley.newRequestQueue(makale.this);
            rQueue2.add(request2);



        }

        //baslik değerini çekiyoruz
        void parseJsonData(String jsonString) {
            try {
                JSONObject object = new JSONObject(jsonString);
                JSONArray linksArray = object.getJSONArray("makale");
                ArrayList al = new ArrayList();

                for(int i = 0; i < linksArray.length(); ++i) {
                    al.add(linksArray.getJSONObject(i).get("fullText"));
                }

                adapter = new ArrayAdapter(this,R.layout.activity_listview_2, al);
                listviewMakale.setAdapter(adapter);


                EditText ara=(EditText) findViewById(R.id.ara);
               // ara.getBackground().mutate().setColorFilter(getResources().getColor(R.color.colorPrimary2), PorterDuff.Mode.SRC_ATOP);





                listviewMakale.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // Get the selected item text from ListView
                        String selectedItem = (String) parent.getItemAtPosition(position);
                        Toast.makeText(makale.this,selectedItem,Toast.LENGTH_SHORT).show();
                        final   String linkler="https://"+a22.get(position);

                        AlertDialog.Builder builder = new AlertDialog.Builder(makale.this);
                      builder.setMessage("Pdf olarak indirmek istiyor musunuz ?");
                        builder.setNegativeButton("İPTAL", new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int id) {

                                //İptal butonuna basılınca yapılacaklar.Sadece kapanması isteniyorsa boş bırak.

                            }
                        });

                        builder.setPositiveButton("TAMAM", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                //Tamam butonuna basılınca yapılacakları yaz
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setDataAndType(Uri.parse(linkler), "text/html");
                                startActivity(intent);

                            }
                        });


                        builder.show();
                        }
                });//listview click son

                ara.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    makale.this.adapter.getFilter().filter(charSequence);
                    }

                    @Override
                    public void afterTextChanged(Editable editable) {

                    }
                });

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

        }

        //linklere erişiyoruz
            void parseJsonDataLinks (String jsonString2) {
                try {
                    JSONObject object2 = new JSONObject(jsonString2);
                    JSONArray linksArray2 = object2.getJSONArray("makale");


                    for(int j = 0; j < linksArray2.length(); ++j) {
                        a22.add(linksArray2.getJSONObject(j).get("link"));
                    }

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

        }

    }

0 个答案:

没有答案