单击后如何在列表视图中显示该项目

时间:2017-06-15 13:08:22

标签: android listview

我是Android的新手,我有listview个项目,我创建了一个弹出菜单,当用户按下一行时,他必须在其他活动中有一个项目。 我的问题是,无论我在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;
}

1 个答案:

答案 0 :(得分:1)

通过使用listview setOnItemClickListener(),您可以处理listview的click事件

lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    }
                });