我正在使用ExpandableListView。我在子列表元素上有按钮,单击时会更改列表元素的背景颜色。遇到的问题: 多个列表元素在不同的列表标题中执行。
适配器:
public class DashHomeAdapter extends BaseExpandableListAdapter {
private Context _context;
private ArrayList<OrderHeader> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<OrderHeader, ArrayList<FoodItem>> _listDataChild;
ParseQuery<ParseObject> query;
HashMap<Integer, String> map;
public DashHomeAdapter(Context context, ArrayList<OrderHeader> listDataHeader,
HashMap<OrderHeader, ArrayList<FoodItem>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
map = new HashMap<>();
}
@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 FoodItem child = (FoodItem) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.content_order, null);
ChildViewHolder viewHolder = new ChildViewHolder(convertView);
convertView.setTag(viewHolder);
}
final ChildViewHolder viewHolder = (ChildViewHolder) convertView.getTag();
viewHolder.nameTextView.setText(child.getName());
viewHolder.priceTextView.setText(child.getPrice());
viewHolder.quantityTextView.setText("x" + Integer.toString(child.getCount()));
viewHolder.commentsTextView.setText(child.getComments());
map.put(childPosition, "Inititalized");
viewHolder.acceptOrderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (map.get(childPosition).equals("Initialized")) {
final ProgressDialog pgDialog = new ProgressDialog(_context);
pgDialog.setMessage("Please Wait..");
pgDialog.show();
query = ParseQuery.getQuery(child.getId());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
pgDialog.hide();
for (int i = 0; i < objects.size(); i++) {
String refId = objects.get(i).getString("ID");
if (refId.equals(child.getOrderId())) {
objects.get(i).put("Status", "Order Accepted");
objects.get(i).saveInBackground();
viewHolder.childCardView.setBackgroundColor(Color.GREEN);
viewHolder.acceptOrderButton.setVisibility(View.INVISIBLE);
viewHolder.rejectOrderButton.setVisibility(View.INVISIBLE);
viewHolder.serveOrderButton.setVisibility(View.VISIBLE);
}
}
}
});
}
}
});
viewHolder.rejectOrderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (map.get(childPosition).equals("Initialized")) {
final ProgressDialog pgDialog = new ProgressDialog(_context);
pgDialog.setMessage("Please Wait..");
pgDialog.show();
query = ParseQuery.getQuery(child.getId());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
pgDialog.hide();
for (int i = 0; i < objects.size(); i++) {
String refId = objects.get(i).getString("ID");
if (refId.equals(child.getOrderId())) {
objects.get(i).put("Status", "Order Rejected");
objects.get(i).saveInBackground();
viewHolder.childCardView.setBackgroundColor(Color.RED);
viewHolder.acceptOrderButton.setVisibility(View.INVISIBLE);
viewHolder.rejectOrderButton.setVisibility(View.INVISIBLE);
}
}
}
});
}
}
});
viewHolder.serveOrderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (map.get(childPosition).equals("Initialized")) {
final ProgressDialog pgDialog = new ProgressDialog(_context);
pgDialog.setMessage("Please Wait..");
pgDialog.show();
query = ParseQuery.getQuery(child.getId());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
pgDialog.hide();
for (int i = 0; i < objects.size(); i++) {
String refId = objects.get(i).getString("ID");
if (refId.equals(child.getOrderId())) {
objects.get(i).put("Status", "Order Done");
objects.get(i).saveInBackground();
viewHolder.childCardView.setBackgroundColor(Color.GRAY);
viewHolder.serveOrderButton.setVisibility(View.INVISIBLE);
}
}
}
});
}
}
});
return convertView;
}
static class ChildViewHolder {
TextView nameTextView;
TextView priceTextView;
TextView quantityTextView;
TextView commentsTextView;
AppCompatButton rejectOrderButton;
AppCompatButton acceptOrderButton;
AppCompatButton serveOrderButton;
CardView childCardView;
public ChildViewHolder(View view) {
nameTextView = (TextView) view.findViewById(R.id.nameTextView);
priceTextView = (TextView) view.findViewById(R.id.priceTextView);
quantityTextView = (TextView) view.findViewById(R.id.quantityTextView);
commentsTextView = (TextView) view.findViewById(R.id.commentsTextView);
acceptOrderButton = (AppCompatButton) view.findViewById(R.id.acceptOrderButton);
rejectOrderButton = (AppCompatButton) view.findViewById(R.id.rejectOrderButton);
serveOrderButton = (AppCompatButton) view.findViewById(R.id.serveOrderButton);
childCardView = (CardView) view.findViewById(R.id.childCardView);
}
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final OrderHeader headerTitle = (OrderHeader) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.header_order, null);
ParentViewHolder viewHolder = new ParentViewHolder(convertView);
convertView.setTag(viewHolder);
}
ParentViewHolder viewHolder = (ParentViewHolder) convertView.getTag();
viewHolder.userNameTextView.setTypeface(null, Typeface.BOLD);
viewHolder.userNameTextView.setText("Username: " + headerTitle.getUsername());
viewHolder.dateTimeStampTextView.setTypeface(null, Typeface.BOLD);
viewHolder.dateTimeStampTextView.setText(headerTitle.getDateTimeStamp());
viewHolder.orderNumberTextView.setTypeface(null, Typeface.BOLD);
viewHolder.orderNumberTextView.setText("OrderNo: " + headerTitle.getOrderNo());
return convertView;
}
static class ParentViewHolder {
TextView userNameTextView;
TextView dateTimeStampTextView;
TextView orderNumberTextView;
public ParentViewHolder(View view) {
userNameTextView = (TextView) view
.findViewById(R.id.usernameTextView);
dateTimeStampTextView = (TextView) view
.findViewById(R.id.dateTimeStampTextView);
orderNumberTextView = (TextView) view
.findViewById(R.id.orderNumberTextView);
}
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
我甚至试图通过使用HashMap从另一个类似问题实现接受的解决方案,但它没有用。提前谢谢。