我有这个java代码,我有两个问题:
我想为每个家长的每个孩子设置一个不同的图标,但是使用此代码我只能为所有孩子设置相同的图标,或者为位于X地方的孩子设置图标,为另一个图标设置另一个孩子。
例如,当我按下名为“Apple”的项目时,我怎么做才开始活动?
如何解决我的问题?非常感谢你。
public class sedactivity extends ExpandableListActivity {
ExpandableListAdapter mAdapter;
private final static String NAME = "NAME";
private final static String SURNAME = "SURNAME";
private Resources res;
private Drawable photo, photo2, photo3, photo4;
private List<Drawable> albumCovers;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_main);
// Create a List of Drawables to insert into the Expandable List
// This can be completely dynamic
res = this.getResources();
photo = (Drawable) res.getDrawable(R.drawable.bee);
photo2 = (Drawable) res.getDrawable(R.drawable.astro);
photo3 = (Drawable) res.getDrawable(R.drawable.bomb);
photo4 = (Drawable) res.getDrawable(R.drawable.apple);
albumCovers = new ArrayList<Drawable>();
albumCovers.add(photo);
albumCovers.add(photo2);
// The following code simply generates the Expandable Lists content (Strings)
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
Map<String, String>
//List A
curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "A");
curGroupMap.put(SURNAME, "(2 Photos)");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Apple");
curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Astro");
curChildMap = new HashMap<String, String>();
children.add(curChildMap);
childData.add(children);
//List B
curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "B");
curGroupMap.put(SURNAME, "(2 Photos)");
children = new ArrayList<Map<String, String>>();
curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Bee");
curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Bomb");
curChildMap = new HashMap<String, String>();
children.add(curChildMap);
childData.add(children);
//List C
curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "C");
curGroupMap.put(SURNAME, "(0 Photo)");
children = new ArrayList<Map<String, String>>();
childData.add(children);
// Set up our adapter
mAdapter = new MyExpandableListAdapter(
this,
groupData,
R.layout.list_parent,
new String[] { NAME, SURNAME },
new int[] { R.id.rowText1, R.id.rowText2,R.id.photoAlbumImg },
childData,
R.layout.list_child,
new String[] { NAME, SURNAME },
new int[] { R.id.rowText1, R.id.photoAlbumImg }
);
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
}
/**
* A simple adapter which allows you to bind data to specific
* Views defined within the layout of an Expandable Lists children
* (Implement getGroupView() to define the layout of parents)
*/
public class MyExpandableListAdapter extends SimpleExpandableListAdapter {
private List<? extends List<? extends Map<String, ?>>> mChildData;
private String[] mChildFrom;
private int[] mChildTo;
public MyExpandableListAdapter(Context context,
List<? extends Map<String, ?>> groupData, int groupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData,
int childLayout, String[] childFrom, int[] childTo) {
super(context, groupData, groupLayout, groupFrom, groupTo,
childData, childLayout, childFrom, childTo);
mChildData = childData;
mChildFrom = childFrom;
mChildTo = childTo;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = newChildView(isLastChild, parent);
} else {
v = convertView;
}
bindView(v, mChildData.get(groupPosition).get(childPosition), mChildFrom,
mChildTo, groupPosition, childPosition);
return v;
}
// This method binds my data to the Views specified in the child xml layout
private void bindView(View view, Map<String, ?> data, String[] from, int[] to, int groupPosition, int childPosition) {
int len = to.length - 1;
// Apply TextViews
for (int i = 0; i < len; ++i) {
TextView v = (TextView) view.findViewById(to[i]);
if (v != null) {
v.setText((String) data.get(from[i]));
}
// Apply ImageView
ImageView imgV = (ImageView) view.findViewById(to[1]);
if (imgV != null) {
if(childPosition % 1 == 0) imgV.setImageDrawable(albumCovers.get(0));
else imgV.setImageDrawable(albumCovers.get(1));
}
}
}
}
}
答案 0 :(得分:0)
该行
imgV.setImageDrawable(albumCovers.get(1))
bindView
方法中的设置子视图的图像。您可以将其设置为您想要的任何内容,可以使其具有特定数据(假设您在此视图所代表的数据中有图像网址,您可以设置imgV
以显示该图像。
要将View.OnClickListener
附加到每个子项目,只需在覆盖的getChildView
(针对v/convertView
)或bindView
方法(针对{{1 }}):
view
更新1
如果你确实有一个使用列表中显示的名称定义的drawable,你可以将你的for循环更新为:
view.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO: Implement the tasks to be done here
}
});
只需使用您应用程序的默认包更新for (int i = 0; i < len; ++i) {
TextView v = (TextView) view.findViewById(to[i]);
if (v != null) {
v.setText((String) data.get(from[i]));
}
ImageView imgV = (ImageView) view.findViewById(to[1]);
imgV.setImageResource(getResources().getIdentifier((String)
data.get(from[i]), "drawable", "yourdefaultpackage"));
}
}
部分。
更新2
下面是一个详细的示例,说明当您希望根据行所包含的数据为可扩展列表视图中的所有子行设置不同的图标时:
让我们假设要显示的分层数据结构是:
<强> RES /原料/ data.xml中强>:
yourdefaultpackage
您需要定义类以将此数据表示为java对象。这很简单,基本上你需要<?xml version="1.0" encoding="UTF-8"?>
<data>
<group letter="A">
<child name="American Bull" img="american_bull" />
<child name="Arco's" img="arcos" />
</group>
<group letter="B">
<child name="Barter" img="barter" />
<child name="Bitter B." img="bitter_b" />
<child name="Bull Hunter" img="bull_hunter" />
<child name="Bull In Cuba" img="bull_in_cuba" />
<child name="Bullito" img="bullito" />
</group>
<group letter="C">
<child name="Cackle" img="cackle" />
<child name="Cheapstake" img="cheapstake" />
<child name="Checco's Bull" img="checcos_bull" />
<child name="Cheeriness" img="cheeriness" />
<child name="Cross Current" img="cross_current" />
<child name="Cruel" img="cruel" />
</group>
<group letter="D"></group>
<group letter="E"></group>
<group letter="F">
<child name="Firearm" img="firearm" />
<child name="First Of All" img="first_of_all" />
<child name="First Apple" img="first_apple" />
</group>
<group letter="G">
<child name="Golden" img="golden" />
<child name="Green Bull" img="green_bull" />
</group>
<group letter="H">
<child name="Hollywood Passion" img="hollywood_passion" />
</group>
<group letter="I"></group>
<group letter="J"></group>
<group letter="K">
<child name="Kind Bull" img="kind_bull" />
<child name="Kingling" img="kingling" />
</group>
<group letter="L">
<child name="Lab" img="lab" />
<child name="Leary" img="leary" />
<child name="Leg Pull" img="leg_pull" />
<child name="Long Bull" img="long_bull" />
<child name="Lucy" img="lucy" />
</group>
<group letter="M">
<child name="Mayla" img="mayla" />
</group>
<group letter="N">
<child name="New Red Bull Fantasy" img="new_red_bull_fantasy" />
<child name="Nu Energy" img="nu_energy" />
</group>
<group letter="O"></group>
<group letter="P">
<child name="Passion" img="passion" />
<child name="Power" img="power" />
</group>
<group letter="Q"></group>
<group letter="R">
<child name="Rebellion Red Peach" img="rebellion_red_peach" />
<child name="Red Bulloska" img="red_bulloska" />
<child name="Red 28" img="red_28" />
<child name="Red Bull Wallbanger" img="red_bull_wallbanger" />
<child name="Red Cell" img="red_cell" />
<child name="Red Hothead" img="red_hothead" />
<child name="Red Lime" img="red_lime" />
</group>
<group letter="S">
<child name="Speedy" img="speedy" />
<child name="Spritz Power" img="spritz_power" />
<child name="Stiff-Necked" img="stiff_necked" />
</group>
<group letter="T"></group>
<group letter="U"></group>
<group letter="V">
<child name="Vedi O' Mare Come E' Bull" img="vedi_o_mare_come_e_bull" />
</group>
<group letter="W"></group>
<group letter="X"></group>
<group letter="Y"></group>
<group letter="Z"></group>
<group letter="#"></group>
</data>
Group
和letter
,其中list of Child
有Child
和name
。<登记/>
因此,您将拥有包含上述属性的image
和Group.java
类:
<强> Group.java:强>
Child.java
<强> Child.java 强>:
public class Group
{
private String letter;
private ArrayList<Child> children;
/**
* @return the letter
*/
public String getLetter()
{
return letter;
}
/**
* @param letter the letter to set
*/
public void setLetter(String letter)
{
this.letter = letter;
}
/**
* @return the children
*/
public ArrayList<Child> getChildren()
{
return children;
}
/**
* @param children the children to set
*/
public void setChildren(ArrayList<Child> children)
{
this.children = children;
}
}
要将xml结构读入public class Child
{
private String name;
private String image;
/**
* @return the name
*/
public String getName()
{
return name;
}
/**
* @param name the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return the image
*/
public String getImage()
{
return image;
}
/**
* @param image the image to set
*/
public void setImage(String image)
{
this.image = image;
}
}
个对象的ArrayList,您还需要一个简单的xml处理程序,它将检查标记,并构建您需要的结构:
<强> XmlHandler.java 强>:
Group
要显示列表/可展开列表,您需要import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XmlHandler extends DefaultHandler
{
/**
* The tag- and attribute names in the xml data
*/
private static final String TAG_GROUP = "group";
private static final String TAG_CHILD = "child";
private static final String ATTR_LETTER = "letter";
private static final String ATTR_NAME = "name";
private static final String ATTR_IMG = "img";
/**
* holds the currently processing Group instance
*/
private Group currentGroup;
/**
* holds the currently processing Child instance
*/
private Child currentChild;
/**
* the list of Group objects parsed from the xml
*/
private ArrayList<Group> records = null;
/**
* @return the list of Group parsed from the xml
*/
public ArrayList<Group> getGroups()
{
return records;
}
@Override
public void startDocument() throws SAXException
{
super.startDocument();
this.records = new ArrayList<Group>();
}
@Override
public void startElement(final String Uri, final String localName, final String qName,
final Attributes attributes) throws SAXException
{
if (localName == null)
return;
if (localName.equals(TAG_GROUP))//examining a group tag
{
currentGroup = new Group();
currentGroup.setLetter(attributes.getValue(ATTR_LETTER));//the letter attribute
currentGroup.setChildren(new ArrayList<Child>());
records.add(currentGroup);
}
else if (localName.equals(TAG_CHILD))//examining a child tag
{
currentChild = new Child();
currentChild.setName(attributes.getValue(ATTR_NAME));//the name attribute
currentChild.setImage(attributes.getValue(ATTR_IMG));//the img attribute
currentGroup.getChildren().add(currentChild);
}
}
}
类。在这种情况下,我们将使用简单的Adapter
实现来显示父行和子行及其正确的图标:
MyExpandableListAdapter.java :
BaseExpandableListAdapter
最后,活动类使用以上三种:
<强> testactivity.java 强>:
import java.util.ArrayList;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyExpandableListAdapter extends BaseExpandableListAdapter
{
private final LayoutInflater inflater;
private final ArrayList<Group> groups;
public MyExpandableListAdapter(LayoutInflater inflater, final ArrayList<Group> groups)
{
this.inflater = inflater;
this.groups = groups;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parentView)
{
final Group parent = groups.get(groupPosition);
if (convertView == null) //if the row is null, we create it (by inflation)
convertView = inflater.inflate(R.layout.list_parent, parentView, false);
// display the letter of the group:
((TextView) convertView.findViewById(R.id.rowText1)).
setText(parent.getLetter());
// display the children count of the group:
((TextView) convertView.findViewById(R.id.rowText2)).
setText("(" + parent.getChildren().size() + " Ricette)");
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parentView)
{
final Group group = groups.get(groupPosition);
final Child child = group.getChildren().get(childPosition);
if (convertView == null)//if the child row is null, we inflate it
convertView = inflater.inflate(R.layout.list_child, parentView, false);
// display the name of the child on the row:
((TextView) convertView.findViewById(R.id.rowText1)).setText(child.getName());
// set the proper icon of the child on the child row:
((ImageView) convertView.findViewById(R.id.photoAlbumImg)).
setImageResource(parentView.getResources().
getIdentifier(child.getImage(), "drawable", "com.test.com"));
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
return groups.get(groupPosition).getChildren().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition)
{
return groups.get(groupPosition).getChildren().size();
}
@Override
public Object getGroup(int groupPosition)
{
return groups.get(groupPosition);
}
@Override
public int getGroupCount()
{
return groups.size();
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public void notifyDataSetChanged()
{
super.notifyDataSetChanged();
}
@Override
public boolean isEmpty()
{
return ((groups == null) || groups.isEmpty());
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
@Override
public boolean hasStableIds()
{
return true;
}
@Override
public boolean areAllItemsEnabled()
{
return true;
}
}
我希望你能以这种方式解决这个问题。