[Android Studio] anish555:
How to add image and text to both parentgroup and childgroup of expandablelistview dynamically from database.T his code uses expendable listview. I want to display image and text to parentgroup and childgroup of expendablelistview dynamically from database. i am a newbie in android development so please some one help me out.

This is my code:

public class CustomListAdapter15 extends BaseExpandableListAdapter {

List<HashMap<String, Object>> models, models2;

Context context;

LayoutInflater inflater;

ViewHolder viewHolder;

View view1;

// JSON Node names

private static final String TAG_SUCCESS = "success";

SharedPreferences sPref;

public CustomListAdapter15(Context context, List<HashMap<String, Object>> models2, List<HashMap<String, Object>> models) {

this.context = context;

this.models2 = models2; //parentItems

this.models = models; //childtems

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

this.counters = new int[30];

//this.session_email = sPref.getString("SESSION_UID","");

}

public class ViewHolder {

public TextView countt = null;

public ImageView likem = null, createButton;

//RelativeLayout rel, rel2;

LinearLayout rel;

private Button btnSend;

}

public void clear() {

if (models != null)

models.clear();

}

public int getItemViewType(int position) {

// TODO Auto-generated method stub

return position;

}

[MENTION=439709]override[/MENTION]

public Object getChild(int groupPosition, int childPosition) {

//return null;

//return models.get((HashMap<String, Object>)models2.get(groupPosition)).get(childPosition);

return models.get(groupPosition).get(childPosition);

}

[MENTION=439709]override[/MENTION]

public long getChildId(int groupPosition, int childPosition) {

//return 0;

return childPosition;

}

[MENTION=439709]override[/MENTION]

public int getChildrenCount(int groupPosition) {

return ((HashMap<String, Object>) models.get(groupPosition)).size();

//return models.size();

}

[MENTION=439709]override[/MENTION]

public Object getGroup(int groupPosition) {

//return null;

return models2.get(groupPosition);

}

[MENTION=439709]override[/MENTION]

public int getGroupCount() {

return models2.size();

}

[MENTION=439709]override[/MENTION]

public void onGroupCollapsed(int groupPosition) {

super.onGroupCollapsed(groupPosition);

}

[MENTION=439709]override[/MENTION]

public void onGroupExpanded(int groupPosition) {

super.onGroupExpanded(groupPosition);

}

[MENTION=439709]override[/MENTION]

public long getGroupId(int groupPosition) {

//return 0;

return groupPosition;

}

[MENTION=439709]override[/MENTION]

public boolean hasStableIds() {

return true;

}

[MENTION=439709]override[/MENTION]

public boolean isChildSelectable(int groupPosition, int childPosition) {

return true;

}

public HashMap<String, Object> getItem(int position) {

return models.get(position);

}

public HashMap<String, Object> getItem1(int position2) {

return models2.get(position2);

}

/ [MENTION=439709]override[/MENTION]

public long getChildId(int listPosition, int expandedListPosition) {

return expandedListPosition;

}*/

[MENTION=439709]override[/MENTION]

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

View view = convertView;

final ViewHolder viewHolder;

if (view == null) {

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

view = inflater.inflate(R.layout.chat_right, null);

/*if(session_ph.equals((sphon)))

{

view = inflater.inflate(R.layout.chat_right, parent, false);

}

else if(!session_ph.equals((sphon)))

{

view = inflater.inflate(R.layout.chat_left, parent, false);

}*/

//view.setBackgroundColor(color_arr[pos]);

viewHolder = new ViewHolder();

/* viewHolder.gppho=(TextView)view.findViewById(R.id.gphn);

viewHolder.gadminph=(TextView)view.findViewById(R.id.gadphn);

viewHolder.rel = (LinearLayout)view.findViewById(R.id.comrel);*/

gppho = (TextView) view.findViewById(R.id.gphn);

gadminph = (TextView) view.findViewById(R.id.gadphn);

viewHolder.rel = (LinearLayout) view.findViewById(R.id.comrel);

// rel1 = (LinearLayout)view.findViewById(R.id.comrel);

/* HashMap<String, Object> item = models.get(position);

gppho.setText((CharSequence) item.get("SPHON"));

gadminph.setText((CharSequence) item.get("GAPHON"));

String gadm_ph=gadminph.getText().toString();

String sendersphon=gppho.getText().toString();

RelativeLayout.LayoutParams layoutParams =

(RelativeLayout.LayoutParams) rel1.getLayoutParams();

if(gadm_ph.equals((sendersphon)))

{

layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);

rel1.setLayoutParams(layoutParams);

rel1.setBackground(getResources().getDrawable(R.drawable.bubble2_out));

//v1iew = inflater.inflate(R.layout.chat_right, parent, false);

}

else if(!gadm_ph.equals((sendersphon)))

{

layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);

rel1.setLayoutParams(layoutParams);

rel1.setBackground(getResources().getDrawable(R.drawable.bubble1_in));

//view = inflater.inflate(R.layout.chat_left, parent, false);

}*/

viewHolder.rel.setOnLongClickListener(new View.OnLongClickListener() {

[MENTION=439709]override[/MENTION]

public boolean onLongClick(View v) {

// TODO Auto-generated method stub

int position = (Integer) v.getTag();

HashMap<String, Object> item = models2.get(position);

gppho.setText((CharSequence) item.get("SPHON"));

gadminph.setText((CharSequence) item.get("GAPHON"));

String gadm_ph1 = gadminph.getText().toString();

String sendersphon1 = gppho.getText().toString();

gpida.setText((CharSequence) item.get("GID"));

pacmos = gpida.getText().toString();

Toast.makeText(Group_create_view_adm.this, gadm_ph1, Toast.LENGTH_SHORT).show();

Toast.makeText(Group_create_view_adm.this, sendersphon1, Toast.LENGTH_SHORT).show();

// removeItemFromList();

return true;

}

});

view.setTag(viewHolder);

} else {

viewHolder = (ViewHolder) view.getTag();

}

viewHolder.rel.setTag(childPosition);

final HashMap<String, Object> item = getItem(childPosition);

/* name.setText(((String)item.get(R.id.name)));

*

*/

gpida = (TextView) view.findViewById(R.id.gid);

gpnam = (TextView) view.findViewById(R.id.gnam);

gadminph = (TextView) view.findViewById(R.id.gadphn);

gpema = (TextView) view.findViewById(R.id.gema);

gppho = (TextView) view.findViewById(R.id.gphn);

namesa = (TextView) view.findViewById(R.id.senname);

msgsa = (TextView) view.findViewById(R.id.txt_msg);

tims = (TextView) view.findViewById(R.id.sentime);

imga = (ImageView) view.findViewById(R.id.gperimg);

gpida.setText((CharSequence) item.get("GID"));

gpnam.setText((CharSequence) item.get("GNAME"));

gadminph.setText((CharSequence) item.get("GAPHON"));

gpema.setText((CharSequence) item.get("SEMAIL"));

gppho.setText((CharSequence) item.get("SPHON"));

namesa.setText((CharSequence) item.get("SNAME"));

msgsa.setText((CharSequence) item.get("SCOMM"));

tims.setText((CharSequence) item.get("STIM"));

Picasso.with(context)

//.load("PIC")

.load((String) item.get("GPRIMG"))

// .centerCrop()

// .fit()

.into(imga);

rel1 = (LinearLayout) view.findViewById(R.id.comrel);

gadm_ph = gadminph.getText().toString();

sendersphon = gppho.getText().toString();

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

if (session_ph.equals((sendersphon))) {

layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);

rel1.setLayoutParams(layoutParams);

//rel1.setGravity(Gravity.RIGHT);

rel1.setBackground(getResources().getDrawable(R.drawable.bubble2_out));

//v1iew = inflater.inflate(R.layout.chat_right, parent, false);

} else if (!session_ph.equals((sendersphon))) {

layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

rel1.setLayoutParams(layoutParams);

// rel1.setGravity(Gravity.LEFT);

rel1.setBackground(getResources().getDrawable(R.drawable.bubble1_in));

//view = inflater.inflate(R.layout.chat_left, parent, false);

}

