-1

this is my code

how to make filter works?

and this my arrayAdapter


package com.example.android.emanprice;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;


public class WordAdapter extends ArrayAdapter<Word> {
    private ArrayList<Word> wordListFull;
    private ArrayList<Word> words = null;

    private int mColorResourceId;

    public WordAdapter(Activity context, ArrayList<Word> words, int colorResourceId) {
        // Here, we initialize the ArrayAdapter's internal storage for the context and the list.
        // the second argument is used when the ArrayAdapter is populating a single TextView.
        // Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
        // going to use this second argument, so it can be any value. Here, we used 0.

        super(context, 0, words);
        mColorResourceId = colorResourceId;

    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }

        // Get the object located at this position in the list
        Word currentWord = getItem(position);

        //Find the TextView in the list_item.xml layout with the ID version_name
        TextView modelNameTextView = listItemView.findViewById(R.id.modelName_text_view);

        modelNameTextView.setText(currentWord.getModelName());

        TextView modelPriceTextView = listItemView.findViewById(R.id.modelPrice_text_view);

        modelPriceTextView.setText(currentWord.getModelPrice());

        ImageView iconView = listItemView.findViewById(R.id.list_item_image);

        iconView.setImageResource(currentWord.getImageResourceID());

        // Set the theme color for the list item
        View textContainer = listItemView.findViewById(R.id.text_container);
        // Find the color that the resource ID maps to
        int color = ContextCompat.getColor(getContext(), mColorResourceId);

        textContainer.setBackgroundColor(color);


