我想從編輯文本的用戶輸入到列表視圖,我已經看到了答案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
它的工作原理:) 感謝您的幫助薩米爾 –