Android 将编辑文本中的用户输入添加到列表视图中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10899335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:22:06  来源:igfitidea点击:

Adding user input from Edit Text into List View

androidarrayslistviewarraylistandroid-arrayadapter

提问by David Lamborghini Tan

I'm trying to get a user input from Edit Text into List View, I had seen the answer to thissimilar question but I'm unable to figure it out

我正在尝试将用户输入从 Edit Text 获取到 List View,我已经看到了这个类似问题的答案,但我无法弄清楚

I tried this, received no errors from the IDE, but it does not work

我试过了,没有收到来自 IDE 的错误,但它不起作用

public class ListtestActivity extends Activity {
/** Called when the activity is first created. */

Button bt;
EditText et;
TextView tv;
ListView lv;
ArrayAdapter<String> adapter;

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

    bt = (Button) findViewById(R.id.button1);
    et = (EditText) findViewById(R.id.editText1);
    tv = (TextView) findViewById(R.id.textView1);
    lv = (ListView) findViewById(R.id.listView1);
    String input = et.getText().toString();
    String[] values = new String[] {"", input};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);
    lv.setAdapter(adapter);

Tried the following also

也尝试了以下

public class ListtestActivity extends Activity {
ArrayAdapter<String> m_adapter;
ArrayList<String> m_listItems = new ArrayList<String>();
/** Called when the activity is first created. */

Button bt;
EditText et;
TextView tv;
ListView lv;

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

    bt = (Button) findViewById(R.id.button1);
    et = (EditText) findViewById(R.id.editText1);
    tv = (TextView) findViewById(R.id.textView1);
    lv = (ListView) findViewById(R.id.listView1);
    m_adapter = new ArrayAdapter<String>(this, R.layout.main, m_listItems);
    lv.setAdapter(m_adapter);
    final String input = et.getText().toString();

    bt.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            m_listItems.add(new String(input));
            m_adapter.notifyDataSetChanged();
        }
    });

Any help would be greatly appreciated

任何帮助将不胜感激

Thank You

谢谢你

*Very new to Android/Java/SO

*非常新的Android/Java/SO

回答by Samir Mangroliya

in Second code snippet, change row of m_adapter

在第二个代码片段中,更改行 m_adapter

  m_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, m_listItems);

Then add in String in m_listItems

然后在String中加入 m_listItems

bt.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        String input = et.getText().toString();
        if(null!=input&&input.length()>0){     

        m_listItems.add(input);

        m_adapter.notifyDataSetChanged();

      }
    }
});

回答by Thkru

Try to add the String to the adapter instead to the listview:

尝试将字符串添加到适配器而不是列表视图:

m_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, m_listItems);//you can delete your previous initialisation

bt.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        String strAddMe = et.getText().toString();
        if(strAddMe != null)
            m_listItems.add(strAddMe);

        m_adapter.notifyDataSetChanged();

      }
    }
});

As the listview knows about the adapter, the adapter has to know about the content wich it "adapts" to the listview. Once you know what that means you can use adapters without any problems ;)

由于列表视图知道适配器,因此适配器必须知道它“适应”列表视图的内容。一旦您知道这意味着什么,您就可以毫无问题地使用适配器;)

Furthermore you should read the editText as late as you need its input.

此外,您应该在需要输入时再阅读 editText。

回答by Cathal Comerford

You're getting the value of the EditText when you create your Activity, and it's empty. Put the line where you declare "input" into your onClick listener.

创建活动时,您将获得 EditText 的值,但它是空的。将您声明“输入”的行放入您的 onClick 侦听器中。

bt.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        final String input = et.getText().toString();

        m_listItems.add(new String(input));
        m_adapter.notifyDataSetChanged();
    }
});