        // Return the whole list item layout (containing 2 TextViews )
        // so that it can be shown in the ListView
        return listItemView;

    }
    // Filter Class
    public void filter(String charText) {
        charText = charText.toUpperCase(Locale.getDefault());
        words.clear();
        if (charText.length() == 0) {
            words.addAll(wordListFull);
        } else {
            for (Word wp : words) {
                if (wp.getModelName().toLowerCase(Locale.getDefault()).contains(charText)) {
                    wordListFull.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }


}

the crush happens when call filter method

    public void filter(String charText) {
        charText = charText.toUpperCase(Locale.getDefault());
        words.clear();
        if (charText.length() == 0) {
            words.addAll(wordListFull);
        } else {
            for (Word wp : words) {
                if 
(wp.getModelName().toLowerCase(Locale.getDefault()).contains(charText)) {
                    wordListFull.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }

pleas help me to fix error

the activity class and arrayList:

package com.example.android.emanprice;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.TextView;

import java.util.Locale;

public class BabyActivity extends AppCompatActivity {




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





         final ArrayList<Word> words = new ArrayList<Word>();
        // Create an array of words
        // Create a list of words

        words.add(new Word("1050", "012 بيبي", R.drawable.ic_012_));
        words.add(new Word("1250", "035بى بى", R.drawable.ic_035_));
        words.add(new Word("1500", "1019 بيبي", R.drawable.ic_1019_));
        words.add(new Word("1500", "1065 بيبي", R.drawable.ic_1065_));
        words.add(new Word("1500", "1089 بيبي", R.drawable.ic_1089_));
        words.add(new Word("1200", "1093 بيبي", R.drawable.ic_1093_));
        words.add(new Word("1500", "1095 بيبي", R.drawable.ic_1095_));
        words.add(new Word("1500", "1098 بيبي", R.drawable.ic_1098_));
        words.add(new Word("1500", "1099 بيبي", R.drawable.ic_1099_));
        words.add(new Word("1500", "1105 بيبي", R.drawable.ic_1105_));
        words.add(new Word("900", "1602 بيبي", R.drawable.ic_1602_));
        words.add(new Word("900", "1775 بيبي", R.drawable.ic_1775_));
        words.add(new Word("900", "1783 بيبي", R.drawable.ic_1783_));
        words.add(new Word("850", "1888 بيبي", R.drawable.ic_1888_));
        words.add(new Word("1400", "2588B بيبي", R.drawable.ic_2588b_));
        words.add(new Word("1400", "2715B بيبي", R.drawable.ic_2715b_));
        words.add(new Word("1400", "2736B بيبي", R.drawable.ic_2736b_));
        words.add(new Word("1500", "2738B بيبي", R.drawable.ic_2738b_));
        words.add(new Word("1400", "2750B بيبي", R.drawable.ic_2750b_));
        words.add(new Word("1100", "3003 بيبي", R.drawable.ic_3003_));
        words.add(new Word("1100", "3005 بيبي", R.drawable.ic_3005_));
        words.add(new Word("1100", "3023 بيبي", R.drawable.ic_3023_));
        words.add(new Word("850", "353 بيبي", R.drawable.ic_353_));
        words.add(new Word("1150", "505356636بيبي", R.drawable.ic_505356636));
        words.add(new Word("1450", "609بي بي", R.drawable.ic_609_));
        words.add(new Word("1500", "80-10 بيبي", R.drawable.ic_80_10_));
        words.add(new Word("1350", "80-11 بيبي", R.drawable.ic_80_11_));
        words.add(new Word("1450", "80-12 بيبي", R.drawable.ic_80_12_));
        words.add(new Word("1450", "80-13 بيبي", R.drawable.ic_80_13_));
        words.add(new Word("1450", "80-14 بيبي", R.drawable.ic_80_14_));
        words.add(new Word("1450", "80-20 بيبي", R.drawable.ic_80_20_));
        words.add(new Word("1200", "80-5 بيبي", R.drawable.ic_80_5_));
        words.add(new Word("1350", "80-7 كنترة بيبي", R.drawable.ic_80_7__));
        words.add(new Word("1400", "80-9 بيبي", R.drawable.ic_80_9_));
        words.add(new Word("1100", "8362007 بيبي", R.drawable.ic_8362007_));
        words.add(new Word("1100", "8371021 بيبي", R.drawable.ic_8371021_));
        words.add(new Word("1600", "8802بيبي", R.drawable.ic_8802_));
        words.add(new Word("1600", "9903بيبي", R.drawable.ic_9903));
        words.add(new Word("1050", "AT3069 بيبي", R.drawable.ic_at3069_));
        words.add(new Word("1250", "AT3120 بيبي", R.drawable.ic_at3120_));
        words.add(new Word("1150", "B001 بيبي", R.drawable.ic_b001_));
        words.add(new Word("1100", "B002 كنترة بيبي", R.drawable.ic_b002__));
        words.add(new Word("950", "B011 بيبي", R.drawable.ic_b011_));
        words.add(new Word("900", "B1625", R.drawable.ic_b1625));
        words.add(new Word("1150", "B67 بيبي", R.drawable.ic_b67_));
        words.add(new Word("1150", "B6715 بيبي", R.drawable.ic_b6715_));
        words.add(new Word("1150", "B6716 كنترة بيبي", R.drawable.ic_b6716__));
        words.add(new Word("950", "B6761 بيبي", R.drawable.ic_b6761_));
        words.add(new Word("1100", "B6926 بيبي", R.drawable.ic_b6926_));
        words.add(new Word("1250", "B7006 بيبي", R.drawable.ic_b7006_));
        words.add(new Word("950", "B82 بيبي", R.drawable.ic_b82_));
        words.add(new Word("1100", "B8712 بيبي", R.drawable.ic_b8712_));
        words.add(new Word("1000", "B9003 كنترة بيبي", R.drawable.ic_b9003__));
        words.add(new Word("1100", "B9015 كنترة بيبي", R.drawable.ic_b9015__));
        words.add(new Word("1100", "B9018 كنترة بيبي", R.drawable.ic_b9018__));
        words.add(new Word("1650", "BB16-1", R.drawable.ic_bb16_1));
        words.add(new Word("1200", "H066 بيبي", R.drawable.ic_h066_));
        words.add(new Word("1300", "KB16-81", R.drawable.ic_kb16_81));
        words.add(new Word("1500", "KB16-98", R.drawable.ic_kb16_98));
        words.add(new Word("1500", "KB18-105", R.drawable.ic_kb18_105));
        words.add(new Word("1170", "KB18-107", R.drawable.ic_kb18_107));
        words.add(new Word("1170", "KB18-108", R.drawable.ic_kb18_108));
        words.add(new Word("1000", "KB18-111", R.drawable.ic_kb18_111));
        words.add(new Word("1000", "KB18-112", R.drawable.ic_kb18_112));
        words.add(new Word("1000", "KB18-113", R.drawable.ic_kb18_113));
        words.add(new Word("1000", "KB18-114", R.drawable.ic_kb18_114));
        words.add(new Word("1000", "KB18-115", R.drawable.ic_kb18_115));
        words.add(new Word("1000", "KB18-115-1", R.drawable.ic_kb18_115_1));
        words.add(new Word("1000", "KB18-116", R.drawable.ic_kb18_116));
        words.add(new Word("1000", "KB18-116-1", R.drawable.ic_kb18_116_1));
        words.add(new Word("1500", "KB18-120", R.drawable.ic_kb18_120));
        words.add(new Word("1250", "L-18 بيبي", R.drawable.ic_l_18_));
        words.add(new Word("1100", "MQ001 بيبي", R.drawable.ic_mq001_));




        // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
        // adapter knows how to create list items for each item in the list.
         final WordAdapter adapter = new WordAdapter(this, words, R.color.category_baby);

        // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
        // There should be a {@link ListView} with the view ID called list, which is declared in the
        // word_list.xml file.
        ListView listView = (ListView) findViewById(R.id.list);

        // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
        // {@link ListView} will display list items for each {@link Word} in the list.
        listView.setAdapter(adapter);
        final EditText  editText= findViewById(R.id.search);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = editText.getText().toString().toUpperCase(Locale.getDefault());
                adapter.filter(text);


            }
        });




    }


}

in Logcat show me this error

java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.ArrayList.clear()' on a null object reference

  • You have to tell us where the error is happening or we can't really help you. – Akaisteph7 Jul 09 '19 at 21:49
  • the error is happening when call words.clear(); in filter function in wordAdapter.class her public void filter(String charText) { charText = charText.toUpperCase(Locale.getDefault()); words.clear(); – Mahmmod Mohmmed Jul 09 '19 at 21:56
  • Ok check my updated answer, consider accepting if it works for you, ask clarification otherwise – Jasurbek Jul 10 '19 at 05:04

1 Answers1

1

You have not initialised your ‘words’ array object. Change the declaration to:

private ArrayList<Word> words = new ArrayList<Word>();

Or you can do a null check.

if (words != null) {
    words.clear();
}
Brandon Stillitano
  • 1,304
  • 8
  • 27