【问题标题】:How to use simpleadapter with activity如何在活动中使用 simpleadapter
【发布时间】:2014-02-09 13:52:01
【问题描述】:

我正在尝试使用简单适配器将内容从服务器获取到列表视图中。但问题是我想将它用作活动而不是列表活动。那我该怎么做呢? 这是我的代码

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

【问题讨论】:

  • 创建一个ListView,然后给它分配适配器:listView.setAdapter(adapter);

标签: android listview simpleadapter


【解决方案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"
  ...
  />

【讨论】:

    【解决方案2】:

    试试这样的

    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

    【讨论】:

      【解决方案3】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      相关资源
      最近更新 更多