您需要运行树视图中属于资源文件夹的分层文件。我也在这里提交Folderstucture fragemets。
实际上我必须传递数据而不做任何硬编码,你可以看到案例1()...请帮助我。抱怨不好的例子任何需要我可以再次提供你的硬拷贝。
努力做到这一点。请给我一个android studio的例子。谢谢。
<?xml version='1.0' encoding='utf-8'?>
<de>
<haw_hamburg>
<bt7>
<r1485>
<devices>
<device id='135165'>
<device_name>TV living room</device_name>
<id_type>1</id_type>
<id_type_device>1</id_type_device>
<model>SmartTV LED 47"</model>
<owner></owner>
<connectivity>wifi</connectivity>
<address>192.168.1.100</address>
<info>
<screen>
<height>1080</height>
<width>1920</width>
</screen>
<speaker>true</speaker>
<input>
<item>
<name></name>
<id></id>
<unit></unit>
<sensortype></sensortype>
</item>
</input>
<output>
<item>
<name></name>
<id></id>
<unit></unit>
<actuatortype></actuatortype>
</item>
</output>
<wifi>
<mac>3C:CD:93:74:62:56</mac>
</wifi>
<ethernet>
<mac_eth>4D:CF:93:23:61:17</mac_eth>
</ethernet>
<rf>false</rf>
</info>
</device>
<device id='165165'>
<device_name>TV room</device_name>
<id_type>1</id_type>
<id_type_device>1</id_type_device>
<model>SmartTV LED 28"</model>
<owner></owner>
<connectivity>eth</connectivity>
<address></address>
<info>
<screen>
<height>768</height>
<width>1366</width>
</screen>
<speaker>true</speaker>
<input>
<item>
<name></name>
<id></id>
<unit></unit>
<sensortype></sensortype>
</item>
</input>
<output>
<item>
<name></name>
<id></id>
<unit></unit>
<actuatortype></actuatortype>
</item>
</output>
<wifi>
<mac>3C:CD:93:74:62:56</mac>
</wifi>
<ethernet>
<mac_eth>4D:CF:93:23:61:17</mac_eth>
</ethernet>
<rf>false</rf>
</info>
</device>
<device id='546161'>
<device_name>Temperature sensor kitchen</device_name>
<id_type>3</id_type>
<id_type_device>4</id_type_device>
<model>Sensor temperature</model>
<owner></owner>
<connectivity>radio</connectivity>
<address></address>
<info>
<screen>
<height></height>
<width></width>
</screen>
<speaker></speaker>
<input>
<item>
<name></name>
<id></id>
<unit></unit>
<sensortype></sensortype>
</item>
</input>
<output>
<item>
<name></name>
<id></id>
<unit></unit>
<actuatortype></actuatortype>
</item>
</output>
<wifi>
<mac>3C:CD:93:74:62:56</mac>
</wifi>
<ethernet>
<mac_eth>4D:CF:93:23:61:17</mac_eth>
</ethernet>
<rf>true</rf>
</info>
</device>
<device id='459863'>
<device_name>John's Phone</device_name>
<id_type>2</id_type>
<id_type_device>3</id_type_device>
<model>Samsung Galaxy Note 4</model>
<owner>2</owner>
<connectivity>wifi</connectivity>
<address>192.168.1.100</address>
<info>
<screen>
<height>2560</height>
<width>1440</width>
</screen>
<speaker>true</speaker>
<input>
<item>
<name></name>
<id></id>
<unit></unit>
<sensortype></sensortype>
</item>
</input>
<output>
<item>
<name></name>
<id></id>
<unit></unit>
<actuatortype></actuatortype>
</item>
</output>
<wifi>
<mac>3C:CD:93:74:62:56</mac>
</wifi>
<ethernet>
<mac_eth></mac_eth>
</ethernet>
<rf>false</rf>
</info>
</device>
<device id='189964'>
<device_name>Heating</device_name>
<id_type>1</id_type>
<id_type_device>5</id_type_device>
<model>Mitsubishi Electric</model>
<owner></owner>
<connectivity>wifi</connectivity>
<address>192.168.1.28</address>
<info>
<screen>
<height></height>
<width></width>
</screen>
<speaker></speaker>
<input>
<item>
<name>Humidity sensor</name>
<id>1</id>
<unit>mb</unit>
<sensortype>1</sensortype>
</item>
<item>
<name>Temperature sensor</name>
<id>2</id>
<unit>ยบC</unit>
<sensortype>2</sensortype>
</item>
</input>
<output>
<item>
<name>RGB led lights</name>
<id>3</id>
<unit></unit>
<actuatortype>1</actuatortype>
</item>
</output>
<wifi>
<mac>3C:XD:93:94:61:46</mac>
</wifi>
<ethernet>
<mac_eth></mac_eth>
</ethernet>
<rf>false</rf>
</info>
</device>
</devices>
</r1485>
</bt7>
</haw_hamburg>
</de>
package haw.com.smarthomelib;
import android.app.Fragment;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class FolderStructureFragment extends Fragment {
private TextView statusBar;
private AndroidTreeView tView;
private String Type;
private String Value;
private int check = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
/**
* setContentView(int) with a layout resource defining our UI
* using findViewById(int) to retrieve the widgets in that UI
* that we need to interact with
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_default, null, false);
ViewGroup containerView = (ViewGroup) rootView.findViewById(R.id.container);
statusBar = (TextView) rootView.findViewById(R.id.status_bar);
TreeNode root = TreeNode.root();
TreeNode Country = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_location, "Germany",""));
TreeNode City = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_location_city, "Hamburg",""));
TreeNode University = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_gps_fixed, "HAW Hamburg",""));
TreeNode Buliding1 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_gps_not_fixed, "Berliner Tor 1",""));
TreeNode Building2 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_gps_not_fixed, "Berliner Tor 3",""));
TreeNode Building3 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_gps_not_fixed, "Berliner Tor 5",""));
TreeNode Building4 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_hotel, "Berliner Tor 7",""));
University.addChildren(Buliding1, Building2, Building3, Building4);
TreeNode ServerRoom = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_laptop, "Server room",""));
TreeNode ProjectGroup1 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_group, "Group 1",""));
//TreeNode ProjectGroup2 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_group, "Group 2",""));
// TreeNode ProjectGroup3 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_group, "Group 3",""));
//TreeNode ProjectGroup4 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_group, "Group 4",""));
City.addChild(University);
Country.addChildren(City);
root.addChildren(Country);
Building4.addChild(ServerRoom);
// ServerRoom.addChildren(ProjectGroup1, ProjectGroup2, ProjectGroup3, ProjectGroup4);
ServerRoom.addChildren(ProjectGroup1);
fillFolder(ProjectGroup1);
// fillFolder(ProjectGroup2);
tView = new AndroidTreeView(getActivity(), root);
tView.setDefaultAnimation(true);
// 2Dscroll
//tView.setUse2dScroll(true);
tView.setDefaultContainerStyle(R.style.TreeNodeStyleCustom);
tView.setDefaultViewHolder(IconTreeItemHolder.class);
tView.setDefaultNodeClickListener(nodeClickListener);
tView.setDefaultNodeLongClickListener(nodeLongClickListener);
containerView.addView(tView.getView());
if (savedInstanceState != null) {
String state = savedInstanceState.getString("tState");
if (!TextUtils.isEmpty(state)) {
tView.restoreState(state);
}
}
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.expandAll:
tView.expandAll();
break;
case R.id.collapseAll:
tView.collapseAll();
break;
}
return true;
}
private void fillFolder(TreeNode folder) {
switch (check) {
case 1: {
TreeNode file1 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_check_circle_blank, "TempIn", "-5°c"));
TreeNode file2 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_check_circle_blank, "TempOut", "25°c"));
TreeNode file3 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_check_circle_blank, "Humidity", "35%"));
TreeNode file4 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_check_circle_blank, "Battery", "62%"));
folder.addChildren(file1, file2, file3,file4 );
break;
}
case 2:
try {
//System.out.println("----------------------------");
File fXmlFile = new File("/Users/student/Desktop/Configuration.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
// System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("Sensors");
//System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
//System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
//System.out.println("Sensors id : "
// + eElement.getAttribute("id"));
Type = eElement.getElementsByTagName("type")
.item(0).getTextContent();
Value = eElement.getElementsByTagName("value")
.item(0).getTextContent();
TreeNode file = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_check_circle_blank, Type, Value));
folder.addChildren(file);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private TreeNode.TreeNodeClickListener nodeClickListener = new TreeNode.TreeNodeClickListener() {
@Override
public void onClick(TreeNode node, Object value) {
IconTreeItemHolder.IconTreeItem item = (IconTreeItemHolder.IconTreeItem) value;
statusBar.setText("Last clicked: " + item.text);
}
};
private TreeNode.TreeNodeLongClickListener nodeLongClickListener = new TreeNode.TreeNodeLongClickListener() {
@Override
public boolean onLongClick(TreeNode node, Object value) {
IconTreeItemHolder.IconTreeItem item = (IconTreeItemHolder.IconTreeItem) value;
Toast.makeText(getActivity(), "Long click: " + item.text, Toast.LENGTH_SHORT).show();
return true;
}
};
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("tState", tView.getSaveState());
}
}