if(childPosition == getChildrenCount(groupPosition)-1)

{

view = inflater.inflate(R.layout.chat_left,null);

viewHolder.btnSend =(Button)view.findViewById(R.id.btn_chat_send);

editText = (EditText)view.findViewById(R.id.msg_type);

//TextView txtFooter = (TextView)view.findViewById(R.id.txtFooter);

//txtFooter.setText(currentParent.textToFooter);

}

viewHolder.btnSend.setOnClickListener(new View.OnClickListener(){

[MENTION=439709]override[/MENTION]

public void onClick(View v) {

int position = (Integer) v.getTag();

if (editText.getText().toString().trim().equals("")) {

Toast.makeText(Group_create_view_adm.this, "Please input some text...", Toast.LENGTH_LONG).show();

} else {

HashMap<String, Object> item = models.get(position);

pho.setText((CharSequence) item.get("PHON"));

pacm11=pho.getText().toString();

msg=editText.getText().toString();

new LoadAllProducts95().execute();

}

}

});

return view;

}

[MENTION=439709]override[/MENTION]

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

view1 = convertView;

if(view1==null)

{

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

view1 = inflater.inflate(R.layout.groupss_view_row, null);

//view.setBackgroundColor(color_arr[pos]);

viewHolder = new ViewHolder();

viewHolder.createButton = (ImageView)view1.findViewById(R.id.pic123df);

viewHolder.createButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

int position = (Integer) v.getTag();

HashMap<String, Object> item = models.get(position);

pho.setText((CharSequence) item.get("PHON"));

pacm=pho.getText().toString();

Intent intent = new Intent(context, Friendsprofile2.class);

intent.putExtra("TITLE", pacm);

//Intent putExtra = intent.putExtra("title", item.get(pac));

//intent.putExtra("image", item.getImage());

//Start details activity

context.startActivity(intent);

}

});

