Frage

Now I use 2 entities to get group name and child name to ExpendableListView.

I what to use only group entity to get group and child values for ExpendableListView, group entity already have relationship with child entity. How should I get values only use group entity(there are 2 values in group, 3 values in child)? And how to reset adapter?

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

    jsonFileParser = new JSONFileParser(getApplication());

    try {

        jsonFileParser.parseJsonData();

    } catch (JSONException e) {

        e.printStackTrace();

    }

    listAdapter = new ExpandableListViewAdapter(this,
            listDataPLCategoryHeader, listDataPLPhoneHeader);

    expandableListView.setAdapter(listAdapter);
}

private void getPLCategoryDB() {

    DevOpenHelper plcategoryHelper = new DaoMaster.DevOpenHelper(this,
            "plcategory-db", null);
    plcategorydb = plcategoryHelper.getWritableDatabase();
    plcategoryDaoMaster = new DaoMaster(plcategorydb);
    plcategoryDaoSession = plcategoryDaoMaster.newSession();
    plCatogoryDao = plcategoryDaoSession.getPLCategoryDao();

}

private void getPLPhoneDB() {

    DevOpenHelper plphoneHelper = new DaoMaster.DevOpenHelper(this,
            "plphone-db", null);
    plphonedb = plphoneHelper.getWritableDatabase();
    plphoneDaoMaster = new DaoMaster(plphonedb);
    plphoneDaoSession = plphoneDaoMaster.newSession();
    plphoneDao = plphoneDaoSession.getPLPhoneDao();

}

    private void dataToAdapter() {

-       getPLCategory();

        listDataPLCategoryHeader = new ArrayList<String>();
        listDataPLCategoryHeader.add(PLCategoryDao.Properties.Name.columnName);
-       

-       getPLPhone();
-       
-       listDataPLPhoneHeader = new ArrayList<ArrayList<Phone>>();
-       listDataPLPhonePhone = new ArrayList<Phone>();
+       getPLPhoneDB();
+
+       listDataPLPhoneHeader = new ArrayList<ArrayList<PhoneValueGetter>>();
+       listDataPLPhonePhone = new ArrayList<PhoneValueGetter>();

        String columnPLPhoneName = PLPhoneDao.Properties.Name.columnName;
        String columnPLPhonePhone = PLPhoneDao.Properties.Phone.columnName;
        String columnPLPhoneVisible = PLPhoneDao.Properties.Visible.columnName;

-       listDataPLPhonePhone.add(new Phone(columnPLPhoneName,
+       listDataPLPhonePhone.add(new PhoneValueGetter(columnPLPhoneName,
                columnPLPhonePhone));
        listDataPLPhoneHeader.add(listDataPLPhonePhone);

ExpandableListViewAdapter

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

private Context _context;

ArrayList<String> _listDataHeader;
ArrayList<ArrayList<PhoneValueGetter>> _listDataChild;

public ExpandableListViewAdapter(Context context,
        ArrayList<String> listDataPLCategoryHeader,
        ArrayList<ArrayList<PhoneValueGetter>> listDataPLPhoneHeader) {
    this._context = context;
    this._listDataHeader = listDataPLCategoryHeader;
    this._listDataChild = listDataPLPhoneHeader;

}

@Override
public Object getChild(int groupPosition, int childPosition) {

    // return
    // this._listDataChild.get(this._listDataHeader.get(groupPosition))
    // .get(childPosition);

    return this._listDataChild.get(groupPosition).get(childPosition);

}

@Override
public long getChildId(int groupPosition, int childPosition) {

    return childPosition;

}

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

    final PhoneValueGetter childText = (PhoneValueGetter) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_item, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.listItem);
    txtListChild.setText(childText.getName());

    TextView txtListChild2 = (TextView) convertView
            .findViewById(R.id.listItem2);
    txtListChild2.setText(childText.getPhone());

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    // return
    // this._listDataChild.get((this._listDataHeader).get(groupPosition)).size();

    return this._listDataChild.get(groupPosition).size();
}

@Override
public Object getGroup(int groupPosition) {

    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {

    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {

    return groupPosition;
}

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

    String headerTitle = (String) getGroup(groupPosition);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_group, null);

        TextView listHeader = (TextView) convertView
                .findViewById(R.id.listHeader);
        listHeader.setText(headerTitle);
    }

    return convertView;
}

@Override
public boolean hasStableIds() {

    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {

    return true;
}

}

War es hilfreich?

Lösung

You have to use one single schema in one database to use the built-in relation-mechanics of greendao.

Since you didn't post your schema I can't tell whether you are using a single schema or not or if your schema is containing errors, but you are definitly using two database-files:

database "plcategory-db":

DevOpenHelper plcategoryHelper = new DaoMaster.DevOpenHelper(this,
        "plcategory-db", null);

and database "plphone-db":

DevOpenHelper plphoneHelper = new DaoMaster.DevOpenHelper(this,
        "plphone-db", null);

Also notice the following:

  1. Initialization of databases should not occur in the main thread as it may be a lenghty oeration and block the UI.
  2. DevOpenHelpershould be used in development only. I even recommend to not using it at all, but to extend OpenHelper directly. This way your schema-update-mechanism will be well tested ;)

To reset your adapter you can use the following code:

public void set(ArrayList<String> listDataPLCategoryHeader, ArrayList<ArrayList<PhoneValueGetter>> listDataPLPhoneHeader) {
    this._listDataHeader = listDataPLCategoryHeader;
    this._listDataChild = listDataPLPhoneHeader;
    notifyDataSetChanged();
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top