Inserting bank value in Expandablelistview via hasmap

Asked

Viewed 65 times

1

I’m having trouble inserting values in Expandablelistview from Android Sqlite database.

In my app, I have a main table called DISCIPLINES and another table that has foreign key reference to this main one, called NOTES.

In my Expandablelistview, the group is being filled by the DISCIPLINE table of the bank, which are values already defined in a previous step in my application. So far so good, the problem appears when I need to fill the Childrens, these Children need to be filled according to the discipline that will be selected via Expandablelistview. And I’m trying to do it via Hashmap.

I think my problem is in logic, but I’ve tried several ways to fill this out, if anyone can help me or even talk another way to do it I appreciate.

Expandabled class:

`public class Expandableadapter extends Baseexpandablelistadapter {

private List<Disciplina> listGroup;
private HashMap<Disciplina, List<Nota>> listData;
private LayoutInflater inflater;
private Nota nota = new Nota();
private Disciplina disciplina = new Disciplina();
Context context;

public ExpandableAdapter(Context context, List<Disciplina> listGroup, HashMap<Disciplina, List<Nota>> listData){
    this.listGroup  = listGroup;
    this.listData = listData;
    this.context = context;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getGroupCount() {
    return listGroup.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    if(listData.size() == 0){
        Toast toast = Toast.makeText(context, "Não há notas nesta disciplina", Toast.LENGTH_SHORT);
        toast.show();

        return 0;
    }
    else{
        return listData.get(listGroup.get(groupPosition)).size();
    }
}

@Override
public Object getGroup(int groupPosition) {
    return listGroup.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return listData.get(listGroup.get(groupPosition)).get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return listGroup.get(groupPosition).getId();
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return listData.get(listGroup.get(groupPosition)).get(childPosition).getId();
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    final ViewHolderGroup holder;

    if(convertView == null) {
        convertView = inflater.inflate(R.layout.header_expandable_list_view, null);
        holder = new ViewHolderGroup();
        convertView.setTag(holder);

        holder.txtGroup = (TextView) convertView.findViewById(R.id.txtGroup);
        //holder.imbOpcoes = (Button) convertView.findViewById(R.id.imbOpcoes);
    }else{

        holder = (ViewHolderGroup) convertView.getTag();
    }

    disciplina = listGroup.get(groupPosition);
    holder.txtGroup.setText(String.valueOf(disciplina.getAbreviacao()));

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    ViewHolderItem holder;

    if(convertView == null) {
        convertView = inflater.inflate(R.layout.item_expandable_list_view, null);
        holder = new ViewHolderItem();
        convertView.setTag(holder);

        holder.txtItem = (TextView) convertView.findViewById(R.id.txtItem);
    }else{

        holder = (ViewHolderItem) convertView.getTag();
    }

    nota = listData.get(listGroup.get(groupPosition)).get(childPosition);

    holder.txtItem.setText(String.valueOf(nota.getNota()));

    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

class ViewHolderGroup {

    TextView txtGroup;
}

class ViewHolderItem {

    TextView txtItem;
}`

Last method I tried to insert the notes:

    public void construirLista(){

    listGroup = new ArrayList<Disciplina>();
    listData = new HashMap<Disciplina, List<Nota>>();
    ArrayList<Nota> auxilirList = new ArrayList<Nota>();
    ArrayList<Nota> arrayNota = new ArrayList<Nota>();
    ArrayList<Disciplina> arrayDisciplina = new ArrayList<Disciplina>();

    nota = new Nota();
    disciplina = new Disciplina();
    repositorioDisciplina = new RepositorioDisciplina(conn);

    listGroup = repositorioDisciplina.buscaDisciplina(0);
    disciplina = listGroup.get(0);

    for(int f = 0; f < listGroup.size(); f++) {

        for(int j = 0; j < arrayNota.size(); j++){

            nota = arrayNota.get(j);

            auxilirList = repositorioNota.buscarNota(this, nota.getId_disciplina());//busca as NOTAS pelo ID da disciplina
            arrayDisciplina = repositorioDisciplina.buscaDisciplina(nota.getId_disciplina());

            disciplina = arrayDisciplina.get(0);
        }

        listGroup.add(disciplina);
        listData.put(disciplina, auxilirList);
    }
}
  • 1

    Why, instead of using a Hashmap, doesn’t the Discipline class have an attribute that is a Note list? If you want to see an example of implementation see this reply, Adapting to your case Question will be Discipline and Answer will be Notes.

  • Thanks for the help! but I think this example would not fit in my case, because the notes table will be used on other occasions, it has several attributes, weight, identification, etc. I believe that there has to be a notes table even, I’m not sure rsrs

  • 1

    The two tables will still have to exist. The difference is only in the class Discipline.

  • Now yes, it worked perfectly, I managed to make the adaptation I needed, THANK YOU

No answers

Browser other questions tagged

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