view1.setTag(viewHolder);

}

else {

viewHolder = (ViewHolder) view1.getTag();

}

viewHolder.createButton.setTag(groupPosition);

// viewHolder.rel.setTag(position);

//viewHolder.rel2.setTag(position);

final HashMap<String, Object> item = getItem1(groupPosition);

/* name.setText(((String)item.get(R.id.name)));

*

*/

// like = (ImageView) view.findViewById(R.id.like);

//sharingButton = (ImageButton) view.findViewById(R.id.share);

//text =(TextView)view.findViewById(R.id.text123);

name=(TextView)view1.findViewById(R.id.name);

idam=(TextView)view1.findViewById(R.id.id);

pho=(TextView)view1.findViewById(R.id.phon);

pic =(ImageView)view1.findViewById(R.id.pic123df);

bio=(TextView)view1.findViewById(R.id.bio);

//gtot=(TextView)view.findViewById(R.id.gptat);

name.setText((CharSequence) item.get("NAME"));

idam.setText((CharSequence) item.get("IDAC"));

pho.setText((CharSequence) item.get("PHON"));

bio.setText((CharSequence) item.get("BIOK"));

//gtot.setText((CharSequence) item.get("GPTAT"));

// text.setText((CharSequence) item.get("TEXT"));

//rel=(RelativeLayout)view.findViewById(R.id.frpos);

sapar=(TextView)view1.findViewById(R.id.sap);

gree = (ImageView)view1.findViewById(R.id.green);

sapar.setText((CharSequence) item.get("STAT"));

stato=sapar.getText().toString();

phoneno=pho.getText().toString();

//abc=Integer.parseInt(stato);

if((stato.equals(1)))

{

// Toast.makeText(context, "abc", Toast.LENGTH_LONG).show();

Picasso.with(context)

//.load("PIC")

.load(R.mipmap.green)

.resize(30, 30)

// .centerCrop()

// .fit()

.into(gree);

// Toast.makeText(context, phoneno, Toast.LENGTH_LONG).show();

}

else if(!stato.equals(1))

{

// Toast.makeText(context, "koll", Toast.LENGTH_LONG).show();

Picasso.with(context)

//.load("PIC")

.load(R.mipmap.gray)

.resize(30, 30)

// .centerCrop()

// .fit()

.into(gree);

}

Picasso.with(context)

//.load("PIC")

.load((String)item.get("PIC"))

.resize(150, 150)

// .centerCrop()

// .fit()

.into(pic);

return view1;

}

}