我使用此代码获取可扩展列表中的选定项目并获取其文本并将其打印在日志中。但是日志显示此a.b.ExpandListChild@41cc3c90
,我点击的项目包含此1
的其他文字。我想访问此文1
,这将在列表视图中显示。
ExpandList.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView parent, View v, int group_position, int child_position, long id)
{
if(group_position==0 && child_position==0){
startActivity(intent);
ExpandableListAdapter itemAdapter=parent.getExpandableListAdapter();
String selectedItem=""+itemAdapter.getChild(group_position, child_position);
// String country = ""+ExpAdapter.getChild(group_position, child_position);
Log.i("ddd", ""+selectedItem);
} else if(group_position==2 && child_position==2){
}
return false;
}
});
所有代码:
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
private ExpandListAdapter ExpAdapter;
private ArrayList<ExpandListGroup> ExpListItems;
private ExpandableListView ExpandList;
ArrayList<ExpandListGroup> list;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ExpandList = (ExpandableListView) findViewById(R.id.ExpList);
ExpListItems = SetStandardGroups();
ExpAdapter = new ExpandListAdapter(MainActivity.this, ExpListItems);
ExpandList.setAdapter(ExpAdapter);
final Intent intent= new Intent (this, zero.class);
ExpandList.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView parent, View v, int group_position, int child_position, long id)
{
if(group_position==0 && child_position==0){
startActivity(intent);
ExpandableListAdapter itemAdapter=parent.getExpandableListAdapter();
String selectedItem=""+itemAdapter.getChild(group_position, child_position);
//String country = ""+ExpAdapter.getChild(group_position, child_position);
Log.i("ddd", ""+selectedItem);
}
else if(group_position==2 && child_position==2){
}
return false;
}
});
ExpandList.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
int itemType = ExpandableListView.getPackedPositionType(id);
if(itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPosition = ExpandableListView.getPackedPositionGroup(id);
Log.i("s", "removepos");
if ( itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int childPosition = ExpandableListView.getPackedPositionChild(id);
}
list.remove(groupPosition);
ExpAdapter.notifyDataSetChanged();}
return true;
}
});
}
public boolean onContextItemSelected1(MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
//String title = ((TextView) info.targetView).getText().toString();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
removeGroup(groupPos);
ExpAdapter.notifyDataSetChanged();
}
return false;
}
public ArrayList<ExpandListGroup> SetStandardGroups() {
list = new ArrayList<ExpandListGroup>();
String[] listlist= new String[] {"1","2","3","4","5","6","7","8","9"};
String[] names= new String[] {"one","two","three"};
for ( int i=0 ; i<3 ;i++)
{
ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();
ExpandListGroup gru1 = new ExpandListGroup();
gru1.setName(names[i]);
for ( int k=i*3 ; k<(i+1)*3 ;k++){
ExpandListChild ch1_1 = new ExpandListChild();
ch1_1.setName(listlist[i]);
ch1_1.setTag(null);
list2.add(ch1_1);
}
gru1.setItems(list2);
list.add(gru1);
}
return list;
}
public void dialog (long id)
{
final int position=(int) id;
//String number=list.get(position);
list.remove(position);
ExpAdapter.notifyDataSetChanged();
}
public void removeGroup(int group) {
//TODO: Remove the according group. Dont forget to remove the children aswell!
Log.v("Adapter", "Removing group"+group);
}
}
}
答案 0 :(得分:2)
我认为错误就在这里
itemAdapter.getChild(...)
getClass().getName() + '@' + Integer.toHexString(hashCode())
具有返回类型的Object。当您打印对象时,它将打印Object类的默认toString()实现。
Object.toString()
getChild(...)
答案:将{{1}}投射到适配器项类型并使用它。