gpt4 book ai didi

android - 如何将文本从 EditText 显示到 ListView?

转载 作者:搜寻专家 更新时间:2023-11-01 08:06:47 26 4
gpt4 key购买 nike

我是 Android 编程的初学者,希望在用户单击按钮时将用户在 EditText 中输入的文本显示到 ListView 中。我写的代码是:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myListView = (ListView) findViewById(R.id.myListView);
final EditText myEditText = (EditText) findViewById(R.id.myEditText);
final ArrayList<String> todoItems = new ArrayList<String>();
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
myListView.setAdapter(aa);
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener() {
String item;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
item = myEditText.getText().toString();
//Toast.makeText(null, item, Toast.LENGTH_SHORT).show();//Problem here

}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

最佳答案

嗨,asp_NewBee, 您应该将新输入的 EditText 项目添加到您的列表 todoItems 中,如下面的 onclick block 内。从你的代码

  myButton.setOnClickListener(new View.OnClickListener() {
String item;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
item = myEditText.getText().toString();
//Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();//Problem here

// Add your input text to list as follows
todoItems.add(item ); // Now list todoItems has been added with new item at end of list.

aa.notifyDataSetChanged(); // So your list adapter to be refreshed with new item




}
});

关于android - 如何将文本从 EditText 显示到 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13507060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com