0

I am trying to perform a search in a listview. Listview gets all info from db and displays them. The problem is that ArrayAdapter of search has a conflict with ListAdapter of listview. Because search doesn't work if it's performed before ListAdapter then if I place it below ListAdapter search works but on the other page arne't displayed other informations.

 private void updateList() {
        ListView lv = getListView();
        Set<String> unionSet = new HashSet<String>();
        for (HashMap<String, String> hashMap : mPlatesList) {
            for(String key : hashMap.keySet())
                if(key.equals(TAG_TARGA))
                    unionSet.add(hashMap.get(key));
        }

        String[] table = unionSet.toArray(new String[unionSet.size()]);
        inputSearch = (EditText) findViewById (R.id.searchText);
        myAdapter = new ArrayAdapter<String>(this, R.layout.plates_search, R.id.plates_search, table);
        lv.setAdapter(myAdapter);

        inputSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                Lista.this.myAdapter.getFilter().filter(cs);
            }
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
            }
            @Override
            public void afterTextChanged(Editable arg0) {
            }
        });

        ListAdapter adapter = new SimpleAdapter(this, mPlatesList,
                R.layout.plates, new String[] { TAG_TARGA, TAG_NGJYRA, TAG_MARKA, TAG_SIGURACION, TAG_GJOBA, TAG_PRONAR, TAG_SGS },
                new int[] { R.id.title, R.id.ngjyra, R.id.marka, R.id.siguracion, R.id.gjoba, R.id.pronar, R.id.sgs});
        setListAdapter(adapter);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                String targa = ((TextView)view.findViewById(R.id.title)).getText().toString();
                String ngjyra = ((TextView)view.findViewById(R.id.ngjyra)).getText().toString();
                String marka = ((TextView)view.findViewById(R.id.marka)).getText().toString();
                String siguracion = ((TextView)view.findViewById(R.id.siguracion)).getText().toString();
                String gjoba = ((TextView)view.findViewById(R.id.gjoba)).getText().toString();
                String sgs = ((TextView)view.findViewById(R.id.sgs)).getText().toString();
                String pronar = ((TextView)view.findViewById(R.id.pronar)).getText().toString();
                String username = getIntent().getExtras().getString("Username");

                Intent intent = new Intent(Lista.this,Info.class);
                intent.putExtra("Targa",targa);
                intent.putExtra("Ngjyra",ngjyra);
                intent.putExtra("Marka",marka);
                intent.putExtra("Gjoba",gjoba);
                intent.putExtra("Siguracion",siguracion);
                intent.putExtra("Sgs",sgs);
                intent.putExtra("Pronar",pronar);
                intent.putExtra("Username",username);
                startActivity(intent);
            }
        });
    }

The problem is that R.id.title, R.id.ngjyra, R.id.marka, R.id.siguracion, R.id.gjoba, R.id.pronar, R.id.sgs that are displayed on the other page are empty if search is above ListAdapter otherwise info on the other page are displayed but search doesn't works. What to do?

2 Answers2

0

In your OnItemClickListener you should not read from the list but from your data source. You got position passed for that reason, so you should be able to retrieve right, corresponding data from table

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • But from the table I get only "Targa" not "Ngjyra", "Marka" etc... I need those others in order to display on the other page –  Aug 01 '14 at 09:35
  • Then you may rethink if you are using proper Adapter. Apparently `StringAdapter` is too limited. Use `ArrayAdapter` for example and have your data in datasource complete – Marcin Orlowski Aug 01 '14 at 09:37
  • I am using ArryaAdapter what do you mean? –  Aug 01 '14 at 09:56
0

You can refer here for CustomSearch Feature, u need to write CustomAdapter for that -->ReferThis

    public void search() {
        ListView lv = getListView();
        Set<String> unionSet = new HashSet<String>();
        List<Map<String, String>> mPlateClone = new ArrayList<Map<String, String>>(
                mPlatesList);
        for (Map<String, String> hashMap : mPlateClone) {
            for (String key : hashMap.keySet())
                if (key.equals(TAG_TARGA))
                    unionSet.add(hashMap.get(key)); 
        }

        String[] table = unionSet.toArray(new String[unionSet.size()]);
        inputSearch = (EditText) findViewById(R.id.searchText);
        // myAdapter = new ArrayAdapter<String>(this, R.layout.plates,
        // R.id.title,
        // table);
        // lv.setAdapter(myAdapter);

        inputSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2,
                    int arg3) {

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                // // ((SimpleAdapter)
                // //
                // adapter).getFilter().filter(inputSearch.getText().toString().trim());
                ((SimpleAdapter) Lista.this.adapter).getFilter().filter(
                        inputSearch.getText().toString().trim());

            }
        });
    }
Community
  • 1
  • 1
KOTIOS
  • 11,177
  • 3
  • 39
  • 66