如何在ListAdapter中获取特定子按钮的引用
问题描述:
我有一个扩展了BaseExpandableListAdapter
的类,并且我在它的每个组上都创建了一个按钮。共有6个组。在getChildView
的适配器上,我想更改特定位置(例如2)上的按钮文本。如何在ListAdapter中获取特定子按钮的引用
我听说过的标签功能,但我不知道如何使用它我的情况。如何在specific child
上更改button text
?
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final int position = groupPosition + 1;
switch (position) {
case 1:
allQuestions = MyConstants.QUESTIONS_1;
break;
case 2:
allQuestions = MyConstants.QUESTIONS_2;
break;
case 3:
allQuestions = MyConstants.QUESTIONS_3;
break;
case 4:
allQuestions = MyConstants.QUESTIONS_4;
break;
case 5:
allQuestions = MyConstants.QUESTIONS_5;
break;
case 6:
allQuestions = MyConstants.QUESTIONS_6;
break;
}
int correctQuestions = MyDatabase.getInstance(context).getCorrectQuestions(position).getCount();
int wrongQuestions = MyDatabase.getInstance(context).getWrongQuestions(position).getCount();
int openQuestions = allQuestions - correctQuestions - wrongQuestions;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
tv_questionOpenResult = (TextView) convertView.findViewById(R.id.tv_questionOpenResult);
tv_correctAmountResult = (TextView) convertView.findViewById(R.id.tv_correctAmountResult);
tv_wrongAmountResult = (TextView) convertView.findViewById(R.id.tv_wrongAmountResult);
bt_openQuestions = (Button) convertView.findViewById(R.id.bt_openQuestions);
bt_openQuestions.setTag(position);
bt_correctQuestions = (Button) convertView.findViewById(R.id.bt_correctQuestions);
bt_wrongQuestions = (Button) convertView.findViewById(R.id.bt_wrongQuestions);
if (allQuestions == correctQuestions) {
bt_openQuestions.setEnabled(true);
bt_openQuestions.setText("Test");
allCorrect = true;
}
}
答
package com.example.adapter;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import com.example.R;
import com.example.model.filter_Item;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class Filter_ExpandableAdapter extends BaseExpandableListAdapter {
private Context _context;
ArrayList<String> _listDataHeader;
HashMap<Integer, ArrayList<filter_Item>> list_child = new HashMap<>();
private HashMap<Integer, boolean[]> mChildCheckStates;
public static ArrayList<filter_Item> selected_list_child = new ArrayList<>();
private ChildViewHolder childViewHolder;
public Filter_ExpandableAdapter(Context _context, ArrayList<String> _listDataHeader, HashMap<Integer, ArrayList<filter_Item>> list_child) {
this._context = _context;
this._listDataHeader = _listDataHeader;
this.list_child = list_child;
mChildCheckStates = new HashMap<Integer, boolean[]>();
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return list_child.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return list_child.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.header_row_filter, null);
}
TextView lblListHeader = (TextView) convertView.findViewById(R.id.filter_name);
lblListHeader.setText(_listDataHeader.get(groupPosition).toString());
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final int mGroupPosition = groupPosition;
final int mChildPosition = childPosition;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_row_filter, null);
childViewHolder = new ChildViewHolder();
childViewHolder.select_item = (CheckBox) convertView.findViewById(R.id.select_checkbox);
convertView.setTag(R.layout.child_row_filter, childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag(R.layout.child_row_filter);
}
childViewHolder.select_item.setText("" + list_child.get(groupPosition).get(childPosition).getLc_sub_name());
childViewHolder.select_item.setOnCheckedChangeListener(null);
if (mChildCheckStates.containsKey(mGroupPosition)) {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
childViewHolder.select_item.setChecked(getChecked[mChildPosition]);
}
else {
boolean getChecked[] = new boolean[getChildrenCount(mGroupPosition)];
mChildCheckStates.put(mGroupPosition, getChecked);
childViewHolder.select_item.setChecked(false);
}
childViewHolder.select_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
getChecked[mChildPosition] = isChecked;
mChildCheckStates.put(mGroupPosition, getChecked);
selected_list_child.add(list_child.get(groupPosition).get(childPosition));
}
else
{
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
getChecked[mChildPosition] = isChecked;
mChildCheckStates.put(mGroupPosition, getChecked);
selected_list_child.remove(list_child.get(groupPosition).get(childPosition));
}
notifyDataSetChanged();
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private class ChildViewHolder {
CheckBox select_item;
}
}
试试这个代码...