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

Asked

Viewed 894 times

1

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

For now the app is adding a subcategory to all categories as I haven’t been able to solve this yet.

Exemplo de categoria e subcategoria

Example, when I clicked to add a subcategory ("+" button next to the category) open a dialog to insert the name of the subcategory (I’m ready) and when clicking to add it linked the id of the category that was in the same line button (in the image the Acquisitions line) and insert in the idcategory key in the subcategory table.

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

Lista CategoriasLista SubCategorias

Code entering the Subcategory - Categoriaadapter

@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();
        }
    });
  • Where’s the code showing how far you’ve gone?

  • You have to explain this layout. It’s confusing.

  • There’s no use throwing an image and asking us to help in this or that.

  • @Rafael, you’re right, it is a layout with 2 lists, 2 Adapters, I will put images and the code that makes add the subclass.

1 answer

1

Simple!

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

I hope I’ve helped!

  • Would that be the method? 
final ImageButton button = (ImageButton)
 layout.findViewById(R.id.btAddSubCat);
button.setTag("idcategoria");
e como seria o cast pra pegar a id do textview? @Felipebonezi

  • You have to exchange the "idcategory" for the integer that identifies the category, not a String. For the cast, just make int idCategory = (Integer) button.getTag();

  • Now the subcategory is not appearing, botei no debug is returning to subcategory correctly, but with idcategory 0, that must be why it does not appear in the list. @Felipebonezi

  • You passed the idCategory in setTag()?

  • I passed yes @Felipebonezi, look at the code there in the question, I just updated.

  • 1

    @Allanchrystian you’re instantiating a Category for every view created in getView, you can’t! Your Adapter has to receive a populated Category List, and in getView you call the Adapter getItem method to return to Category! How do you want to have the category id if when you give a new Category() the id value is initialized with 0???

  • I get what you mean @Felipebonezi, but how am I going to do it? This part I don’t understand, can you explain to me? There are some correct code parde?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.