3

I am making a question that could be like a duplicated question, but i have tried everything and nothing worked for me. I have created a listview witch every item has two views, one textview and one checkbox. Its like a multiselect listview. Every item has a level on its own: easy, normal, hard. When a level is chosen from a dropdown: All, Easy, Normal, Hard.. the list changes, just like a filter system. But when i write listView.getChildAt(i).setVisibility(View.GONE); the content of the row is removed but the space occupied is not released.

Any help?

This is my code:

public class CreatePresentation extends Activity
{
    DatabaseHelper db = new DatabaseHelper(this);
    MyCustomAdapter dataAdapter = null;
    List<Slider> list;
    ListView listView;
    String Text;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_presentation);
        displayListView();
        checkButtonClick();
    }

    private void displayListView()
    {
        list = new ArrayList<Slider>();
        ArrayList<Slider> oldList = db.getAllSliders();

        for (Slider anOldList : oldList) {
            String s = anOldList.toString();
            int in = anOldList.getId();
            String lev = anOldList.getLevel();
            Slider slider = new Slider(in, s, lev, false);
            list.add(slider);
        }
        dataAdapter = new MyCustomAdapter(this, R.layout.list_check_box, list);
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(dataAdapter);
    }

    private class MyCustomAdapter extends ArrayAdapter<Slider> {
        private ArrayList<Slider> list;

        public MyCustomAdapter(Context context, int textViewResourceId, List<Slider> list) {
            super(context, textViewResourceId, list);
            this.list = new ArrayList<Slider>();
            this.list.addAll(list);
        }

        private class ViewHolder {
            TextView text;
            CheckBox checkbox;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            final Slider slider = list.get(position);

            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.list_check_box, null);

                holder = new ViewHolder();

                holder.text = (TextView) convertView.findViewById(R.id.sliderTitle);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox);
                convertView.setTag(holder);

                Spinner dropdown = (Spinner)findViewById(R.id.spinner);
                String[] items = new String[]{"Tutto", "Facile", "Medio", "Difficile"};
                ArrayAdapter<String> adapter = new ArrayAdapter<String (CreatePresentation.this, android.R.layout.simple_spinner_item, items);
                dropdown.setAdapter(adapter);

                Text = dropdown.getSelectedItem().toString();

                holder.checkbox.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        Slider slider = (Slider) cb.getTag();
                        slider.setSelected(cb.isChecked());
                    }
                });

                try {
                    if (list.get(position).getLevel().equals("Facile"))
                        convertView.setBackgroundColor(Color.parseColor("#477C3D"));
                    else if (list.get(position).getLevel().equals("Medio"))
                        convertView.setBackgroundColor(Color.parseColor("#936019"));
                    else if (list.get(position).getLevel().equals("Difficile"))
                        convertView.setBackgroundColor(Color.parseColor("#A02307"));
                } catch (Throwable e) {
                    e.printStackTrace();
                }

                dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, final int position, long id) {
                        Text = parentView.getItemAtPosition(position).toString();
                        try {
                            if (Text.equals("All")){
                                Runnable run = new Runnable(){
                                    public void run(){
                                        for (int i = 0; i < list.size(); i++) {
                                            listView.getChildAt(i).setVisibility(View.VISIBLE);
                                        }
                                    }
                                };
                                runOnUiThread(run);
                            }
                            if (Text.equals("Easy")){
                                Runnable run = new Runnable(){
                                    public void run(){
                                        for (int i = 0; i < list.size(); i++) {
                                            if (list.get(i).getLevel().equals("Easy")) {
                                                listView.getChildAt(i).setVisibility(View.VISIBLE);
                                            }
                                            else {
                                                listView.getChildAt(i).setVisibility(View.GONE);
                                            }
                                        }
                                    }
                                };
                                runOnUiThread(run);
                            }
                            if (Text.equals("Normal")){
                                Runnable run = new Runnable(){
                                    public void run(){
                                        for (int i = 0; i < list.size(); i++) {
                                            if (list.get(i).getLevel().equals("Normal"))
                                                listView.getChildAt(i).setVisibility(View.VISIBLE);
                                            else {
                                                listView.getChildAt(i).setVisibility(View.GONE);
                                            }
                                            dataAdapter.notifyDataSetChanged();
                                        }
                                    }
                                };
                                runOnUiThread(run);
                            }
                            if (Text.equals("Hard")){
                                Runnable run = new Runnable(){
                                    public void run(){
                                        for (int i = 0; i < list.size(); i++) {
                                            if (list.get(i).getLevel().equals("Hard"))
                                                listView.getChildAt(i).setVisibility(View.VISIBLE);
                                            else
                                                listView.getChildAt(i).setVisibility(View.GONE);
                                        }
                                    }
                                };
                                runOnUiThread(run);
                            }
                        } catch (Throwable e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> parentView) {
                    }
                });

                holder.checkbox.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        Slider slider = (Slider) cb.getTag();
                        slider.setSelected(cb.isChecked());
                    }
                });
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.text.setText(slider.getQuestion());
            holder.checkbox.setChecked(slider.isSelected());
            holder.checkbox.setTag(slider);

            return convertView;
        }
    }
