0

Having taken a look at this, http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/, I tried to modify it to work with a LinearLayout rather than a ListView.

In my application, I'm not able to use a ListView in this instance (since I'm displaying a rather large list and don't want to make it scrollable - rather it is the entire screen which is scrollable).

Anyway, I'm trying to make it work with a LinearLayout, but I suspect there is a n issue with getFilter() and LinearLayout. Would appreciate some guidance

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- Editext for Search -->
    <EditText android:id="@+id/inputSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Search products.."
        android:inputType="textVisiblePassword"/>

   <LinearLayout
       android:id="@+id/list_view_linear"
       android:orientation="vertical"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">

   </LinearLayout>



</LinearLayout>

MainActivity.java

package com.me.searchfilter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    // List view
    private LinearLayout lv;

    // Listview Adapter
    ArrayAdapter<String> adapter;

    // Search EditText
    EditText inputSearch;


    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Listview Data
        String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                "iPhone 4S", "Samsung Galaxy Note 800",
                "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};

        lv = (LinearLayout) findViewById(R.id.list_view_linear);
        inputSearch = (EditText) findViewById(R.id.inputSearch);

        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);

        for (int i = 0; i < adapter.getCount(); i++) {
            View item = adapter.getView(i, null, null);
            lv.addView(item);
        }


        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                MainActivity.this.adapter.getFilter().filter(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
            }
        });
    }
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- Single ListItem -->

    <!-- Product Name -->
    <TextView android:id="@+id/product_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold"/>

</LinearLayout>

Note: for obvious reasons, this isn't my real application, but a small test app to make a linearlayout work with the filter

aqibjr1
  • 651
  • 5
  • 21
  • There is no link between your LinearLayout and adapter. How will the layout know that a filter has been applied? In case of ListView , you say listView.setAdapter(adapter); But you are not doing that here. – nupadhyaya Jul 31 '16 at 20:48
  • Understood, but this is the way I was adding my items to the linear layout. I believe that a ListView cannot be forced to not scroll (i.e. display all items)? – aqibjr1 Jul 31 '16 at 20:49
  • I think rather than trying something very overcomplicated, I shall figure out a way to use the listview and then instead just use the code provided. – aqibjr1 Jul 31 '16 at 20:57
  • If you don't want to scroll , then using the LinearLayout itself is fine. But to use filter , you will have to recreate the LinearLayout every time onTextChanged instead of calling getFillter.filter() – nupadhyaya Jul 31 '16 at 20:57
  • How could I go about recreating the LinearLayout? – aqibjr1 Jul 31 '16 at 21:04
  • "I'm not able to use a ListView in this instance (since I'm displaying a rather large list and don't want to make it scrollable - rather it is the entire screen which is scrollable)." Why don't you just make the ListView the size of your screen / fragment and make the whole thing scrollable or just disable scrolling (although it could be confusing to users). I am currently working on a app with custom ListView so I could help you out if it is something that could help you. EDIT: Screenshot of my current ListView http://imgur.com/a/MR52i – eeffoc Jul 31 '16 at 23:52
  • Yes, that definitely sounds more reasonable to do than what I originally have attempted to do. – aqibjr1 Aug 01 '16 at 09:36
  • Okay I'll get back to you in ~6h when I'm out of work with some code. – eeffoc Aug 01 '16 at 13:01
  • Appreciated, I'm struggling to find a sound way of displaying the entire list view. – aqibjr1 Aug 01 '16 at 15:41

2 Answers2

0

I went through the linked tutorial and can confirm everything is working. If you could further specify what is not working, what you need help with and why you insist on LinearLayout instead of ListView then I could be of a bigger help. Here are two screenshot to show the tutorial working, I reverted from your LinearLayout to ListView as is shown in tutorial. Let me know what exactly you need help with.

https://i.stack.imgur.com/FwqA9.jpg

If you want to add more information / more text fields to a single list row you will have to use a custom ListAdapter and if you are using a really big data set that you are displaying and the scrolling / loading is slow you should use a RecyclerView.

eeffoc
  • 468
  • 6
  • 19
  • @ak1652 That's good, do you still need help with this question - http://stackoverflow.com/questions/38704968/how-to-make-a-listview-display-all-items-without-scrolliing ? If you could mark this answer as accepted I would appreciate it. – eeffoc Aug 01 '16 at 19:19
  • Nope, got that to work as well. Thank you very much :) – aqibjr1 Aug 01 '16 at 20:13
0

I know I am extremely late to the party, but I had a similar issue which I solved and thought I would share.

Let's say you have an empty LinearLayout with an ID of buttonLayout in your XML, which you would like to add dynamic data to. You have looped a number of buttons into said layout by doing something similar to this:

for (final Object value : myArray)

    try {


        final AppCompatButton myButton = new AppCompatButton();
        myButton.setText(value);
        buttonLayout.addView(myButton);


    } catch (Exception e) {
            Log.d(TAG, "Failed to create new button");
    }

Now you wish to filter the list.

Assuming you already have an EditText in your XML where you can search, lets say you call it et_search_name and have initialised it etc.

You could then do the following, which is filtering based on the name on the button, but this could easily be changed to whatever is needed:

 et_search_name.addTextChangedListener(new TextWatcher() {

   @Override
   public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

   // When user changed the Text
     for(int i = 0; i < buttonLayout.getChildCount(); i++){

        if(!((AppCompatButton)buttonLayout.getChildAt(i)).getText().toString().contains(cs)){

            buttonLayout.getChildAt(i).setVisibility(View.GONE);

            Log.v("Buttons To Disappear", (String)((AppCompatButton)buttonLayout.getChildAt(i)).getText());

       }else{

           buttonLayout.getChildAt(i).setVisibility(View.VISIBLE);

       }
    }
 }
Display name
  • 730
  • 2
  • 8
  • 23