Android ExpandableListView更改为ListView
在我的Android应用程序中,我有一个可扩展的列表视图。我想把它改成普通的ListView。是否可以不改变完整的代码 以下是适配器类中的代码: 代码没有所有导入,因为有许多导入Android ExpandableListView更改为ListView,android,listview,Android,Listview,在我的Android应用程序中,我有一个可扩展的列表视图。我想把它改成普通的ListView。是否可以不改变完整的代码 以下是适配器类中的代码: 代码没有所有导入,因为有许多导入 public class ExpandableListAdapter extends BaseExpandableListAdapter { private Activity context; private Map<String, List<BizDriveNotification&g
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Activity context;
private Map<String, List<BizDriveNotification>> bizCollections;
private List<String> biz;
public ExpandableListAdapter(Activity context, List<String> biz, Map<String, List<BizDriveNotification>> bizCollections) {
this.context = context;
this.bizCollections = bizCollections;
this.biz = biz;
}
public Object getChild(int groupPosition, int childPosition) {
return bizCollections.get(biz.get(groupPosition)).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final BizDriveNotification note = (BizDriveNotification) getChild(groupPosition, childPosition);
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_item, null);
}
ImageView symbol = (ImageView) convertView.findViewById(R.id.imgNotification);
String checkNotification;
checkNotification = note.type.toString();
if (checkNotification.equals("notiz")){
//setz als Symbol das BizDrive-Icon für Notiz
symbol.setImageResource(R.drawable.notiz);
} else if (checkNotification.equals("aufgabe")){
//setz als Symbol das BizDrive-Icon für Auffgaben
symbol.setImageResource(R.drawable.aufgabe);
}else if (checkNotification.equals("kalendertermin")){
//setz als Symbol das BizDrive-Icon für Kalender
symbol.setImageResource(R.drawable.kalender);
}
TextView timeDate = (TextView) convertView.findViewById(R.id.nfDateTime);
if (note.time == null ){
timeDate.setText("");
}else{
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("dd.MM.yyyy\nHH:mm");
timeDate.setTextSize(8);
timeDate.setText(simpleDateFormat2.format(note.time) + " Uhr");
}
//baut den Titel zusammen
TextView titel = (TextView) convertView.findViewById(R.id.nfHeader);
titel.setText(note.title);
titel.setTypeface(null, Typeface.BOLD);
titel.setTextSize(16);
//baut den Notification-Inhalt zusammen
TextView description = (TextView) convertView.findViewById(R.id.nfContent);
description.setText(note.description);
//setzt den Ersteller
TextView creator = (TextView) convertView.findViewById(R.id.nfCreator);
creator.setTextSize(8);
creator.setText("Ersteller: " + note.creator);
// //setzt das Datum
TextView date = (TextView) convertView.findViewById(R.id.nfCreationDate);
//Datum in dd.MM.yyyy Format umwandeln und in die Liste eintragen
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
date.setTextSize(8);
date.setText("erstellt am: " + simpleDateFormat.format(note.date));
return convertView;
}
public int getChildrenCount(int groupPosition) {
return bizCollections.get(biz.get(groupPosition)).size();
}
public Object getGroup(int groupPosition) {
return biz.get(groupPosition);
}
public int getGroupCount() {
return biz.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
//Baut den Titel ListView Punkte zusammen
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String title = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_item, null);
}
TextView item = (TextView) convertView.findViewById(R.id.head);
item.setText(title);
return convertView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
// //aus der ListView eine XML Datei erzeugen und speichern
public void persistListView(){
try {
FileOutputStream out = context.openFileOutput("notifications.xml", Context.MODE_PRIVATE);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//Wurzelelement
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("notifications");
doc.appendChild(rootElement);
for (String singleKey : bizCollections.keySet()) {
for(BizDriveNotification noti: bizCollections.get(singleKey)){
// Knotenpunkt notification
Element note = doc.createElement("notification");
rootElement.appendChild(note);
// Knotenpunkt id
Element id = doc.createElement("id");
id.appendChild(doc.createTextNode(Integer.toString(noti.id)));
note.appendChild(id);
// Knotenpunkt title
Element title = doc.createElement("title");
title.appendChild(doc.createTextNode(noti.title));
note.appendChild(title);
// Knotenpunkt description
Element description = doc.createElement("description");
description.appendChild(doc.createTextNode(noti.description));
note.appendChild(description);
// Knotenpunkt type
Element type = doc.createElement("type");
type.appendChild(doc.createTextNode(noti.type));;
note.appendChild(type);
//Knotenpunkt creator
Element creator = doc.createElement("creator");
creator.appendChild(doc.createTextNode(noti.creator));
note.appendChild(creator);
//Knotenpunkt time
Element time = doc.createElement("time");
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
if (noti.time != null){
time.appendChild(doc.createTextNode(simpleDateFormat2.format(noti.time).toString()));
note.appendChild(time);
}
else{
time.appendChild(doc.createTextNode(""));
note.appendChild(time);
}
// Knotenpunkt date
Element date = doc.createElement("date");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
date.appendChild(doc.createTextNode(simpleDateFormat.format(noti.date).toString()));
note.appendChild(date);
}
}
// Inhalt in xml Datei schreiben
TransformerFactory transformerFactory = TransformerFactory.newInstance();
StringWriter sw = new StringWriter();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(doc), new StreamResult(sw));
String newFileXml = sw.toString();
try {
//xml Datei speichern
OutputStreamWriter wrt = new OutputStreamWriter(out);
try {
wrt.write(newFileXml);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if (wrt != null){
wrt.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
你可以使用你的代码。需要更改。如果设计良好,则只需更改适配器和任何类型声明。显示您的代码。下面是适配器的完整代码