【发布时间】:2012-08-15 09:25:19
【问题描述】:
我正在尝试解析具有以下结构的 XML 文件:
<groups>
<group>
<id>...</id>
<name>...</name>
<description>...</description>
<actions>
<action>...</action>
<action>...</action>
</actions>
</group>
<group>
<id>...</id>
<name>...</name>
<description>...</description>
<actions>
<action>...</action>
<action>...</action>
<action>...</action>
</actions>
</group>
<group>
<id>...</id>
<name>...</name>
<description>...</description>
<actions>
<action>...</action>
</actions>
</group>
</groups>
目前在第一个 ListView 中,每行可见“名称”和“描述”。这是功能齐全的。选择一行时,将使用另一个 ListView 创建一个新活动。在这里,我想根据上一个列表中的选定位置显示每行的操作。我该怎么做?我还尝试将“动作”添加到我的其他项目添加到的地图中,但它只为每组添加 1 个“动作”。 请指教?
公共类 ErrorCodeList 扩展 ListActivity {
public static final String LANGUAGE = null;
public ArrayList<HashMap<String, String>> mylist;
public ListAdapter adapter;
public Dialog progDialog;
public ProgressBar progBar;
public TextView lblMessage;
private Intent myIntent;
private String URLvariable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
myIntent = getIntent();
URLvariable = myIntent.getExtras().getString("urlType");
mylist = new ArrayList<HashMap<String, String>>();
adapter = new SimpleAdapter(this, mylist , R.layout.activity_error_list,
new String[] { "tid", "tname" },
new int[] { R.id.item_id, R.id.item_title });
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> hashMap = (HashMap<String, String>) lv.getItemAtPosition(position);
Intent myIntent = new Intent(view.getContext(),ErrorCodeDetails.class);
myIntent.putExtra("map",hashMap);
startActivity(myIntent);
}
});
progDialog = new Dialog(this, R.style.progress_dialog);
progDialog.setContentView(R.layout.progress_dialog);
progBar = (ProgressBar) progDialog.findViewById(R.id.progBar);
lblMessage = (TextView) progDialog.findViewById(R.id.txtProgMessage);
lblMessage.setText("Please Wait.....");
progDialog.show();
new GetDataTask().execute();
}
private Boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni != null && ni.isConnected())
return true;
return false;
}
private class GetDataTask extends AsyncTask<Void, Void, Integer> {
@Override
protected Integer doInBackground(Void... params) {
if(isOnline()){
mylist.clear();
}
// Start the http request
String feedURL="http://...";
String xml = XMLfunctions.getXML(feedURL);
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = 1;
if((numResults <= 0)){
Toast.makeText(ErrorCodeList.this, "Geen resultaten gevonden", Toast.LENGTH_LONG).show();
finish();
}
NodeList troubles = doc.getElementsByTagName("trouble");
for (int i = 0; i < troubles.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)troubles.item(i);
if((e.getAttributes().getNamedItem("type").getNodeValue().equals("error"))) {
//map.put("id", "ID:" + Integer.valueOf(e.getAttributes().getNamedItem("id").getNodeValue()));
//map.put("status", "Status:" + (e.getAttributes().getNamedItem("status").getNodeValue()));
map.put("tid", XMLfunctions.getValue(e, "id"));
map.put("tname", XMLfunctions.getValue(e, "name"));
map.put("tdescription", XMLfunctions.getValue(e, "description"));
//NodeList ns=e.getElementsByTagName("trouble_cause");
mylist.add(map);
}
}
System.out.println(mylist);
}else{
Toast.makeText(ErrorCodeList.this, "No connection..", Toast.LENGTH_LONG).show();
}
return 1;
}
@Override
protected void onPostExecute(Integer result) {
setListAdapter(adapter);
progDialog.dismiss();
super.onPostExecute(result);
}
}
}
【问题讨论】:
-
我不是在寻找可消耗的列表,因为必须在操作列表中添加一些其他内容。因此,我正在寻找一个显示新 ListView 的新屏幕(活动)。我的问题是我不知道将操作添加到我的arrayList 的正确方法。其他项目是通过 hashmap
添加的,但是在指向多个操作时我遇到了问题。 -
您提到使用数组列表,这似乎是适合我的方式。究竟是什么问题?
-
正确。问题是我现在有一个 hashmap
被添加到我的 mylist arrayList 中。在这个哈希图中,“id”、“name”和“description”被添加并正确传递给 ListView。但我有困难的行动。似乎我无法将哈希图添加到哈希图中。我的感觉是这种做法是不正确的。 -
要添加到目前我使用 simpleAdapter 将 mylist arrayList 传递给 listview。
标签: android xml listview arraylist hashmap