Briefly about the problem. I decided to try using RecyclerView. A ready-made search solution as a listView is not there, you need to implement everything yourself. So I found a solution, if anyone needs the source below, but there is a problem.

There is a list in it that displays headers; when you click on any item, I switch to the second activation where there is already a title and a full description.

I implemented it like this:

In the MainActivity I load from the database (using the Sugar ORM) headers and id

ArrayList<String> arrTitle = new ArrayList<>(); for(Contact contact:allContacts){ arrTitle.add(contact.title); } ArrayList<String> arrId = new ArrayList<>(); for(Contact contact:allContacts){ long i = contact.getId(); String str = Long.toString(i); arrId.add(str); // or arr.add(contact.name); if it's public } 

Then I transfer them to Adaptar.

 mAdapter = new RecyclerAdapter(arrTitle, arrId); 

in the RecyclerAdapter Adapter I get these values, the title is listed, and id is passed to the second Activiti

  @Override public void onBindViewHolder(ViewHolder holder, int position) { // ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΡŽ id Π² массив idTab = new String[mId.size()]; for (int i = 0; i != mId.size(); i++) { idTab[i] = mId.get(i); } // позиция элСмСнта final int idvadaptere = position; // Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Π½Π° экран Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠΈ, Π² RecyclerView holder.mTextView.setText(mDataset.get(position)); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Context context = v.getContext(); Intent ddddd = new Intent(context, LastActivity.class); ddddd.putExtra("id", idTab[idvadaptere]); context.startActivity(ddddd); } }); } 

In the second Activity LastActivity, I get the id and, based on this id, I am already outputting the values ​​into text fields

  // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ Intent ΠΈΠ·Π²Π»ΠΈΠΊΠ°Π΅ΠΌ ΠΈΠ· Π½Π΅Π³ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Intent intent = getIntent(); // ΠΈΠ·Π²Π»Π΅ΠΊΠ°Π΅ΠΌ ΠΈΠ· Π½Π΅Π³ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ idString = intent.getStringExtra("id"); // ΠΊΠΎΠ½Π²Π΅Ρ€Ρ‚ΠΈΡ€ΡƒΠ΅ΠΌ id Π² число idInt = Integer.parseInt(idString); // Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ тСкстовыС поля Contact title = Contact.findById(Contact.class, idInt); String titleStr = title.title; textView.setText(titleStr); Contact prich = Contact.findById(Contact.class, idInt); String prichStr = prich.prich; textView2.setText(prichStr); 

The problem is that if you enter a title in the search, then in the list the position of this title will no longer be the fifth one, for example, but the first and id will already be different, in the second one you will substitute the values ​​not from the fifth id but from the first

Search source, it works fine, if anyone needs to copy, the only problem is data transfer when you click on the list item

activity_main.xml

 <android.support.v7.widget.Toolbar 

...>

  <android.support.v7.widget.SearchView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/search_view" android:layout_gravity="right" app:theme="@style/Theme.AppCompat.NoActionBar" app:searchIcon="@drawable/ic_search_white_24dp"/> 

MainActivity.java

 private SearchView searchView; 

in onCreate

  searchView = (SearchView) findViewById(R.id.search_view); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String text) { return false; } @Override public boolean onQueryTextChange(String text) { mAdapter.filter(text); return false; } }); 

RecyclerAdapter.java

// headers

 private ArrayList<String> mDataset; private ArrayList<String> mCleanCopyDataset; 

Constructor

  // конструктор public RecyclerAdapter(ArrayList<String> dataset) { mDataset = dataset; mCleanCopyDataset = mDataset; } 

Search

  // Π’ ΠΌΠ΅Ρ‚ΠΎΠ΄Π΅ filter() ΠΌΡ‹ ΠΏΠ΅Ρ€Π΅Π±ΠΈΡ€Π°Π΅ΠΌ всС ΠΏΡƒΠ½ΠΊΡ‚Ρ‹ списка ΠΈ, Ссли ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ ΠΏΡƒΠ½ΠΊΡ‚ содСрТит искомый тСкст, Ρ‚ΠΎ ΠΌΡ‹ добавляСм Π΅Π³ΠΎ Π² Π½ΠΎΠ²Ρ‹ΠΉ список mDataset: public void filter(String charText) { charText = charText.toLowerCase(Locale.getDefault()); mDataset = new ArrayList<String>(); if (charText.length() == 0) { // mCleanCopyDataset Ρƒ нас всСгда содСрТит Π½Π΅ΠΈΠ·ΠΌΠ΅Π½Π΅Π½Π½ΡƒΡŽ ΠΈ Π½Π΅ΠΎΡ‚Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ΠΎΠ²Π°Π½Π½ΡƒΡŽ (ΠΏΠΎΠ»Π½ΡƒΡŽ) копию Π΄Π°Π½Π½Ρ‹Ρ… списка mDataset.addAll(mCleanCopyDataset); } else { for (String item : mCleanCopyDataset) { // ΠΌΡ‹ ΠΏΠ΅Ρ€Π΅Π±ΠΈΡ€Π°Π΅ΠΌ всС ΠΏΡƒΠ½ΠΊΡ‚Ρ‹ списка ΠΈ Ссли ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ ΠΏΡƒΠ½ΠΊΡ‚ содСрТит искомый тСкст, Ρ‚ΠΎ ΠΌΡ‹ добавляСм Π΅Π³ΠΎ Π² Π½ΠΎΠ²Ρ‹ΠΉ список mDataset if (item.toLowerCase(Locale.getDefault()).contains(charText)) { mDataset.add(item); } } } // ΠΌΠ΅Ρ‚ΠΎΠ΄ notifyDataSetChanged() позволяСт ΠΎΠ±Π½ΠΎΠ²ΠΈΡ‚ΡŒ список Π½Π° экранС послС Ρ„ΠΈΠ»ΡŒΡ‚Ρ€Π°Ρ†ΠΈΠΈ notifyDataSetChanged(); } 

enter image description here enter image description here

  • Try replacing ddddd.putExtra("id", idTab[idvadaptere]); on ddddd.putExtra("id", idTab[holder.getAdapterPosition()]); - Yura Ivanov
  • Tried, does not help - Artsait
  • ID - a unique identifier that is strictly tied to each record in the database. It can not change with the change of position. What you use is anything but an ID, where do you get it? I also could not understand why your ID is a string. - pavlofff

0