【问题标题】:How to pass hashmap<string, hashmap<string, string>> to a simple adapter?如何将 hashmap<string, hashmap<string, string>> 传递给简单的适配器?
【发布时间】:2012-08-15 14:07:20
【问题描述】:

我有一个简单的适配器以这种方式填充ListView

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

我调整了我的代码,现在有一个hashmap&lt;string, hashmap&lt;string,string&gt;&gt;,但我无法将它传递给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);
    }
}
}

这是在我尝试嵌套 hasmap 之前。 这是否阐明了我的目标?

【问题讨论】:

  • 我该怎么做你有例子吗?
  • 谢谢我会读这个。如果涉及到 android 开发,我是一个新手,但是在本教程中我应该关注什么来解决我的问题?

标签: android hashmap adapter arrays


【解决方案1】:

SimpleAdapter 的构造函数采用 List&lt;Map&lt;String, String&gt;&gt;,而不是像您一样的 Map&lt;String, Map&lt;String, String&gt;&gt;

你的内心 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 Views 关联值应该出现(例如,在您的 XML 布局中引用 TextViews,如 R.id.authorR.id.titleR.id.year)。

【讨论】:

  • 然后 asList 应该在 new string[]{"item1","item2"};?
  • 不,如果asList 包含每个项目的数据,那么它将是第二个参数(它将替换您上面问题中的mylist)。第 4 个参数 new String[] 具有来自 Map 的键,第 5 个参数 new int[] 具有告诉该值去向的资源 ID。
  • 我想要实现的是用我现在将在我的问题中发布的 XML 结构填充 ListView。当在 ListView 中选择一行时,从这个 XML 填充一个 ListView,另一个活动将创建一个新的 ListView,其中包含连接到从中选择的组的“”信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 2021-11-13
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 2011-04-07
  • 1970-01-01
相关资源
最近更新 更多