我是Android的新手。我有一个像这样的主类
public class SearchDay extends AppCompatActivity implements View.OnClickListener {
private ImageButton insertDate;
private Calendar cal;
private int day;
private int month;
private int year;
DatabaseHelper db;
private Spinner SelectTypeNote;
private EditText et;
private Button btnSubmitSearchByDay, btnCancelSearchByDay;
SimpleCursorAdapter dataAdapter;
private static final int REQUEST_CODE = 10;
private ListView listViewManagePage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_day);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Tìm kiếm theo ngày");
SelectTypeNote = (Spinner) findViewById(R.id.TypeNote_SearchByDay);
insertDate = (ImageButton) findViewById(R.id.dateInsertSearchByDay);
cal = Calendar.getInstance();
et = (EditText) findViewById(R.id.dateInsertSearchByDay_editText);
btnSubmitSearchByDay = (Button) findViewById(R.id.searchByDay);
btnCancelSearchByDay = (Button) findViewById(R.id.cancelSearchByDay);
db = new DatabaseHelper(this);
cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
insertDate.setOnClickListener(this);
Bundle extras = getIntent().getExtras();
final String idUser = extras.getString("IdUser");
setBtnSubmitNoteSearchByDay();
setBtnCancelNoteSearchByDay(idUser);
// registerForContextMenu(listViewManagePage);
listViewManagePage = (ListView) findViewById(R.id.listView_SearchByDay);
listViewManagePage.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(SearchDay.this, "Thanh cong", Toast.LENGTH_SHORT).show();
registerForContextMenu(listViewManagePage);
}
});
}
}
当我点击Listview中的项目时,它是成功的。但我不知道如何打开上下文菜单但不使用适配器。
你能指导我如何创建一个没有适配器的上下文菜单。
这一行registerForContextMenu(listViewManagePage);
不要求我覆盖任何方法。
答案 0 :(得分:1)
覆盖onCreateContextMenu()以将项目添加到菜单
public void onCreateContextMenu(ContextMenu菜单,View v,ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu,v,menuInfo);
menu.setHeaderTitle(“选择动作”);
menu.add(0,v.getId(),0,“Call”); // groupId,itemId,order,title
menu.add(0,v.getId(),0,“SMS”);
}
覆盖onContextItemSelected()以处理菜单项的点击事件
public boolean onContextItemSelected(MenuItem item){
如果(item.getTitle()== “呼叫”){
Toast.makeText(getApplicationContext(),“调用代码”,Toast.LENGTH_LONG)。show();
}
否则if(item.getTitle()==“SMS”){
Toast.makeText(getApplicationContext(),“发送短信代码”,Toast.LENGTH_LONG)。show();
}其他{
返回false;
}
返回true;
}