如何使用simpleadapter与活动

时间:2014-02-09 13:52:02

标签: android listview simpleadapter

我正在尝试使用simpleadapter将服务器中的内容导入listview。但问题是我想将它用作活动而不是列表活动。那我该怎么办呢? 这是我的代码

    try
      {
          JSONArray jArray = new JSONArray(result);
          String s="";
          Log.w("Lengh",""+jArray.length());
          for(int i=0;i<jArray.length();i++){
                 ListView lv=getListView();
                 JSONObject json_data = jArray.getJSONObject(i);
                 s=json_data.getString("Cname");
                 HashMap<String, String> map = new HashMap<String, String>();
                 map.put("Zname",s/*json_data.getString("Cname")*/);
                 val.add(map);
         ListAdapter ad=new SimpleAdapter(Second.this,val,R.layout.v,new String[]{"Zname","Zname"},new int[]{R.id.textView1,R.id.textView2});
                setListAdapter(ad);  
                 Log.i("cname",s);
      }

3 个答案:

答案 0 :(得分:1)

只需将适配器设置为布局xml中定义的ListView

SimpleAdapter adapter = new SimpleAdapter(Second.this,val,R.layout.v,new String[]{"Zname","Zname"},new int[]{R.id.textView1,R.id.textView2});
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);

并在您的活动布局中xml文件将是:

<ListView
  android:id="@+id/list"
  ...
  />

答案 1 :(得分:1)

尝试这样的事情

Adapter adapter = new SimpleAdapter(Second.this,val,R.layout.v,new String[]{"Zname","Zname"},new int[]{R.id.textView1,R.id.textView2});
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);

您也可以custom adapter

答案 2 :(得分:0)