我正在尝试将用户输入从编辑文本获取到列表视图中,我已经看到了这个类似问题的答案,但我无法弄清楚
我试过这个,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 非常陌生