Get the id of the textview that is inserted in the listview and put in the bd

1

I have a layout with 2 lists, one complements the other, category and subcategory I would like, when I would insert a subcategory when clicking add feed the database with the id of the category that is on the same line, that is, I will link a category to a subcategory.

For now the app is adding a subcategory to all categories, as I have not been able to resolve this yet.

Example,whenIclickedtoaddasubcategory("+" button next to the category) open a dialog to insert the name of the subcategory (I already have ready) and clicking to add it would call the id of the category that was in the same line of the button (in the image the Acquisitions line) and insert in the idcategory key that has in the subcategory table.

Here is the layout, it is a linear one with an edittext, a button and a list, that inside it has another list where the subclasses go

Code that inserts the Subcategory - CategoryAdapter

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    final int auxPosition = position;

    Categoria categoria = new Categoria();



    LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    final RelativeLayout layout = (RelativeLayout)
            inflater.inflate(R.layout.categoria_row,null);

    final TextView categ = (TextView)
            layout.findViewById(R.id.tvCat);
    categ.setText(lista.get(position).getNome());



    final ImageButton button = (ImageButton)
            layout.findViewById(R.id.btAddSubCat);
    button.setTag(categoria.getId());

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            AlertDialog.Builder alert = new AlertDialog.Builder(context);
            alert.setTitle("Adicionar subcategoria");
            alert.setMessage("Digite um nome para a subcategoria:");

            final EditText input = new EditText(context);
            alert.setView(input);


            alert.setPositiveButton("Adicionar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    int idCategoria = (Integer) button.getTag();

                    subcategoria.setNome(input.getText().toString());
                    subcategoria.setIdCategoria(idCategoria);


                    SubCategoriaDAO subCategoriaDAO = new SubCategoriaDAO(context);

                    subCategoriaDAO.inserir(subcategoria);
                }
            });

            alert.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    return;
                }
            });

            alert.show();
        }
    });
    
asked by anonymous 22.01.2015 / 18:52

1 answer

1

Simple!

You need to add the category id to the '+' button using the setTag(...) method that is made available to any visual component that inherits from View . That is, when the user clicks the button, the listener responsible for the click event will pass as parameter to View clicked, at that moment you execute a getTag(...) that will return a Object , then just give a cast to Integer and perform CRUD in the database. : -)

I hope I have helped!

    
22.01.2015 / 19:10