Hi All
This is my first post and I am fairly new to java, so please excuse this basic question.
I am developing an app with expandable list adapter which gets it's data from a mysql database via json.
This is what I have so far:
All the group items and child items display ok.
I have a button on some child items, which is normally hidden, but made visible when the json string includes 'Mark as bought'. This is working fine.
But I now need this button, which when pressed, calls another activity with the id number (only!) of the item from the database. At the moment I have a 'Toast' which displays the entire string.
I think I am barking up the wrong tree somewhere and my approach may need to be completely different, but I am stuck!
Any help greatly appreciated!
In ExpandableListAdapter:
In MainActivity:
This is my first post and I am fairly new to java, so please excuse this basic question.
I am developing an app with expandable list adapter which gets it's data from a mysql database via json.
This is what I have so far:
All the group items and child items display ok.
I have a button on some child items, which is normally hidden, but made visible when the json string includes 'Mark as bought'. This is working fine.
But I now need this button, which when pressed, calls another activity with the id number (only!) of the item from the database. At the moment I have a 'Toast' which displays the entire string.
I think I am barking up the wrong tree somewhere and my approach may need to be completely different, but I am stuck!
Any help greatly appreciated!
In ExpandableListAdapter:
Code:
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
//listDataChild
final Button markAsBoughtButton = convertView.findViewById(R.id.BtnToClick);
// set 'Mark as bought' button to visible
if(childText.contains("bought by")){
markAsBoughtButton.setVisibility(View.GONE);
}else{
markAsBoughtButton.setVisibility(View.VISIBLE);
}
final TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
markAsBoughtButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
LinearLayout rl = (LinearLayout) v.getParent();
TextView tv1 = (TextView)rl.findViewById(R.id.lblListItem);
String Name = tv1.getText().toString();
Toast.makeText(_context, Name+" Button is clicked", Toast.LENGTH_LONG).show();
}
});
txtListChild.setText(childText);
return convertView;
}
In MainActivity:
Code:
@Override
protected Void doInBackground(Void... arg0) {
JSONParser jp = new JSONParser();
String jsonstr = jp.makeServiceCall(url);
Log.d("Response = ", jsonstr);
if (jsonstr != null) {
// For Header title Arraylist
listDataHeader = new ArrayList<String>();
// Hashmap for child data key = header title and value = Arraylist (child data)
listDataChild = new HashMap<String, List<String>>();
try {
JSONObject jobj = new JSONObject(jsonstr);
JSONArray jarray = jobj.getJSONArray("presents_db");
for (int hk = 0; hk < jarray.length(); hk++) {
JSONObject d = jarray.getJSONObject(hk);
// Adding Header data
listDataHeader.add(d.getString("name") + "'s presents are: ");
// Adding child data for members
List<String> members = new ArrayList<String>();
JSONArray xarray = d.getJSONArray("presents_list");
Button tv[] = new Button[xarray.length()];
for (int i = 0; i < xarray.length(); i++) {
JSONObject a = xarray.getJSONObject(i);
String id = a.getString("id");
String present = a.getString("present");
if("".equals(a.getString("bought_by"))) {
members.add(id + " " + present);
}else{
members.add(id + " " + present + "\r\n bought by: " + a.getString("bought_by"));
}
}
// Header into Child data
listDataChild.put(listDataHeader.get(hk), members);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(),"Please Check internet Connection", Toast.LENGTH_SHORT).show();
}
return null;
}