简体   繁体   中英

Android expandableListView add variable to a button

Hope you can help. I need to add a variable to a button which I make visible depending on a value which may or may not be present in the database. This is working fine. But I now need to add the id to this button. Please see below.

ExpandableListView.java

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosititon);
}

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

@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);
    }

    final TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);

    //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);
    }

    markAsBoughtButton.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View view) {
             View parentRow = (View) view.getParent();
             ListView listView = (ListView) parentRow.getParent();
             final int pos = listView.getPositionForView(parentRow);

             Toast.makeText(_context, "Bought! " + " Position " + pos, Toast.LENGTH_LONG).show();
         }
    });

    txtListChild.setText(childText);
    return convertView;
}

etc.

MainActivity.java

public class MainActivity extends ProfileActivity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;

private ProgressDialog mprocessingdialog;
private static String url = "http://www.xxx/xxxx/xxxx/xxxx.php";
public static String id;

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

    // added to include custom icon
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setIcon(ic_launcher);

    mprocessingdialog = new ProgressDialog(this);

    // get the listview
    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    // preparing list data
    new DownloadJason().execute();
}

/*
 * Preparing the list data
 */
private class DownloadJason extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        //Showing Progress dialog
        mprocessingdialog.setTitle("Please Wait..");
        mprocessingdialog.setMessage("Loading");
        mprocessingdialog.setCancelable(false);
        mprocessingdialog.setIndeterminate(false);
        mprocessingdialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub

        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");
                    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;
    }

Apologies for the bad coding but I am new to java and it's a steep learing curve. Thanks for your help!

You can use setTag() and getTag()

//Set tag
button.setTag(position);

//Get Tag 
new OnClickListener() {
@Override
public void onClick(View v) {
    int pos = v.getTag(); 
//Get Tag work on view like button
}

What is the main purpose of setTag() getTag() methods of View?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM