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();
}
});