[英]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.
但是我现在需要将id添加到此按钮。 Please see below.
请看下面。
ExpandableListView.java 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 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. 不好意思的编码表示歉意,但是我对Java还是陌生的,这是一条陡峭的曲线。 Thanks for your help!
谢谢你的帮助!
You can use setTag()
and getTag()
您可以使用
setTag()
和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? View的setTag()getTag()方法的主要目的是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.