在android中重新填充ListView

时间:2012-10-04 08:39:58

标签: android listview android-listview

如何在android中重新填充ListView

    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

        }
    });

这是我的示例代码。我想在public void onTextChanged(CharSequence s,int start,int int count) function.on onTextChanged函数上重新填充listview,我想用新的字符串数组填充listview。我怎么能这样做?

5 个答案:

答案 0 :(得分:0)

您似乎正在使用“名称”值填充适配器,您可以做的是onTextChanged清除“名称”中的旧数据并添加新数据并设置适配器。

答案 1 :(得分:0)

首先,您需要将适配器放入变量:

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

然后,当您需要重新填写列表时(例如,当“姓名”更改时),请致电

mAdapter.notifyDataSetChanged();

答案 2 :(得分:0)

您需要的只是将notifyDataSetChanged();方法放在onTextChanged方法中。

赞:

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
    }
});

希望这有帮助..享受..

答案 3 :(得分:0)

你不能在任何TextWatcher方法调用中调用notifyDataSetChanged(),这将调用RecyclerView notifyDatasetChange() illegalstateexception

我在RecyclerView列表中实现自动添加新行的方法是实现OnFocusChangeListener接口,一旦editText获得用户关注,它将自动添加,并且还能够避免上述错误。

editText.setOnFocusChangeListener(new View.OnFocusChangeListener {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus)
                // ADD NEW ITEM INTO LIST
                notifyDataSetChanged();
        }
    })

答案 4 :(得分:0)

您可以使用下面的代码重新创建列表适配器:

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

    }
});

如果你只是想在你的'Names'arrayList中添加新数据,你可以使用以下代码:

 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

    }
});