• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Help needed with button in ExpandableListAdapter

feichter

Lurker
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:
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;
}
 
Not sure what your question is, but yes it's possible to start an Activity when your button is clicked.
 
The question is: how can a button click event 'carry' the id number ONLY from my json response and open a new activity so that I can use this id in that new activity?
Sorry, if this is not clear.
 
Thank you LV426, I had seen the 'Intent' option, but can not for the life of me figure out how to implement it in my code.

I would have to add it here, I think:

Code:
if("".equals(a.getString("bought_by"))) {
                        members.add(id + " " + present);
                    }else{
                        members.add(id + " " + present + "\r\n bought by: " + a.getString("bought_by"));
                    }
but this would create a lot of intents, wouldn't it?
 
If you want the new Activity to be started when you click the button, then the code has to be placed in the button click listener onClick method.
 
Thanks LV426, but if you look at my code, you'll see that the button ('markAsBoughtButton') is there and it toasts the whole child entry. But I would like the button to start a new activity with ONLY the id (see MainActivity line 38 - 42).
 
Please clarify how you are going to start new Activity in the lines 38-42. This is background task, that fills members list with a bunch of IDs.
Then you put this list of ids into local Map and do nothing.

The question is how you are going to start activity in the main thread with the data stored in the local map of background task. According your code map is lost when background task is finished.

Please clarify your idea.
 
Hello v777779.

Let me try again to explain what I am trying to achieve.

In ExpandableListAdapter I have the setOnClickListener bound to markAsBoughtButton. This is currently returning a 'Toast' with the id and item name returned from the mysql database.
What I really need, instead of the 'Toast', is for the the button click to open a new activity with the id from the mysql database only, so that in that activity I can then perform other actions with this id (for example update the database, send email, etc.).

Any help greatly appreciated.
 
If you want to start a new Activity when you click the markAsBoughtButton, then the code to do that must be placed in the onClick() method shown below.
If you are unsure how to create a new Activity, look at the link in post #4

Code:
    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();
        }
    });
 
OK, before start, let me ask about activity registration.
You should register all activities in Manifest. Imagine you have 100 records in the Database.
How you are going to register Activities? Will you register all of them in Manifest.
 
If you want to start a new Activity when you click the markAsBoughtButton, then the code to do that must be placed in the onClick() method shown below.
If you are unsure how to create a new Activity, look at the link in post #4

Code:
    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();
        }
    });

I know I've got to place it in the onClick event, but I don't know how! And how to attach the id from the database.
 
You can store in database just indices 1,2,3,4 and so on and call method with switch() operator, which starts Activity according to index.
But as I said before you need to register all activities.
 
Back
Top Bottom