2012-08-15 44 views
0

我有一个简单的适配器以这种方式填充ListView如何将hashmap <string,hashmap <string,string >>传递给一个简单的适配器?

adapter = new SimpleAdapter(this, mylist , 
        R.layout.itemlist, new String[] { "item1", "items2" }, 
        new int[] { R.id.item_id, R.id.item_title }); 

调整我的代码,并有hashmap<string, hashmap<string,string>>现在,我不能把它传递给new string[]{"item1","item2"};

我应该如何解决这个问题?

这里是我的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> 

我准备了,但就死在代码是这样的:

public class ErrorCodeList extends 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")); 

        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); 
    } 
} 
} 

这之前,我试图巢hasmaps。 这是否澄清我的目标?

+0

使用此自定义列表视图 – 2012-08-15 14:18:32

+0

我应该如何做这个你有一个例子吗? – iJar 2012-08-15 14:20:01

+0

[看这个tuts](http://samir-mangroliya.blogspot.in/p/android-customized-listview.html) – 2012-08-15 14:20:45

回答

0

SimpleAdapter的构造函数需要List<Map<String, String>>,而不是Map<String, Map<String, String>>

请问您的内部Map需要被另一个Map而不是List包含吗?如果没有,你可以改变类型。如果是这样,你可以使用下面的提取内Map对象的List

asList = new ArrayList<Map<String, String>>(outerMap.values()); 

注:我假设你Map包含在你的列表中的每个行的数据,作为指定here。例如:

// Create an example row (you'd have many rows) 
Map<String, String> row = new HashMap<String, String>(); 
row.put("author", "George Orwell"); 
row.put("title", "Nineteen Eighty-Four"); 
row.put("year", "1949"); 

// Create an example list of rows (this would contain many maps) 
List<Map<String, String>> rows = new ArrayList<Map<String, String>>(); 
rows.add(row); 

rows将取代你的问题myList以上。您仍然有一个String[],其中包含Map(作者,标题,年份)中的密钥,并且您仍然有一个int[],其中包含相关值出现的布局中的资源ID View s(例如,对TextView的引用s在您的XML布局中,如R.id.author,R.id.titleR.id.year)。

+0

然后asList应该在新字符串[] {“item1”,“item2”} ;? – iJar 2012-08-15 14:32:13

+0

不,如果'asList'包含每个项目的数据,那么它将是第二个参数(它将替换上面问题中的'mylist')。第四个参数'new String []'具有来自Map的键,第五个参数'new int []'具有资源ID,用于指示值的位置。 – user113215 2012-08-15 14:57:52

+0

我想要实现的是填充一个ListView与XML结构,我现在将发布在我的问题。并且从这个XML填充ListView时,在ListView中选择一行时,将使用一个新的ListView创建另一个活动,该ListView包含连接到从中选择的组的“”的信息。 – iJar 2012-08-15 15:50:29

相关问题