问题描述
我想在一个自定义ExpandableListView组的onclick处理。有了这个code上点击时收到组的号码:
I want to handle onclick on groups of a custom ExpandableListView. With this code I am getting the number of group when clicking on it:
exList.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
Log.i("group position", groupPosition + "");
return false;
}
});
我加入了简化code。我希望你能找出问题可能是什么:
I am adding the simplified code. I hope you can figure out what the problem might be:
public class ProductLists extends Activity {
private static final String G_TEXT = "G_TEXT";
private static final String C_TITLE = "C_TITLE";
private static final String C_TEXT = "C_TEXT";
private static final String C_CB = "C_CB";
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
List<Map<String, Boolean>> groupCheckBox = new ArrayList<Map<String,Boolean>>();
List<List<Map<String, Boolean>>> childCheckBox = new ArrayList<List<Map<String,Boolean>>>();
String loadedexpanded;
String upperString;
int number, k_num, k_num2;
String etext_newitem;
EditText et_newitem;
String CheckedItem;
String CheckedItems;
ExpandBaseAdapter adapter;
ExpandableListView exList;
HotOrNot info;
ArrayList<String> grpsfav = new ArrayList<String>();
ArrayList<String> OftenUsedAll = new ArrayList<String>();
ArrayList<String> OftenUsedID = new ArrayList<String>();
ArrayList<String> OftenUsedName = new ArrayList<String>();
ArrayList<String> OftenUsedNumber = new ArrayList<String>();
Button sqlExp, sqlAdd;
ArrayList<String> PRLists = new ArrayList<String>();
ArrayList<String> PRListsR = new ArrayList<String>();
ArrayList<String> PRListsID = new ArrayList<String>();
ArrayList<String> PRLists2 = new ArrayList<String>();
ArrayList<String> todoItems = new ArrayList<String>();
ArrayList<String> todoItemsID = new ArrayList<String>();
ArrayList<String> todoItemsNAME = new ArrayList<String>();
List<String> usable_chars = Arrays.asList(";", "'", "/", "\"", "%", "'\'", "$", "+", "-", "=", ":", "_");
ArrayList<String> todoItemsTEMP = new ArrayList<String>();
ArrayList<String> todoItemsIDTEMP = new ArrayList<String>();
ArrayList<String> todoItemsNAMETEMP = new ArrayList<String>();
ArrayList<String> inners = new ArrayList<String>();
ArrayList result2;
Button sqlView, sqlValami;
Cursor c, c2;
String newlistname, modifiedlistname;
String loadedCapital, loadedshowhints;
SharedPreferences sharedPreferences;
ExpandableListView expandlist;
DisplayMetrics metrics;
int width;
List<String> selectionList = Arrays.asList("Rename", "Delete");
List<String> selectionListA = Arrays.asList("Add item", "Rename group", "Delete group");
CharSequence[] selectionList2, selectionList2A;
String rowIdtobemodified, rowIDtobedeleted;
int groupPosition;
int childPosition;
AlertDialog.Builder builder;
Dialog dialog_newitem, dialog_newgroup;
Dialog dialog_renamelist, dialog_deletelist;
EditText et_renamelist, et_deletelist;
//String etext_renamelist, etext_deletelist;
String renamelist_name, deletelist_name;
Spinner SPProductLists3, SPProductLists32;
Button btn_rename_save, btn_rename_cancel, btn_delete_save, btn_delete_cancel;
String allerrors_newproductlistdialog;
ArrayList<String> errors_addgroup = new ArrayList<String>();
String selected_item_from_lists_torename, selected_item_from_lists_torename2;
List<String> nonusable_chars = Arrays.asList(";", "'", "/", "\"", "%", "'\'", "$", "+", "-", "=", ":", "_");
int max;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_productlists3);
exList = (ExpandableListView) findViewById(R.id.layoutExListView);
info = new HotOrNot(this);
info.open();
//----------------------------query tables--------------------------------
c = info.showAllTables();
if (c.moveToFirst())
{
do{
PRLists.add(c.getString(0));
}while (c.moveToNext());
}
if (PRLists.size() >= 0)
{
for (int i=0; i<PRLists.size(); i++)
{
Log.d("PRLists(" + i + ")", PRLists.get(i) + "");
}
}
//Declare base adapter
max = 0;
for (int i = 0; i < PRLists.size(); i++)
{
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(G_TEXT, PRLists.get(i).replaceAll("_", " ").substring(2, PRLists.get(i).replaceAll("_", " ").length()));
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
if (!PRLists.get(i).equals("PR_Often_Used"))
{
Log.i("tabla", PRLists.get(i) + "");
c2 = info.getAllTitlesPRtables(PRLists.get(i));
if (c2.moveToFirst())
{
do{
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(C_TITLE, c2.getString(1).replaceAll("_", " "));
curChildMap.put(C_TEXT, "Child ");
}while (c2.moveToNext());
}
}
else
{
Log.i("tabla", "PR_OFTEN_USED");
c2 = info.getAllTitlesOftenUsed("PR_Often_Used");
if (c2.moveToFirst())
{
do{
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(C_TITLE, c2.getString(1).replaceAll("_", " "));
curChildMap.put(C_TEXT, "Child ");
}while (c2.moveToNext());
}
}
if (children.size() > max)
{
max = children.size();
}
childData.add(children);
}
info.close();
for ( int i = 0; i < PRLists.size(); i++) {
List<Map<String, Boolean>> childCB = new ArrayList<Map<String,Boolean>>();
for (int j = 0; j < max; j++) { //leghosszabb belso lista merete
Map<String, Boolean> curCB = new HashMap<String, Boolean>();
childCB.add(curCB);
curCB.put(C_CB, false);
}
childCheckBox.add(childCB);
}
adapter = new ExpandBaseAdapter(ProductLists.this,
groupData, childData, groupCheckBox, childCheckBox);
exList = (ExpandableListView) findViewById(R.id.layoutExListView);
exList.setAdapter(adapter);
exList.setGroupIndicator(null);
exList.setDivider(null);
int groupCount = adapter.getGroupCount();
for (int i = 0; i < groupCount; i++) {
exList.collapseGroup(i);
}
exList.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
CheckBox checkBox = (CheckBox) v.findViewById(R.id.multiple_checkbox);
checkBox.toggle();
if (childCheckBox.get(groupPosition).get(childPosition).get(C_CB)) //ha itt hiba akkor azert van m az adapterben akkor allitok cb statuszt ha van children, es mivel ha nincs akkor ez ertelmezhetetlen
{
childCheckBox.get(groupPosition).get(childPosition).put(C_CB, false);
}
else {
childCheckBox.get(groupPosition).get(childPosition).put(C_CB, true);
}
return false;
}
});
exList.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
//Log.i("group position", groupPosition + "");
Toast.makeText(ProductLists.this, "group position" + groupPosition, Toast.LENGTH_SHORT).show();
Log.i("PRLists.get(groupPosition)", PRLists.get(groupPosition));
if (PRLists.get(groupPosition).equals("PR_Often_Used"))
{
}
return false;
}
});
exList.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
groupPosition = ExpandableListView.getPackedPositionGroup(id);
childPosition = ExpandableListView.getPackedPositionChild(id);
selectionList2 = selectionList.toArray(new CharSequence[selectionList.size()]);
builder = new AlertDialog.Builder(ProductLists.this);
builder.setItems(selectionList2, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, final int item)
{
if (selectionList2[item].equals("Rename"))
{
}
if (selectionList2[item].equals("Delete"))
{
}
}
});
builder.show();
return true;
}
else if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
groupPosition = ExpandableListView.getPackedPositionGroup(id);
Log.i("tablagrp", PRLists.get(groupPosition) + "-" + groupPosition);
selectionList2A = selectionListA.toArray(new CharSequence[selectionListA.size()]);
builder = new AlertDialog.Builder(ProductLists.this);
builder.setItems(selectionList2A, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, final int item)
{
if (selectionList2A[item].equals("Add item"))
{
}
else if (selectionList2A[item].equals("Rename group"))
{
}
else if (selectionList2A[item].equals("Delete group"))
{
}
}
});
builder.show();
return true;
}
return false;
}
});
}
public void refreshList() {
if (PRLists.size() >= 0) {
PRLists.clear();
PRListsID.clear();
}
groupData.clear();
childData.clear();
groupCheckBox.clear();
childCheckBox.clear();
info = new HotOrNot(this);
info.open();
//----------------------------query tables--------------------------------
c = info.showAllTables();
if (c.moveToFirst())
{
do{
PRLists.add(c.getString(0));
}while (c.moveToNext());
}
if (PRLists.size() >= 0)
{
for (int i=0; i<PRLists.size(); i++)
{
Log.d("PRLists(" + i + ")", PRLists.get(i) + "");
}
}
ExpandBaseAdapter adapter = new ExpandBaseAdapter(ProductLists.this, groupData, childData, groupCheckBox, childCheckBox);
ExpandableListView exList = (ExpandableListView) findViewById(R.id.layoutExListView);
exList.setAdapter(adapter);
max = 0;
for (int i = 0; i < PRLists.size(); i++)
{
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(G_TEXT, PRLists.get(i).replaceAll("_", " ").substring(2, PRLists.get(i).replaceAll("_", " ").length()));
ArrayList parent = new ArrayList();
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
ArrayList child = new ArrayList();
c2 = info.getAllTitlesPRtables(PRLists.get(i));
if (c2.moveToFirst())
{
do{
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(C_TITLE, c2.getString(1).replaceAll("_", " "));
curChildMap.put(C_TEXT, "Child ");
}while (c2.moveToNext());
}
if (children.size() > max) {
max = children.size();
}
childData.add(children);
}
info.close();
for ( int i = 0; i < PRLists.size(); i++) {
List<Map<String, Boolean>> childCB = new ArrayList<Map<String,Boolean>>();
for (int j = 0; j < max; j++) {
Map<String, Boolean> curCB = new HashMap<String, Boolean>();
childCB.add(curCB);
curCB.put(C_CB, false);
}
childCheckBox.add(childCB);
}
adapter.notifyDataSetChanged();
//expand only those groups that was expanded by the user
for (int i = 0; i < grpsfav.size(); i++) {
Log.i("grpsfav items", grpsfav.get(i));
exList.expandGroup(Integer.valueOf(grpsfav.get(i)));
}
}
//the Holder class-------------------------------------------------------------------
static class ViewHolder {
TextView cTitle;
// TextView cText;
CheckBox checkBox;
}
public class ExpandBaseAdapter extends BaseExpandableListAdapter {
String loadedFontSize, loadedFontType, loadedConfDel, loadedshowhints;
SharedPreferences sharedPreferences;
Context mContext;
private static final String G_TEXT = "G_TEXT";
//private static final String G_CB = "G_CB";
private static final String C_TITLE = "C_TITLE";
private static final String C_TEXT = "C_TEXT";
private static final String C_CB = "C_CB";
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
List<Map<String, Boolean>> groupCheckBox = new ArrayList<Map<String,Boolean>>();
List<List<Map<String, Boolean>>> childCheckBox = new ArrayList<List<Map<String,Boolean>>>();
private Context context;
ViewHolder holder;
public ExpandBaseAdapter(Context context,
List<Map<String, String>> groupData, List<List<Map<String, String>>> childData,
List<Map<String, Boolean>> groupCheckBox, List<List<Map<String, Boolean>>> childCheckBox) {
this.groupData = groupData;
this.childData = childData;
this.groupCheckBox = groupCheckBox;
this.childCheckBox = childCheckBox;
this.context = context;
mContext = context;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.groupitem, null);
}
TextView title = (TextView) view.findViewById(R.id.groupText);
title.setText(getGroup(groupPosition).toString());
ImageView image = (ImageView) view.findViewById(R.id.groupBox);
if (isExpanded) {
image.setBackgroundResource(R.drawable.expander_ic_maximized);
}
else {
image.setBackgroundResource(R.drawable.expander_ic_minimized);
}
return view;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public Object getGroup(int groupPosition) {
return groupData.get(groupPosition).get(G_TEXT).toString();
}
public int getGroupCount() {
return groupData.size();
}
// **************************************
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.childitem, null);
holder = new ViewHolder();
holder.cTitle = (TextView) convertView.findViewById(R.id.child_title);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.multiple_checkbox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.cTitle.setText(childData.get(groupPosition).get(childPosition).get(C_TITLE).toString());
if (getChildrenCount(groupPosition) > 0) {
Log.i("chlrden", getChildrenCount(groupPosition) + "");
holder.checkBox.setChecked(childCheckBox.get(groupPosition).get(childPosition).get(C_CB));
}
return convertView;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public Object getChild(int groupPosition, int childPosition) {
return childData.get(groupPosition).get(childPosition).get(C_TITLE).toString();
}
public int getChildrenCount(int groupPosition) {
return childData.get(groupPosition).size();
}
// **************************************
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public void SaveExpanded(String key, String value){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
public void LoadExpanded(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
loadedexpanded = sharedPreferences.getString("expand", "false");
}
}
然而,当我再次单击它,集团将关闭,但我不明白的组号。我只得到它,当我打开一组。
However, when I click it again, the group closes, BUT i don't get the group number. I only get it, when I open a group.
如果我用
exList.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (groupPosition > 0){
return true;
}
return false;
}
});
我得到的组号两次,但随后的组不开放。
I get the group number both times, but then the groups are not opening.
任何想法?
推荐答案
然而,onGroupClickListener不能正常工作,我已经找到另一种解决方案。我用 setOnGroupExpandListener
和 setOnGroupCollapseListener
和他们奇迹般地工作!
However the onGroupClickListener is not working properly, I have found another solution. I used setOnGroupExpandListener
and setOnGroupCollapseListener
and they are working magically!
这篇关于Android的ExpandableListView获取组号时关闭组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!