sách gpt4 ăn đã đi

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

In lại 作者:搜寻专家 更新时间:2023-11-01 08:06:47 26 4
mua khóa gpt4 giày nike

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

public class MainActivity extends Activity {

@Ghi đè
được bảo vệ void onCreate(Gói savedInstanceState) {
super.onCreate(savedInstanceState);
thiết lậpContentView(R.layout.activity_main);
ListView myListView = (ListView) findViewById(R.id.myListView);
final EditText myEditText = (EditText) findViewById(R.id.myEditText);
final ArrayList todoItems = new ArrayList();
final ArrayAdapter aa;
aa = new ArrayAdapter(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;
@Ghi đè
public void onClick(View v) {
// TODO Phương thức tự động tạo stub
item = myEditText.getText().toString();
//Toast.makeText(null, item, Toast.LENGTH_SHORT).show();//Problem here

}
});
}
@Ghi đè
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);
trả về giá trị đúng;
}

câu trả lời hay nhất

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

  myButton.setOnClickListener(new View.OnClickListener() {
String item;
@Ghi đè
public void onClick(View v) {
// TODO Phương thức tự động tạo 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
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress