Roland
Roland

Reputation: 876

longClick is not working in my app

I have a listview with a Custon adapter. I implemented a listener for longclick to handle the context menu. The problem is that the long click is not working. The contextmenu is not appering.

Here are the codes:

listener from listview

    registerForContextMenu(lv);

    lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {              
            seguro = (SeguroSelecaoModel)adapter.getItem(position);
            return false;
        }
    }); 

Code from custom adapter

    public View getView(final int index, View view, ViewGroup parent) {
    if(view == null){
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        view = inflater.inflate(R.layout.linha_selecionar_seguro, parent, false);                   
    }

    final SeguroSelecaoModel seguro = (SeguroSelecaoModel)getItem(index);

    view.setLongClickable(true);

    TextView tvNome = (TextView) view.findViewById(R.id.tvNomeSeguro);
    tvNome.setText(seguro.getNomeSeguradora());

    TextView tvData = (TextView) view.findViewById(R.id.tvDataValidadeSelecionarSeguro);
    tvData.setText(seguro.getDataValidade());

    ImageView ivCell = (ImageView) view.findViewById(R.id.ivCellSelecionarSeguro);      
    Button btEdicao = (Button) view.findViewById(R.id.btEditarSeguro);      

    TextView labelSeguro = (TextView) view.findViewById(R.id.labelSeguroSelecionarSeguro);
    TextView labelValidade = (TextView) view.findViewById(R.id.labelValidadeSelecionarSeguro);

    if(seguro.isAtivo() == true){
        ivCell.setBackgroundResource(R.drawable.cellselectedbackground);
        tvNome.setTextColor(view.getResources().getColor(R.color.amarelo));
        tvData.setTextColor(view.getResources().getColor(R.color.amarelo));
        btEdicao.setBackgroundResource(R.drawable.goldbuttonarrow);
        labelSeguro.setTextColor(view.getResources().getColor(R.color.vermelho));
        labelValidade.setTextColor(view.getResources().getColor(R.color.vermelho));
    } else{
        ivCell.setBackgroundResource(R.drawable.cellbg);
        tvNome.setTextColor(Color.WHITE);
        tvData.setTextColor(Color.WHITE);
        btEdicao.setBackgroundResource(R.drawable.bt_seta);
        labelSeguro.setTextColor(view.getResources().getColor(R.color.cinza));
        labelValidade.setTextColor(view.getResources().getColor(R.color.cinza));
    }

    btEdicao.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, DadosSeguro1Activity.class);
            intent.putExtra("idSeguro", seguro.getId());
            context.startActivity(intent);       
            //((Activity) context).finish();                
        }
    });

    return view;
}

Code from context menu

 @Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {     
    super.onCreateContextMenu(menu, v, menuInfo);

    menu.setHeaderTitle(seguro.getNomeSeguradora());
    menu.add("Selecionar Seguro");
    menu.add("Excluir Seguro"); 

}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    sDAO.open();        
    if(item.getTitle().equals("Excluir Seguro")){
        sDAO.selecionaSeguro(seguro.getId());           
        adapter.clearList();
        buildList();
        sDAO.close();   
    } else if(item.getTitle().equals("Selecionar Seguro")){
        sDAO.removeSeguro(seguro.getId());
    }
    return super.onMenuItemSelected(featureId, item);
}

xml from listview

  <ListView
                android:id="@+id/lvSeguroPrincipal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/imgFaixaSeguro2"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/imgFaixaSeguro"
                android:divider="@android:color/transparent"
                android:longClickable="true" >

Upvotes: 0

Views: 1988

Answers (2)

ramaral
ramaral

Reputation: 6179

You must register your listView for context menu on activity onCreate method:

registerForContextMenu(lv);  

You don't need this line view.setLongClickable(true); on adapter

And, in this particular case, setOnItemLongClickListener should return false

Upvotes: 2

AndRSoid
AndRSoid

Reputation: 1825

setOnItemLongClickListener should return true if it is consumed. See here.

   lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {              
            seguro = (SeguroSelecaoModel)adapter.getItem(position);
            return true;
        }
    });

Upvotes: 0

Related Questions