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

时间:2012-06-05 14:36:15

标签: android arrays listview arraylist android-arrayadapter

我正在尝试从“编辑文本”到“列表视图”中获取用户输入,我已经看到this类似问题的答案,但我无法弄清楚

我试过这个,没有从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);

还尝试了以下内容

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

非常感谢任何帮助

谢谢

*非常适合Android / Java / SO

3 个答案:

答案 0 :(得分:5)

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

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

然后在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();

      }
    }
});

答案 1 :(得分:0)

尝试将String添加到适配器,而不是添加到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();

      }
    }
});

由于listview知道适配器,适配器必须知道它“适应”listview的内容。一旦你知道这意味着你可以毫无问题地使用适配器;)

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

答案 2 :(得分:0)

您在创建活动时获得了EditText的值,并且它是空的。将声明“input”的行放入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();
    }
});