I am using the following method to add a ContextMenu to a custom view i have built but i want to know how to react to the press of that contextmenu.
This is not an Activity so i cannot do this:
#override
public boolean onOptionsItemSelected(MenuItem item) {
Here is the code
private View.OnCreateContextMenuListener vC = new View.OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu arg0, View arg1,
ContextMenuInfo arg2) {
// TODO Auto-generated method stub
arg0.add(0, 0, 0, "Call");
arg0.add(0, 1, 0, "Map");
arg0.add(0, 2, 0, "Market");
}
};
Update:
Here is a very simplified verion of my class.
public final class NewView extends View {
public NewView(Context context, AttributeSet attrs) {
super(context, attrs);
cntxt = context;
this.setLongClickable(true);
this.setOnLongClickListener(vLong);
this.setOnCreateContextMenuListener(vC);
}
private View.OnLongClickListener vLong = new View.OnLongClickListener() {
public boolean onLongClick(View view) {
showContextMenu();
return true;
}
};
private View.OnCreateContextMenuListener vC = new View.OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu arg0, View arg1,
ContextMenuInfo arg2) {
// TODO Auto-generated method stub
arg0.add(0, 0, 0, "Call");
arg0.add(0, 1, 0, "Map");
arg0.add(0, 2, 0, "Market");
}
};
}
Noone seem to answer exhaustively TS' question (and mine eventually), thus let me post a solution to this. Given the code above you can attach custom MenuItem's click handlers:
private View.OnCreateContextMenuListener vC = new View.OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0, 0, 0, "Call")
.setOnMenuItemClickListener(mMenuItemClickListener);
menu.add(0, 1, 0, "Map")
.setOnMenuItemClickListener(mMenuItemClickListener);
menu.add(0, 2, 0, "Market")
.setOnMenuItemClickListener(mMenuItemClickListener);
}
};
private OnMenuItemClickListener mMenuItemClickListener = new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case 0:
// do "Call"
return true;
case 1:
// do "Map"
return true;
case 2:
// do "Market"
return true;
}
return false;
}
};
};
Use item.getItemId() and create switch and cases based on the number returned by getItemId()
Something like this.
#override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case 1:
Log.i("FIRST ITEM: ", "CALL");
break;
case 2:
Log.i("2nd ITEM: ", "MAP");
break;
case 3:
Log.i("3rd ITEM: ", "Market");
break;
default:
}
}
I hope this is what you meant by reacting on menu items selection. :)
This does seem to be an odd inconsistency in Android - a View can create a context menu, but the handling of said menu can only happen in completely different code?
I'm also solving this with setOnMenuItemClickListener(). The documentation suggests this is reasonable, but it requires effort to set it up if you are using a MenuInflater.
public void onCreateContextMenu(final ContextMenu menu, final View v,
final ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
final MenuInflater menuInflater = this.menuInflater;
menuInflater.inflate(R.menu.context_date, menu);
final int length = menu.size();
for (int index = 0; index < length; index++) {
final MenuItem menuItem = menu.getItem(index);
menuItem.setOnMenuItemClickListener(this);
}
}
You could assign a variable to your View from your activity after you inflate the main layout (in `onCreate).
myView = (View) findViewById(R.id.my_view);
Next, do registerForContextMenu(myView);
Finally, you can override onCreateContextMenu where you can add what happens when the context menu appears.
In the view :
Let your view implements ContextMenuInfo
public class MyView extends View implements ContextMenuInfo
Then, override getContextMenuInfo to return your view.
#Override
protected ContextMenuInfo getContextMenuInfo() {
return this ;
}
In you activity :
Register your view for ContextMenu.
registerForContextMenu(myView);
Then access the view in your activity onContextItemSelected.
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case R.id.action_ctx_wordview_read_tts:
MyView myView = (MyView) item.getMenuInfo();
...
}
}
Note :
When I add an onClickListener to the MyView, the contextMenu stoped working. I had to add the following code to solve the problem.
tvWord.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});
Related
I have a listview with a custon adapter. I have to put a context menu for it, but it's not working. And I put the onItemLongClick to the list and it's not working too. I don't know how to trigger the contextmenu. If I have to click on an item or long click on it. I do register a long click to get the id from the item.
EDIT I think i figure out whats the problem. I have a button on my item listview. I remove this button from the layout and the context menu worked fine. But I need this button. Why the button was causing problem in the context menu?
This is the class:
public class HistoricoDespesasActivity extends Activity {
Helper h;
AlphaAnimation buttonClick;
DespesasDAO dDAO;
ListView lv;
DespesaHistoricoAdapter adapter;
int idDespesasSelecionada;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_historico_despesas);
lv = (ListView)findViewById(R.id.lvHistoricoDespesas);
TextView tvMarcaModelo = (TextView)findViewById(R.id.tvMarcaModeloCabecalho);
TextView tvApelido = (TextView)findViewById(R.id.tvApelidoCabecalho);
tvApelido.setVisibility(View.INVISIBLE);
tvMarcaModelo.setVisibility(View.INVISIBLE);
buttonClick = new AlphaAnimation(1, 0.5f);
h = new Helper(this);
h.mostraVeiculoAtivo();
adapter = new DespesaHistoricoAdapter(this);
dDAO = new DespesasDAO(this);
dDAO.open();
Cursor cursor = dDAO.consultarTodasDespesasByIdVeiculo(h.getId());
int id;
String data;
String tipoDespesa = null;
double valor;
int tipo = 0;
if(cursor != null && cursor.moveToFirst()){
do {
id = cursor.getInt(cursor.getColumnIndex(DespesasDAO.COLUNA_ID));
data = cursor.getString(cursor.getColumnIndex(DespesasDAO.COLUNA_DESPESA_DATA));
tipo = cursor.getInt(cursor.getColumnIndex(DespesasDAO.COLUNA_ITEM_ID));
valor = cursor.getDouble(cursor.getColumnIndex(DespesasDAO.COLUNA_DESPESA_VALOR));
if(tipo == 1){
tipoDespesa = "Pedágio";
} else if(tipo == 2){
tipoDespesa = "Estacionamento";
} else if(tipo == 3){
tipoDespesa = "Lavagem";
} else if(tipo == 4){
tipoDespesa = "Diversos";
}
adapter.addDespesa(id, tipoDespesa, data, valor);
} while (cursor.moveToNext());
cursor.close();
dDAO.close();
lv.setAdapter(adapter);
}
lv.setLongClickable(true);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
idDespesasSelecionada = (Integer) parent.getItemAtPosition(position);
return true;
}
});
registerForContextMenu(lv);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Despesas");
menu.add(0, v.getId(), 0, "Deletar");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle().equals("Deletar")){
dDAO.open();
dDAO.removerDespesasById(idDespesasSelecionada);
dDAO.close();
}
onCreate(new Bundle());
return super.onContextItemSelected(item);
}
#Override
protected void onResume() {
onCreate(new Bundle());
super.onResume();
}
}
Remove your setOnItemLongClickListener of listView and replace onContextItemSelected with this
#Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if(item.getTitle().equals("Deletar"))
{
dDAO.open();
dDAO.removerDespesasById(info.position);
dDAO.close();
}
return true;
}
change onCreateContextMenu like this :
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater =getMenuInflater();
inflater.inflate(R.menu.more_tab_menu, menu);
}
see this topic :
Android, How to create Context Menu...
EDIT :
use button. image Button and list view is clickable. if you use Button and set android:focusable="false" android:focusableInTouchMode="false" work fine.
I have a defined listview in xml and add the data by modifying it through adapter..
I want to display context menu by single click the list, which I already done..
But how to get the clicked list position ?
Here is the code, this is how I call the context menu :
list = (ListView)findViewById(R.id.grant_list);
// get all data in database
List<GrantListData> dataList = con.getAllList();
try {
list.removeAllViews();
} catch (Exception e) {}
listAll.clear();
arrName.clear();
arrPhone.clear();
for (int i = 0; i < dataList.size(); ++i) {
listAll.add(dataList.get(i));
arrName.add(dataList.get(i).getName().toString());
arrPhone.add(dataList.get(i).getPhone().toString());
}
adapter = new StableArrayAdapter(this,android.R.layout.simple_list_item_1, arrName);
list.setAdapter(adapter);
this.registerForContextMenu(list);
list.setOnLongClickListener(this);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
String item = (String)parent.getItemAtPosition(pos);
activity.registerForContextMenu(view);
activity.openContextMenu(view);
}
});
and this is onCreateContextMenu method :
int num=-1;
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo Info = (AdapterContextMenuInfo) menuInfo;
//String clicked = adapter.getItem(Info.position); --> this is when I get confused
//num = Info.position;
//menu.setHeaderTitle("Options for " + clicked);
menu.add(1, 1, 1, "Details");
//menu.addSubMenu(1, 1, 1, arrName.get(Info.position));
//menu.addSubMenu(1, 1, 2, arrPhone.get(Info.position));
menu.add(1, 2, 2, "Delete");
}
[ADDITION]
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
#Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
#Override
public boolean hasStableIds() {
return true;
}
}
Thanks in advance :)
[SOLVED]
Well, it took a while for me to realize simple thing. What I mean is this
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
currList = (String)parent.getItemAtPosition(pos);
currIndex = pos;
registerForContextMenu(view);
openContextMenu(view);
}
});
and this
int num=-1;
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo Info = (AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle("Options for "+ currList);
menu.add(1, 1, 1, "Details");
menu.addSubMenu(1, 1, 1, arrName.get(currIndex));
menu.addSubMenu(1, 1, 2, arrPhone.get(currIndex));
menu.add(1, 2, 2, "Delete");
}
Thanks for the answers anw :)
You set a tag with position for your view and then set the groupid for your menu item by this position.
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
String item = (String)parent.getItemAtPosition(pos);
view.setTag(position);
activity.registerForContextMenu(view);
activity.openContextMenu(view);
}
});
put the position in the groupid of the menu item which you can retrieve by getGroupId
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo Info = (AdapterContextMenuInfo) menuInfo;
//String clicked = adapter.getItem(Info.position); --> this is when I get confused
int position = Integer.parseInt(v.getTag());
//num = Info.position;
//menu.setHeaderTitle("Options for " + clicked);
menu.add(position, 1, 1, "Details");
//menu.addSubMenu(1, 1, 1, arrName.get(Info.position));
//menu.addSubMenu(1, 1, 2, arrPhone.get(Info.position));
menu.add(position, 2, 2, "Delete");
}
try this one:
((ListView) findViewById(R.id.lisview)).setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
long arg3) {
// TODO Auto-generated method stub
/*"Toast the int which is arg2 to show the position of listview that has been clicked"*/
}
});
Using Context menu:
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Do you want to add?");
MenuItem chix = menu.add("Add Chicken");
MenuItem pasta = menu.add("Add Pasta");
MenuItem drinks = menu.add("Add Drinks");
MenuItem con = menu.add("Continue");
chix.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
//your codes here//
return true;
}
});
pasta.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
//your codes here//
return true;
}
});
drinks.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
//your codes here//
return true;
}
});
con.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
//your codes here//
return true;
}
});
}
I have a dialog that shows a list of items, I need to be able to edit/delete items in this list so I put a context menu in so when the user long presses on an item it they can choose what they want to do (edit or delete the item).
The problem is that onContextItemSelected never gets called when an item in the context menu is selected.
I checked to see if maybe the activity that created the dialog fragment is getting the callback but that is not either so why is there it not getting called? Can you not do a context menu in a dialog?
public class TypesDialogList extends DialogFragment implements LoaderManager.LoaderCallbacks<Cursor>,OnItemClickListener,OnCreateContextMenuListener{
ListView lv;
SimpleCursorAdapter mAdapter;
private int EDIT_TYPE = 1;
private int DELETE_TYPE = 2;
OnEditType mType;
public Dialog onCreateDialog(Bundle state){
final View v = getActivity().getLayoutInflater().inflate(R.layout.layer_dialog_layout, null, false);
lv = (ListView)v.findViewById(R.id.listView1);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setOnItemClickListener(this);
lv.setOnCreateContextMenuListener(this);
return new AlertDialog.Builder(getActivity()).setView(v).setPositiveButton("Add Type", new OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
}).setTitle("Type's").create();
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
long id = info.id;
if(id > 3){
menu.setHeaderTitle("Type Menu");
menu.add(Menu.NONE, EDIT_TYPE, 1, "Edit");
menu.add(Menu.NONE, DELETE_TYPE, 2, "Delete");
}else{
Toast.makeText(getActivity(),"Cannot edit type",Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
AdapterView.AdapterContextMenuInfo oMenuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
long id = oMenuInfo.id;
if(item.getItemId() == EDIT_TYPE){
}else if(item.getItemId() == DELETE_TYPE){
}
return true;
}
}
For anybody still looking for a workaround, I just solved this problem by creating an anonymous OnMenuItemClickListener that delegates back to onContextItemSelected(MenuItem item) and setting it on all the items in my menu.
#Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
// Creation/inflate menu here
OnMenuItemClickListener listener = new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
onContextItemSelected(item);
return true;
}
};
for (int i = 0, n = menu.size(); i < n; i++)
menu.getItem(i).setOnMenuItemClickListener(listener);
}
I tried to get answer here but didn't get any perfect answer.
I am trying to show context menu on onItemLongClick but no success as i am using both onItemClick and onItemLongClick
i am using onItemClick to start a new activity but no success on both.
Here is the code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_contacts);
contactList = new ArrayList<HashMap<String,String>>();
new LoadAllContacts().execute();
registerForContextMenu(getListView());
ListView listView = getListView();
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int postion, long id) {
registerForContextMenu( view );
openContextMenu( view );
return true;
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int postion, long id) {
Intent intent = new Intent(AllContactsActivity.this, editContactActivity.class);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100)
{
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.listview_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.editContactMI:
Intent i = new Intent(getApplicationContext(), editContactActivity.class);
i.putExtra(TAG_ID, cId);
i.putExtra(TAG_NAME, cName);
i.putExtra(TAG_CONTACT_NO, cNumber);
startActivityForResult(i, 100);
cId = null;
cName = null;
cNumber = null;
break;
case R.id.deleteContactMI :
new DeleteContact().execute();
break;
case R.id.saveContactMI:
break;
default:
cId = null;
cName = null;
cNumber = null;
break;
}
return true;
}
I am trying to show context menu on onItemLongClick
To use the context menu system, you do not implement an OnItemLongClickListener. Instead, you call registerForContextMenu() (e.g., from onCreate() of the activity). Simply delete your OnItemLongClickListener from your code shown above, and you should have better luck.
Try this:
listView.setOnItemLongClickListener(this);
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c_index = (Cursor) parent.getItemAtPosition(position);
detail_id = c_index.getInt(c_index.getColumnIndexOrThrow(DbAdapter.KEY_RID));
registerForContextMenu( parent );
openContextMenu( parent );
return true;
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.list) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_context, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_details:
//some code
return true;
case R.id.action_share:
//some code
return true;
case R.id.action_del:
//enter code here`
return true;
default:
return super.onContextItemSelected(item);
}
}
I have a list activity, and I chose to manually add the first item which is "add new item...".
I have registered the context menu for the whole listview, using registerForContextMenu(getListView()); straight into the onCreate.
When the context menu is build, the system calls onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo). The View v is the listView, and I cannot find a way to know which item in the listview is being long-pressed.
I could create a xml layout, with a layout for the "add new item..." and add a listview after, which would be populated by the activity, and that would react to the context menu, but I'm sure there is a way to solve this problem without any xml layout.
I have tried to register each view inside my listview using registerForContextMenu, which works, however the listview doesn't respond to touch anymore.
Here is my activity code listing:
public class AMain extends ListActivity {
private List<String> pregList;
private List<Long> pregIds;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pregList = new ArrayList<String>();
pregIds = new ArrayList<Long>();
registerForContextMenu(getListView());
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// TODO: hide the menu for the 1st item!!
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
Logger.d("id = "+info.id);
switch (item.getItemId()) {
case R.id.menu_show:
showPregnancy((int) info.id);
return true;
case R.id.menu_edit:
editPregnancy((int) info.id);
return true;
case R.id.menu_delete:
//TODO: do the deletion
return true;
default:
return super.onContextItemSelected(item);
}
}
protected void onStart() {
super.onStart();
clearPregList();
loadPregList();
getListView().setAdapter(new PregnancyListAdapter(this));
}
void clearPregList() {
pregList.clear();
pregIds.clear();
}
void loadPregList() {
PregnanciesDbAdapter db = new PregnanciesDbAdapter(this);
db.open();
Cursor c = db.getPregnancies();
if (c != null) {
do {
pregList.add(c.getString(c.getColumnIndex(PregnanciesDbAdapter.KEY_PREG_NOM)));
pregIds.add(c.getLong(c.getColumnIndex(PregnanciesDbAdapter.KEY_PREG_ROWID)));
} while (c.moveToNext());
c.close();
}
db.close();
}
private class PregnancyListAdapter extends BaseAdapter {
private Context context;
public PregnancyListAdapter(Context ctx) {
context = ctx;
}
#Override
public int getCount() {
return pregList.size()+1;
}
#Override
public Object getItem(int position) {
if (position == 0) { // add button
return getString(R.string.addPreg);
} else {
return pregList.get(position-1);
}
}
#Override
public long getItemId(int position) {
if (position == 0) {
return -1;
}
return pregIds.get(position-1);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout itemLayout;
itemLayout= (LinearLayout) LayoutInflater.from(context).inflate(R.layout.homelist_item_pregnancy, parent, false);
ImageView logo = (ImageView) itemLayout.findViewById(R.id.logo);
TextView pregName = (TextView) itemLayout.findViewById(R.id.pregName);
if (position == 0) {
itemLayout.setFocusable(false);
itemLayout.setFocusableInTouchMode(false);
pregName.setText(getString(R.string.addPreg));
} else {
logo.setVisibility(View.INVISIBLE);
pregName.setText(pregList.get(position-1));
}
itemLayout.setId(position);
return itemLayout;
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (position == 0) {
startActivity(prepareIntent(AShowPregnancy.class, 0));
} else {
showPregnancy(position-1);
}
}
void showPregnancy(int pregId) {
startActivity(prepareIntent(AShowPregnancy.class, pregId));
}
void editPregnancy(int pregId) {
startActivity(prepareIntent(ANewPregnancy.class, pregId));
}
Intent prepareIntent(Class<?> className, int pregId) {
Intent i = new Intent(this, className);
if (pregId > 0) {
PregnanciesDbAdapter db = new PregnanciesDbAdapter(this);
db.open();
Pregnancy p = db.load(pregIds.get(pregId));
db.close();
i.putExtra(C.EXTRA_PREGNANCY, p);
}
return i;
}
}
Thanks for reading. Hope you can help.
Oh, god, again. I found out myself how to do it, and it was easier than easy.
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
// info.position is the position in the ListView
I hate myself :)