Joan
  • 99
  • 2
  • 14
  • 1
    u want to remove complete row from listview? – KOTIOS Aug 01 '14 at 08:41
  • yes, even with the blank space.. – Joan Aug 01 '14 at 08:44
  • 1
    so u need to remove entry from list and do notifyadapter – KOTIOS Aug 01 '14 at 08:46
  • Don't you know how much filters you will have? Because if you do you can create a `LinearLayout` with `vertical` orientation and then just add the `adapter` with an `include`. – luiscosta Aug 01 '14 at 08:47
  • I think after changing anything in the ListView, you should refresh it so that the layout changes apply, here is a question post dealing about it : http://stackoverflow.com/questions/4903758/android-how-to-refresh-listview-contents – Kevin Gilles Aug 01 '14 at 08:48
  • More better solution just create four list with respective data like All, Easy, Normal, Hard and try to change adapter list base on DropDown selection. – Haresh Chhelana Aug 01 '14 at 09:01
  • @adcom not removing, because i need to keep the checkboxes selected. – Joan Aug 01 '14 at 09:05
  • @Akagami there are only 4 options in the filter. Can you explain with an example please? – Joan Aug 01 '14 at 09:06
  • @Joan did below answers help you – KOTIOS Aug 01 '14 at 09:43
  • @adcom i am trying the answers, don't worry, if some answer is the correct one i will check it as correct.. – Joan Aug 01 '14 at 09:46
  • @Joan m not worrying :) if below answers didnt wokred i can put my efforts to look into more details on ur issue – KOTIOS Aug 01 '14 at 09:49
  • @adcom will helps me a lot, i'm trying the answersbut nothing till now..:( – Joan Aug 01 '14 at 09:58

7 Answers7

1

the best way to do this is to remove the item from the list and call dataAdapter.notifyDataSetChanged()

steveen zoleko
  • 395
  • 8
  • 23
1

As you want to remove your view but keep your item inside your List I suggest you to use a different method. When the user choose a value that will cause the item to be hide just set that value to your item and then call

  dataAdapter.notifyDataSetChanged();

Doing this you have to modify the logic inside your getView(), I mean if you find an item that is eligible to get hide instead of return convertView inside the getView() method of your customAdapter just return an empty view, like this you item won't be shown but it will still be in your list;)

axl coder
  • 739
  • 3
  • 19
0

You can use a parent Layout for Yuor item to resolve the issue:

For example:

<LinearLayout ...>

    <!-- Here Your item content starts -->
    <LinearLayout android:id="@+id/content">

    ...

    </LinearLayout>
    <!-- Here Your content ends -->

</LinearLayout>

Java code:

listView.getChildAt(i).getChildAt(0).setVisibility(View.GONE);
Roman Black
  • 3,501
  • 1
  • 22
  • 31
0

just like a filter system. Why "like", why not make it actually filterable? There are two options. Easier one is overriding toString of your slider. Other one is creating custom filter, which wouldn't use object's toString method. However, I don't remember how exactly to do second one, only that it's possible.

Slider:
    String toString(){
        return this.type;
    }
When spinner selection changes:
    adapter.getFilter().filter(selectedDifficulty);

This will automatically display items you want to see.

Marius
  • 810
  • 7
  • 20
0

You shouldnt change visibility of views generated by adapter - they change every time when you scroll listview. Instead you should change the behaviour of underlying adapter.

dominik4142
  • 556
  • 3
  • 14
0

You can try this layout instead of the ListView since there's only 4 filters:

<LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

   <include
        android:id="@+id/filter_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/<your_listview_adapter>"
        android:visibility="visible" />

   <include
        android:id="@+id/filter_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/<your_listview_adapter>"
        android:visibility="visible" />

   <include
        android:id="@+id/filter_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/<your_listview_adapter>"
        android:visibility="visible" />

   <include
        android:id="@+id/filter_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/<your_listview_adapter>"
        android:visibility="visible" />

</LinearLayout>

When you want to hide one of the filters you can just do something like:

findViewById(R.id.filter_4).setVisibility(View.GONE);

EDIT:

For instance, if you want to add information to a TextView inside the first include you just have to call the View like this:

View filter1 = findViewById(R.id.filter_1); 
TextView tv1 = (TextView) filter1.findViewById(R.id.<the_id_of_the_textview);
tv1.setText("StackOverflow filter");
luiscosta
  • 855
  • 1
  • 10
  • 16
0

To prevent add another layout in outside of your layout. You just hide the item's all child views, not item itself.

For example:

 if(i == 1){ // assume we need hide the first item. 
    //item itself
    ViewGroup parent = ((ViewGroup) holder.convertView);
    for (int j = 0; j < parent.getChildCount(); j++) {
        parent.getChildAt(j).setVisibility(View.GONE);
     }
 }

And i have test this code, works fine for me.

Bruce too
  • 21
  • 7