动态添加项目到列表视图
我正在制作一个需要使用 ListView 的 Android 应用程序。一旦用户按下菜单按钮,就会弹出一个弹出窗口,其中包含一个 TextView、EditText 和两个按钮“确定”和“取消”。一旦用户按下“确定”,EditText 内的文本就应添加到 ListView 中。而且取消按钮很明显。我还希望能够长按 ListView 项目来打开包含删除按钮的弹出窗口。我怎样才能使这成为可能?到目前为止我正在使用这段代码:
public class NotesActivity extends ListActivity {
/** Called when the activity is first created. */
Button AddItemToListView;
static final String[] COUNTRIES = new String[] {
"Matte på A1 med Ole", "Engelsk på klasserommet", "Film på A1 etter friminuttet"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notes);
setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "Note: " + ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu meny) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.listviewmenubuttons, meny);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.AddItemToListView:
Toast.makeText(NotesActivity.this,
"Add note button pressed", Toast.LENGTH_SHORT)
.show();
break;
}
return true;
}
}
I am making an android application that needs to use a ListView. Once a user presses a menubutton, it pops up a popupwindow containing a TextView, EditText and two Buttons, "Ok" and "Cancel". Once the user presses "Ok", the text inside the EditText should be added to the ListView. And the cancel Button is obvious. I also want to be able to long press on a ListView item to open a popupwindow containing a delete Button. How can I make this possible? I am using this code so far:
public class NotesActivity extends ListActivity {
/** Called when the activity is first created. */
Button AddItemToListView;
static final String[] COUNTRIES = new String[] {
"Matte på A1 med Ole", "Engelsk på klasserommet", "Film på A1 etter friminuttet"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notes);
setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "Note: " + ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu meny) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.listviewmenubuttons, meny);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.AddItemToListView:
Toast.makeText(NotesActivity.this,
"Add note button pressed", Toast.LENGTH_SHORT)
.show();
break;
}
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想在访问 listview.setonlongclick() 后添加功能,请尝试对对话框进行一些研究。这是关于 Android 对话框开发人员的链接。
Try doing some research on dialogs if you want to add feature after accessing listview.setonlongclick(). Here is a link on android dialog developers.
由于您使用的是 ArrayAdapter,因此当用户点击添加时,您必须将新项目添加到数组中(从数组更改为列表以使其更容易)。然后您应该从 ArrayAdapter 调用 notificationDataSetChanged()。
删除是相同的,但是您从列表中删除该项目。调用notifyDataSetChanged()是告诉ListView需要重绘自己。
Since you are using an ArrayAdapter, when the user taps on add, you must add the new item to your array (change from an array to an List to make it easier). Then you should call notifyDataSetChanged() from the ArrayAdapter.
For deleting is the same, but you remove the item from the List. The call to notifyDataSetChanged() is to tell the ListView that it needs to redraw itself.