我正在制作一个需要使用 ListView 的 android 应用程序。一旦用户按下菜单按钮,它就会弹出一个弹出窗口,其中包含一个 TextView、EditText 和两个按钮,“Ok”和“Cancel”。一旦用户按下“确定”,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;
}
}