mercredi 16 septembre 2015

ExpandableListView data mix up

I created an expandable listview with data coming from my sqlite database. apparently after making 5 row. 1 doesn't work, it will display blank and will display the content in a different section. I tested out if i incorrectly made a mistake on the input by using a Toast message but all data seems to be fine. So i think the problem might be in my Activity Class or Adapter. Here is my code about the Activity Class

public class DisplayContent extends Activity {

private static final float BYTES_PER_PX = 4.0f;

HashMap<String, List<String>> Content_category;
List<String> Content_list;
ExpandableListView Exp_list;
ContentAdapter adapter;

/* Front Content*/
ImageView image;
TextView name, desc;
CheckBox cb;


Context context;
DbHerbs myDb;

String getThis;
long conID;
int imgId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.display_content);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    testConnection();
    setViewId();
    getContent();
    }



private void getContent()
{
    getThis = getIntent().getStringExtra(TabOne.GET_THIS);
    conID = Long.parseLong(getThis);

    String conFav = myDb.getFav(conID);
    imgId = myDb.getImage(conID);
    loadImage();

    String conName = myDb.getName(conID);
    String conAltName = myDb.getAltName(conID);

    /*Send to expandable list view*/
    String conDesc = myDb.getDescription(conID);
    String conUses = myDb.getUses(conID);
    String conPartsUsed = myDb.getPartsUsed(conID);
    String conApplications= myDb.getApplications(conID);
    String conSideEffects= myDb.getSideEffects(conID);


    Content_category = getInfo(conDesc, conUses, conPartsUsed, conApplications, conSideEffects);
    Content_list = new ArrayList<String>(Content_category.keySet());
    adapter = new ContentAdapter(this, Content_category, Content_list);
    Exp_list.setAdapter(adapter);


    if(conFav.equals("1"))
    {
    cb.setChecked(true);
    }       
    closeDb();

    name.setText(conName);
    desc.setText(conAltName);
}



public HashMap<String, List<String>> getInfo(String description, String uses, String partsuse, 
        String application, String sideeffects)
{
    HashMap<String, List<String>> ContentDetails = new HashMap<String, List<String>>();

    List<String> ContentDesc = new ArrayList<String>();
    ContentDesc.add(description);

    List<String> ContentUses = new ArrayList<String>();
    ContentUses.add(uses);


    List<String> ContentPartsUse= new ArrayList<String>();
    ContentPartsUse.add(partsuse);


    List<String> ContentApplication = new ArrayList<String>();
    ContentApplication.add(application);

    List<String> ContentSideEffects = new ArrayList<String>();
    ContentApplication.add(sideeffects);

    ContentDetails.put("Applications", ContentApplication);
    ContentDetails.put("Side Effects", ContentSideEffects);
    ContentDetails.put("Description", ContentDesc);
    ContentDetails.put("Uses", ContentUses);
    ContentDetails.put("Parts Used", ContentPartsUse);

    return ContentDetails;

}

And the adapter class. I just got the tutorial from youtube. just changed it a little and tried to work with it.

public class ContentAdapter extends BaseExpandableListAdapter{
private Context ctx;
private HashMap<String, List<String>> Content_Category;
private List<String> Content_List;

public ContentAdapter(Context ctx, HashMap<String, List<String>> Content_Category, List<String> Content_List )
{
    this.ctx = ctx;
    this.Content_Category = Content_Category;
    this.Content_List = Content_List;

}

@Override
public Object getChild(int parent, int child) {


    return Content_Category.get(Content_List.get(parent)).get(child);
}

@Override
public long getChildId(int parent, int child) {
    // TODO Auto-generated method stub
    return child;
}

@Override
public View getChildView(int parent, int child, boolean lastChild, View convertview,
        ViewGroup parentview) 
 {
    String child_title =  (String) getChild(parent, child);
    if(convertview == null)
    {
        LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertview = inflator.inflate(R.layout.custom_child_layout, parentview,false);
    }
    TextView child_textview = (TextView) convertview.findViewById(R.id.child_txt);
    child_textview.setText(child_title);


    return convertview;
}

@Override
public int getChildrenCount(int arg0) {

    return Content_Category.get(Content_List.get(arg0)).size();
}

@Override
public Object getGroup(int arg0) {
    // TODO Auto-generated method stub
    return Content_List.get(arg0);
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return Content_List.size();
}

@Override
public long getGroupId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public View getGroupView(int parent, boolean isExpanded, View convertview, ViewGroup parentview) {
    // TODO Auto-generated method stub
    String group_title = (String) getGroup(parent);
    if(convertview == null)
    {
        LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertview = inflator.inflate(R.layout.custom_parent_layout, parentview,false);
    }
    TextView parent_textview = (TextView) convertview.findViewById(R.id.parent_txt);
    parent_textview.setTypeface(null, Typeface.BOLD);
    parent_textview.setText(group_title);
    return convertview;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean isChildSelectable(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return false;
} 

}

Aucun commentaire:

Enregistrer un commentaire