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);
}
}
Related
I have a ListView. This ListView has an array and the array has 5 numbers. First of all, in the first activity the user selects 5 numbers and theses 5 number go in the ListView in the second activity. I want to start an Intent with theses 5 numbers when the user click on any number on the Context menu. The intent will start that position. But why cant I get the selected number?
public class AnaMenu extends AppCompatActivity {
public static final int MENU_Ara = Menu.FIRST+1;
int sayı = 50;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ana_menu);
ListView liste = (ListView) findViewById(R.id.oyun);
OyunTextView oyunTextView = new OyunTextView();
liste.setAdapter(oyunTextView);
registerForContextMenu(liste);
}
class OyunTextView extends BaseAdapter {
#Override
public int getCount() {
Intent i = getIntent();
final ArrayList<String> secilmis = i.getStringArrayListExtra("listem");
return secilmis.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.oyuntextview , null);
TextView namesbox = (TextView)convertView.findViewById(R.id.isim);
TextView sayılar = (TextView)convertView.findViewById(R.id.sayı);
Intent i = getIntent();
final ArrayList<String> secilmis = i.getStringArrayListExtra("listem");
namesbox.setText(secilmis.get(position));
sayılar.setText(String.valueOf(sayı));
if (sayı == 50) {
sayılar.setTextColor(getResources().getColor(R.color.elli));}
return convertView;
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, MENU_Ara, Menu.NONE, "Ara");
super.onCreateContextMenu(menu, v, menuInfo);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
Intent i = getIntent();
secilmis = i.getStringArrayListExtra("listem");
switch (item.getItemId()) {
case MENU_Ara:
Intent ara = new Intent(Intent.ACTION_DIAL);
ara.setData(Uri.parse());
startActivity(ara);
return true;
}
return super.onContextItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.mains,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_browse :ayar();return true;
case R.id.backbut: geri();return true;
}
return super.onOptionsItemSelected(item);
}
private void ayar() {
Intent i = new Intent(AnaMenu.this, Ayarlar.class);
startActivity(i);
}
private void geri() {
Intent ii = new Intent(AnaMenu.this, MainActivity.class);
startActivity(ii);
AnaMenu.this.finish();
}
}
}
You can use arraylistname.get(position) in the onItemClickListener method to get the position of the selected item.
I'm new in Android, I have a listview of items, and I have created a popup menu, when the user press a row he must have an item in a other activity.
my problem is, he sends me always the first item... no matter where I click in the listview.
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
final ListAdapter adapter = new SimpleAdapter(busqueda.this, productos,
R.layout.list_layout, new String[]{"codigo", "descrip", "precio", "fisicolug"},
new int[]{R.id.txtCodigo, R.id.txtDescrip, R.id.txtPrecio, R.id.Stock});
lista.setAdapter(adapter);
registerForContextMenu(lista);
registerForContextMenu(textView);
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflate = getMenuInflater();
if (v.getId() == R.id.listView) {
inflate.inflate(R.menu.menu_main, menu);
}
}
public boolean onContextItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.masIn:
TextView textView = (TextView) findViewById(R.id.txtCodigo);
String text = textView.getText().toString();
Intent intent;
intent = new Intent(getApplicationContext(), Resultado.class);
intent.putExtra("CODIGO", text);
startActivity(intent);
super.onContextItemSelected(item);
}
return true;
}
By using listview setOnItemClickListener() you can handle the click event of listview
lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
I have multiple ListView in one activity with ContextMenu for each one of ListView.
Actually it should work fine with all the 7 ListViews,but it work for only Seventh List
this is my code.
ArrayAdapter<Course> adapter;
for(int i=0;i<7;i++){
courses = db.findFiltered(String.format("day == %d ", i), "startTime ASC");
ListView lv1 = (ListView) findViewById (arrayList[i]);
registerForContextMenu(lv1);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
adapter = new CourseListAdapter(this, courses);
lv1.setAdapter(adapter);
}
#Override
public void onCreateContextMenu(android.view.ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.editItem:
//do something
return true;
case R.id.showItem:
//do something
return true;
case R.id.deleteItem:
db.removeCourse(adapter.getItem(info.position);
adapter.remove(adapter.getItem(info.position));
return true;
default:
return super.onContextItemSelected(item);
}
}
please help
Adapter instance is set to last iteration ListView object. This is quick code change. Try this:
private ArrayAdapter<Course> selectedListViewAdapter;
...
for(int i=0;i<7;i++){
courses = db.findFiltered(String.format("day == %d ", i), "startTime ASC");
ListView lv1 = (ListView) findViewById (arrayList[i]);
registerForContextMenu(lv1);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
ArrayAdapter<Course> adapter = new CourseListAdapter(this, courses);
lv1.setAdapter(adapter);
}
#Override
public void onCreateContextMenu(android.view.ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
try {
ListView selectedListView = (ListView)v;
selectedListViewAdapter = (ArrayAdapter<Course>)selectedListView.getAdapter();
} catch(ClassCastException e) {
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.editItem:
//do something
return true;
case R.id.showItem:
//do something
return true;
case R.id.deleteItem:
if(null != selectedListViewAdapter) {
db.removeCourse(selectedListViewAdapter.getItem(info.position);
selectedListViewAdapter.remove(selectedListViewAdapter.getItem(info.position));
}
return true;
default:
return super.onContextItemSelected(item);
}
}
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 this code , this is a list class where I'm going to save my notes here but whenever im opening this class it keeps on telling me to force close it .
Any idea why?
public class Class1 extends ListActivity {
private static final int ACTIVITY_CREATE1=0;
private static final int ACTIVITY_EDIT1=1;
private static final int DELETE_ID1 = Menu.FIRST;
private int mNoteNumber1 = 1;
private Class1Db mDbHelper1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.class1);
mDbHelper1 = new Class1Db (this);
mDbHelper1.open();
fillData();
registerForContextMenu(getListView());
Button addnote = (Button)findViewById(R.id.addnotebutton);
addnote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createNote();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.class1, menu);
return true;
}
private void createNote() {
Intent i = new Intent(this, DeleteEdit1.class);
startActivityForResult(i, ACTIVITY_CREATE1);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, DeleteEdit1.class);
i.putExtra(Class1Db.KEY_ROWID1, id);
startActivityForResult(i, ACTIVITY_EDIT1);
}
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor notesCursor = mDbHelper1.fetchAllNotes();
startManagingCursor(notesCursor);
String[] from = new String[] { Class1Db.KEY_TITLE1 ,Class1Db.KEY_DATE1};
int[] to = new int[] { R.id.text1 ,R.id.date_row};
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.class1row, notesCursor, from, to);
setListAdapter(notes);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID1, 0, R.string.menu_delete);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case DELETE_ID1:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper1.deleteNote(info.id);
fillData();
return true;
}
return super.onContextItemSelected(item);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
fillData();
}
}