【问题标题】:SimpleAdapter showing an empty listSimpleAdapter 显示一个空列表
【发布时间】:2014-04-11 19:53:27
【问题描述】:

代码编译没有错误,但结果只是我手机上的一个空列表。那么问题是什么?这是我在 Activity 中的 onCreate 方法

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView textView;
    ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String, String>>();
    HashMap<String, String> m;
    for(int i=0; i<=10; i++) {
        m=new HashMap<String, String>();
        m.put("key", "value"+i);
        list.add(m);
    }
    String[] from={"key"};
    int[] to={R.id.text1};
    ListView lv=(ListView)findViewById(R.id.listView);
    SimpleAdapter sa=new SimpleAdapter(this, list, R.layout.row, from, to);        
    lv.setAdapter(sa);

main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:id="@+id/listView"
    />
</LinearLayout>

row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView android:layout_width="0px" android:layout_height="fill_parent"
              android:id="@+id/text1"
              android:text="dddddddd"
            />    
</LinearLayout>

【问题讨论】:

  • 我认为这可能与您在任何地方都使用 match_parent 和 fill_parent 而不是 wrap_content...?
  • 对不起,我的英文不是很好,你的意思是在布局尺寸中到处使用 wrap_content 吗?要不然是啥?我试过了,但是没用。

标签: android listview simpleadapter


【解决方案1】:

row.xml,更改

<TextView android:layout_width="0px" android:layout_height="fill_parent"

<TextView android:layout_width="wrap_content" android:layout_height="fill_parent"

0px 的宽度将使视图不可见,只能与 layout_weight 结合使用

【讨论】:

  • 天哪,谢谢,我之前只用了0px,因为我用它和layout_weight一起使用,但是现在我忘记了这里没有其他元素可以使用weight
猜你喜欢
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多