0

How to refill a ListView in android

    listview = (ListView) findViewById(R.id.listView1);
    listview.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, Names));

    final String[] TempArray = new String[Names.length];

    editText = (EditText) findViewById(R.id.editText1);
    editText.addTextChangedListener(new TextWatcher()
    {

        public void onTextChanged(CharSequence s, int start, int before,
                int count)
        {
            // TODO Auto-generated method 
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after)
        {
            // TODO Auto-generated method stub
        }

        public void afterTextChanged(Editable s)
        {
            // TODO Auto-generated method stub

        }
    });

That was my sample code. I want to refill the listview on public void onTextChanged(CharSequence s, int start, int int count) function.on onTextChanged function, i want to fill the listview with a new string array. how can i do this???

  • what do you mean by refill? is it filtering the list item? – Aprian Oct 04 '12 at 08:41
  • on onTextChanged function, i want to fill the listview with a new string array – Dawood Abbasi Oct 05 '12 at 08:40
  • that's not so complicated, you have a problem, you must have tried something already, and despite your best efforts you can't get it to work. So, what have you tried? – njzk2 Oct 08 '12 at 07:30

5 Answers5

0

It seems you are filling the adapter with "Names" values, What you can do is onTextChanged clear the old data from "Names" and add the new data and set the Adapter.

Rajeev N B
  • 1,365
  • 2
  • 12
  • 24
0

First you need to get your adapter into a variable:

ArrayAdapter mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Names);
listview.setAdapter(mAdapter);

Then, when you need to refill you list (e.g. when "Names" changed) just call

mAdapter.notifyDataSetChanged();
Thommy
  • 5,070
  • 2
  • 28
  • 51
0

what you need is just put notifyDataSetChanged(); method in your onTextChanged method.

Like :

editText.addTextChangedListener(new TextWatcher()
{
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
        // TODO Auto-generated method 
        notifyDataSetChanged();
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {
        // TODO Auto-generated method stub
    }

    public void afterTextChanged(Editable s)
    {
        // TODO Auto-generated method stub
    }
});

Hope this help.. enjoy..

Agilanbu
  • 2,747
  • 2
  • 28
  • 33
Piyush
  • 2,589
  • 6
  • 38
  • 77
0

You can't call notifyDataSetChanged() inside any of TextWatcher methods call which will throws RecyclerView notifyDatasetChange() illegalstateexception

What I did to achieve auto-add new row inside RecyclerView list is by implementing OnFocusChangeListener interface which will auto-add once editText gain user focused and also able to avoid above error.

editText.setOnFocusChangeListener(new View.OnFocusChangeListener {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus)
                // ADD NEW ITEM INTO LIST
                notifyDataSetChanged();
        }
    })
Community
  • 1
  • 1
Htet Will
  • 21
  • 1
  • 5
0

you can use the code bellow to recreate your list adapter:

editText.addTextChangedListener(new TextWatcher()
{

    public void onTextChanged(CharSequence s, int start, int before,
            int count)
    {
        // TODO Auto-generated method 
        Names = new ArrayList<~>();
        Names.add('your object');
        listview.setAdapter(new ArrayAdapter<String>(this,
                             android.R.layout.simple_list_item_1, Names));
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after)
    {
        // TODO Auto-generated method stub
    }

    public void afterTextChanged(Editable s)
    {
        // TODO Auto-generated method stub

    }
});

and if you just want to add new data to your 'Names' arrayList you could use code bellow:

 editText.addTextChangedListener(new TextWatcher()
{

    public void onTextChanged(CharSequence s, int start, int before,
            int count)
    {
        // TODO Auto-generated method 
        Names.add('your object');
        listview.getAdapter().notifyDataSetChanged();
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after)
    {
        // TODO Auto-generated method stub
    }

    public void afterTextChanged(Editable s)
    {
        // TODO Auto-generated method stub

    }
});
pouyan
  • 3,445
  • 4
  • 26
  • 44