您的位置:首页 > 其它

SimpleAdapter中设定了 R.layout.listview_item

2017-01-02 07:44 381 查看
    private static final String TAG = "MainActivity";

    private List<Person> persons;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

  mButton = (Button) findViewById(R.id.button1);

          mListView = (ListView) findViewById(R.id.listView1);

          mButton.setOnClickListener(new MyOnClickListener());

}

 private class MyOnClickListener implements OnClickListener {

         @SuppressLint("ShowToast")

@Override

         public void onClick(View v) {

                 try {

                         // 得到Json解析成功之后数据

                         persons = JsonParse.getListPerson(urlPath);

                         List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

                         for (int i = 0; i < persons.size(); i++) {

                                 HashMap<String, Object> map = new HashMap<String, Object>();

                                 map.put("id",persons.get(i).getId());

                                 map.put("name", persons.get(i).getName());

                                 map.put("password", persons.get(i).getPassword());

                                 map.put("age", persons.get(i).getAge());  

                                 map.put("sex",persons.get(i).getSex());

                                 map.put("hobby",persons.get(i).getHobby());

                                 map.put("city",persons.get(i).getCity());

                                 data.add(map);

                         }

                         SimpleAdapter _Adapter = new SimpleAdapter(MainActivity.this,

                                 data, R.layout.listview_item, new String[] { "id","name",

                                                 "password", "age","sex","hobby","city" }, new int[] { R.id.textView1,

                                                 R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5, R.id.textView6, R.id.textView7 });

                 mListView.setAdapter(_Adapter);

         } catch (Exception e) {

                 Toast.makeText(MainActivity.this, "解析失败", 2000).show();

                 Log.i(TAG, e.toString());

         }

       }

这里的lisR.layout.listview_item,, R.id.textView1,R.id.button1,,R.id.listView1在xml中怎么写。。才可以显示在应用上
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: